473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to register a property to event ?

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
Nov 15 '06 #1
4 2049
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<Twhere 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
Nov 15 '06 #2
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
Nov 15 '06 #3
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
Nov 15 '06 #4
Sharon <Sh*****@newsgroups.nospamwrote:
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?
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);
}
}

--
Jon Skeet - <sk***@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
Nov 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: MarionEll | last post by:
XML 2004 Program Expands -New Exhibitors, Atom Hackathon, Interop Demos, XML Artwork, and more... XML Conference & Exposition 2004 November 15-19, 2004 Marriott Wardman Park Hotel
1
by: David Veeneman | last post by:
This posting is for the Google crawler and requires no response. I have created a multi-level object hierarchy, in which the root object needs to be notified whenever any property of any object...
2
by: SimonZ | last post by:
I have function in my app_code folder. Is it possible to register client script block from app_code function? public static void CheckSessionModal() { String scriptValue="";
2
by: Nicke | last post by:
I have accomplished this on my machine with vs2005 but how can I deploy the dll on a web server for example? Has anyone made a deployment project that does this? this is my class: ...
1
by: lourivas | last post by:
Hi, I create a class with a property called MyProperty that needs to register a client script if the value is set to true. When i change the property value to true i always receive a message...
0
by: Marcel - IDUG Europe 2006 | last post by:
Hi DB2 user, Register Before the Wednesday, 27 September Deadline http://conferences.idug.org/europe Don't miss your chance to attend the premier European IDUG educational event - IDUG 2006 -...
0
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi all, Can somebody give me a hint why my custom install action does not register event log sources, when I run the MSI package generated by VS2005. The same install action DLL executed via...
0
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Actually, my installer package is not for a Windows Service, but for a WinForms application. Well, it is kind of both: this is a multi-project solution with its main target being a WinForms...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.