W
Whomsoever
Guest
I have the Car class as described here under.
I was trying to see if I could define a private member variable of the delegate type as follows :
// Define a member variable of this delegate :
private CarEngineHandler<string> listOfHandlers;
but it doesn't work. Can anyone please advise where the code is incorrect or is it just not possible to declare a member variable in this manner when using generics ?
Any assistance would be gratefully appreciated.
Thank you.
public class Car
{
// Define a delegate type :
public delegate void CarEngineHandler<T>(T msgForCaller);
// Define a member variable of this delegate :
private CarEngineHandler<T> listOfHandlers;
// Add a registration function for the caller (this helper function is required ,
// because we made our member variable , listOfHandlers , private, thereby making
// it inaccessible outside this class) :
public void RegisterWithCarEngine(CarEngineHandler<T> methodToCall)
{
//listOfHandlers = methodToCall;
// TO make the delegate "multicast"
listOfHandlers += methodToCall;
}
// Add a method which allows a caller to deregister a method from the delegate's
// invocation list :
public void DeregisterWithCarEngine(CarEngineHandler<T> methodToCall)
{
listOfHandlers -= methodToCall;
}
// Internal state data :
public int CurrentSpeed { get; set; }
public int MaxSpeed { get; set; } = 100;
public string PetName { get; set; }
// Is the car dead or alive ?
private bool carIsDead;
// Class Constructors :
public Car() { }
public Car(string name, int maxSp, int currSp)
{
CurrentSpeed = currSp;
MaxSpeed = maxSp;
PetName = name;
}
// At this point, you need to create the Accelerate() method.
// Recall, the point here is to allow a Car object
// to send engine-related messages to any subscribed listener.
public void Accelerate(int delta)
{
// If the car is dead, send message :
if (carIsDead)
{
if (listOfHandlers != null)
{
listOfHandlers("Sorry, this car is dead !");
}
}
else
{
CurrentSpeed += delta;
// Is this car "almost dead"
if (10 == (MaxSpeed - CurrentSpeed) && listOfHandlers != null)
{
listOfHandlers("Careful ! The car engine is going to seize.");
}
if (CurrentSpeed >= MaxSpeed)
{
carIsDead = true;
}
else
Console.WriteLine("Current speed = {0} : ", CurrentSpeed);
}
}
}
}
Continue reading...
I was trying to see if I could define a private member variable of the delegate type as follows :
// Define a member variable of this delegate :
private CarEngineHandler<string> listOfHandlers;
but it doesn't work. Can anyone please advise where the code is incorrect or is it just not possible to declare a member variable in this manner when using generics ?
Any assistance would be gratefully appreciated.
Thank you.
public class Car
{
// Define a delegate type :
public delegate void CarEngineHandler<T>(T msgForCaller);
// Define a member variable of this delegate :
private CarEngineHandler<T> listOfHandlers;
// Add a registration function for the caller (this helper function is required ,
// because we made our member variable , listOfHandlers , private, thereby making
// it inaccessible outside this class) :
public void RegisterWithCarEngine(CarEngineHandler<T> methodToCall)
{
//listOfHandlers = methodToCall;
// TO make the delegate "multicast"
listOfHandlers += methodToCall;
}
// Add a method which allows a caller to deregister a method from the delegate's
// invocation list :
public void DeregisterWithCarEngine(CarEngineHandler<T> methodToCall)
{
listOfHandlers -= methodToCall;
}
// Internal state data :
public int CurrentSpeed { get; set; }
public int MaxSpeed { get; set; } = 100;
public string PetName { get; set; }
// Is the car dead or alive ?
private bool carIsDead;
// Class Constructors :
public Car() { }
public Car(string name, int maxSp, int currSp)
{
CurrentSpeed = currSp;
MaxSpeed = maxSp;
PetName = name;
}
// At this point, you need to create the Accelerate() method.
// Recall, the point here is to allow a Car object
// to send engine-related messages to any subscribed listener.
public void Accelerate(int delta)
{
// If the car is dead, send message :
if (carIsDead)
{
if (listOfHandlers != null)
{
listOfHandlers("Sorry, this car is dead !");
}
}
else
{
CurrentSpeed += delta;
// Is this car "almost dead"
if (10 == (MaxSpeed - CurrentSpeed) && listOfHandlers != null)
{
listOfHandlers("Careful ! The car engine is going to seize.");
}
if (CurrentSpeed >= MaxSpeed)
{
carIsDead = true;
}
else
Console.WriteLine("Current speed = {0} : ", CurrentSpeed);
}
}
}
}
Continue reading...