Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

How to register a property to event ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sharon - 15 Nov 2006 12:14 GMT
Is there a way to have e delegate for property instead of method ?

For method like this:

void doSomthing(int val);

We can have a delegate like: public delegate void dodo(int val);

and event fro it in the shape of:   public event dodo dodoHandler;

and then the above method can be register to the event like this:

dodo += doSomthing;

But in my case I want to register to event using property in the form of:

int m_filed;
public int Filed
{
   get { return filed; }
   set { filed = value; }
}

Can I do that and How ?

---------
Thanks
Sharon
Marc Gravell - 15 Nov 2006 12:23 GMT
No; and what would you have the property do (on invoke) anyway? properties
are really a  pair of methods; a get and a set. The Set would match your
delegate.

Event delegates can technically follow any pattern, but the void
SomeHandler(object, SomeEventArgs : EventArgs) pattern (or the
EventHandler<T> where T : EventArgs pattern) should usually be followed.

Using your unusual pattern, you could use 2.0 delegates to do:

object.SomeEvent += delegate (int val) {
 someOtherObject.SomeProperty += val;
};

But I don't think it is a good idea.

What are you actually trying to achieve? This might steer the best answer...
For instance - is this for property-change notifications? or what?

Marc
Sharon - 15 Nov 2006 12:43 GMT
What I'm actually trying to achieve is:

I already have a property to my field.

Some other class in the application has a delegate/event (which I'm writing).

I need to register some function to this delegate to set my filed, but my
filed already has a the SET function inside the property of his. And I want
to avoid writing another Set method just for this event.

How can I do that?

------
Thanks
Sharon
Marc Gravell - 15 Nov 2006 12:55 GMT
Then I would use the 2.0 inline delegate syntax, but using the standard
event pattern. Depending on the scenario, the new value could be either in
the event-args, or back on the sender (typical for a SomethingChanged
event), i.e.

// EventHandler example
someInstance.SomethingChanged += delegate {
 someOtherInstancePossiblyThis.SomeProperty = someInstance.Something;
};
or
// SomeEventHandler example where SomeEventArgs has a .SomeValue property
someInstance.SomeEvent += delegate (object sender, SomeEventArgs args) {
 someOtherInstancePossiblyThis.SomeProperty = args.SomeValue;
};

(note that in either case you might also case "sender" instead of using
someOtherInstancePossiblyThis)

Marc
Jon Skeet [C# MVP] - 15 Nov 2006 19:10 GMT
> What I'm actually trying to achieve is:
>
[quoted text clipped - 7 lines]
>
> How can I do that?

You can use reflection to set it up. A complete example is below. It's
pretty longwinded, but if you needed this on a regular basis you could
easily set it up in a helper method - especially with generics, if
you're using 2.0.

using System;
using System.Reflection;

delegate void SettingHandler (string value);

class PropertyClass
{
   string name;

   public string Name
   {
       get { return name; }
       set { name = value; }
   }
}

class EventClass
{
   public event SettingHandler SetEvent;
   
   public void RaiseEvent (string value)
   {
       SetEvent(value);
   }
}

class Test
{
   static void Main()
   {
       PropertyClass pc = new PropertyClass();
       
       EventClass ec = new EventClass();
       
       PropertyInfo prop = typeof(PropertyClass).GetProperty("Name");
       MethodInfo method = prop.GetSetMethod();
       
       SettingHandler handler = (SettingHandler)
            Delegate.CreateDelegate
                (typeof(SettingHandler), pc, method);
       
       ec.SetEvent += handler;
       
       ec.RaiseEvent ("testing");
       
       Console.WriteLine (pc.Name);
   }
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.