T
tshad
I was looking at a tutorial for events and can't figure out his worked with
the delegate inside the object.
using System;
namespace CustomEvents
{
public class Car
{
public delegate void OwnerChangedEventHandler(string newOwner);
public event OwnerChangedEventHandler OwnerChanged;
private string make;
private string model;
private int year;
private string owner;
public string CarMake
{
get { return this.make; }
set { this.make = value; }
}
public string CarModel
{
get { return this.model; }
set { this.model = value; }
}
public int CarYear
{
get { return this.year; }
set { this.year = value; }
}
public string CarOwner
{
get { return this.owner; }
set
{
this.owner = value;
if (this.OwnerChanged != null)
this.OwnerChanged(value);
}
}
public Car()
{
}
}
}
If I do this in my main routine:
Car car = new Car();
//adds an event handler to the OwnerChanged event
car.OwnerChanged += new OwnerChangedEventHandler(car_OwnerChanged);
//setting this will fire the OwnerChanged event
car.CarOwner = "The Reddest";
I get an error on the "new OwnerChangedEventHandler" saying that
OwnerChangedEventHandler doesn't exist.
If I moved the delegate to before the car class:
**************************************
class Program
{
public delegate void OwnerChangedEventHandler(string newOwner);
public class Car
{
**************************************
Now it works fine.
Am I missing something in the article?
I get that the delegate is in the class not the object - but why did it work
in the article.
It was the last example in the article:
http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-custom-event-handlers
Thanks,
Tom
the delegate inside the object.
using System;
namespace CustomEvents
{
public class Car
{
public delegate void OwnerChangedEventHandler(string newOwner);
public event OwnerChangedEventHandler OwnerChanged;
private string make;
private string model;
private int year;
private string owner;
public string CarMake
{
get { return this.make; }
set { this.make = value; }
}
public string CarModel
{
get { return this.model; }
set { this.model = value; }
}
public int CarYear
{
get { return this.year; }
set { this.year = value; }
}
public string CarOwner
{
get { return this.owner; }
set
{
this.owner = value;
if (this.OwnerChanged != null)
this.OwnerChanged(value);
}
}
public Car()
{
}
}
}
If I do this in my main routine:
Car car = new Car();
//adds an event handler to the OwnerChanged event
car.OwnerChanged += new OwnerChangedEventHandler(car_OwnerChanged);
//setting this will fire the OwnerChanged event
car.CarOwner = "The Reddest";
I get an error on the "new OwnerChangedEventHandler" saying that
OwnerChangedEventHandler doesn't exist.
If I moved the delegate to before the car class:
**************************************
class Program
{
public delegate void OwnerChangedEventHandler(string newOwner);
public class Car
{
**************************************
Now it works fine.
Am I missing something in the article?
I get that the delegate is in the class not the object - but why did it work
in the article.
It was the last example in the article:
http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-custom-event-handlers
Thanks,
Tom