Home
Forums
New posts
Search forums
Articles
Latest reviews
Search resources
Members
Current visitors
Newsgroups
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Home
Forums
Newsgroups
Microsoft DotNet
Microsoft C# .NET
Delegate for event in class
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="tshad, post: 13868372"] 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: [URL]http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-custom-event-handlers[/URL] Thanks, Tom [/QUOTE]
Verification
Post reply
Home
Forums
Newsgroups
Microsoft DotNet
Microsoft C# .NET
Delegate for event in class
Top