Aug 05
2009
Felsorolható típusok készítése
Filed Under (Uncategorized) by nameless on 05-08-2009
Felsorolható típusok készítése (IEnumerator, IEnumerable):
rádió osztály:
using System;
using System.Collections.Generic;
using System.Text;
namespace CustomEnumerator
{
public class Radio
{
public void TurnOn(bool on)
{
if(on)
Console.WriteLine("Jamming...");
else
Console.WriteLine("Quiet time...");
}
}
}garage osztály:
using System;
using System.Collections.Generic;
using System.Text;
// Need this for IEnumerator
using System.Collections;
namespace CustomEnumerator
{
// Garage contains a set of Car objects.
public class Garage : IEnumerable
{
private Car[] carArray= new Car[4];
// Fill with some Car objects upon startup.
public Garage()
{
carArray[0] = new Car("Rusty", 30);
carArray[1] = new Car("Clunker", 55);
carArray[2] = new Car("Zippy", 30);
carArray[3] = new Car("Fred", 30);
}
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return carArray.GetEnumerator();
}
#endregion
}
}car osztály:
using System;
using System.Collections.Generic;
using System.Text;
namespace CustomEnumerator
{
public class Car
{
#region Member variables & Constructors
// Constant for maximum speed.
public const int MaxSpeed = 100;
// Internal state data.
private int currSpeed;
private string petName;
// Is the car still operational?
private bool carIsDead;
// A car has-a radio.
private Radio theMusicBox = new Radio();
// Constructors.
public Car() {}
public Car(string name, int currSp)
{
currSpeed = currSp;
petName = name;
}
#endregion
#region Properties
public int Speed
{
get { return currSpeed; }
set { currSpeed = value; }
}
public string PetName
{
get { return petName; }
set { petName = value; }
}
#endregion
#region Methods
public void CrankTunes(bool state)
{
// Delegate request to inner object.
theMusicBox.TurnOn(state);
}
// This time, throw an exception if the user speeds up beyond MaxSpeed.
public void Accelerate(int delta)
{
if (carIsDead)
Console.WriteLine("{0} is out of order...", petName);
else
{
currSpeed += delta;
if (currSpeed >= MaxSpeed)
{
carIsDead = true;
currSpeed = 0;
// We need to call the HelpLink property, thus we need
// to create a local variable before throwing the Exception object.
Exception ex =
new Exception(string.Format("{0} has overheated!", petName));
ex.HelpLink = "http://www.CarsRUs.com";
// Stuff in custom data regarding the error.
ex.Data.Add("TimeStamp",
string.Format("The car exploded at {0}", DateTime.Now));
ex.Data.Add("Cause", "You have a lead foot.");
throw ex;
}
else
Console.WriteLine("=> CurrSpeed = {0}", currSpeed);
}
}
#endregion
}
}program osztály:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace CustomEnumerator
{
// This seems reasonable...
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with IEnumerable / IEnumerator *****\n");
Garage carLot = new Garage();
// Hand over each car in the collection?
foreach (Car c in carLot)
{
Console.WriteLine("{0} is going {1} MPH",
c.PetName, c.Speed);
}
Console.WriteLine();
// Manually work with IEnumerator.
IEnumerator i = carLot.GetEnumerator();
i.MoveNext();
Car myCar = (Car)i.Current;
Console.WriteLine("{0} is going {1} MPH", myCar.PetName, myCar.Speed);
Console.ReadLine();
}
}
}
