473,623 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem assigning event handler to object.event

C# 2.0

I have declared some event arguments like this:

public class ModeStateChange dEventArgs : EventArgs
{
Gen.DataState myState;

public ModeStateChange dEventArgs() { }

public ModeStateChange dEventArgs(Gen. DataState val)
{
myState = val;
}

public Gen.DataState MyState
{
get { return myState; }
}
}
In a business object I declare the event and use it like this:

Public class BizClass
{

public event EventHandler<Mo deStateChangedE ventArgs>
ModeStateChange dRaised;
protected virtual void
OnModeStateChan gedRaised(ModeS tateChangedEven tArgs e)
{
try
{
EventHandler<Mo deStateChangedE ventArgs> handler =
ModeStateChange dRaised;
if (handler != null)
{
handler(this, e);
}
}
catch (Exception ex)
{
ExceptionHandle r.ErrLog(ex);
}
}

//And I raise the event like this:

Void SomeMethod()
{
ModeStateChange dEventArgs arg = new
ModeStateChange dEventArgs(myMo deState);
OnModeStateChan gedRaised(arg);
}
}

Now is a list class I want to create instances of BizClass, assign and event
handler to them and then add them to the list.
Public class ListOfBizClass : BindingList< BizClass >, IBindingList
{
//This is where I need help...
public event EventHandler<Mo deStateChangedE ventArgs>
ModeStateChange dRaised;
protected virtual void
OnModeStateChan gedRaised(ModeS tateChangedEven tArgs e)
{
try
{
EventHandler<Mo deStateChangedE ventArgs> handler =
ModeStateChange dRaised;
if (handler != null)
{
handler(this, e);
}
}
catch (Exception ex)
{
ExceptionHandle r.ErrLog(ex);
}
}
public void GetListFromData base()
{
base.Clear();
dsContactMngr ds = oData.PersonGet RecShort();
foreach (dsContactMngr. tbPersonShortLi stRow dr in
ds.tbPersonShor tList)
{
BizClass obj = new BizClass (dr.ID, dr.FName, dr.LName);
//This is where I get a compile error: 'new
EventHandler(On ModeStateChange dRaised)'
//Compile error says: 'No overload for 'OnModeStateCha ngedRaised' matches
delegate'
obj.ModeStateCh angedRaised += new
EventHandler(On ModeStateChange dRaised);
base.Add(obj);
}
}
)

Can someone shed explain what I need to do here?

THANKS!
--
mo*******@nospa m.nospam
Apr 13 '06 #1
3 1560

public void GetListFromData base()
{
base.Clear();
dsContactMngr ds = oData.PersonGet RecShort();
foreach (dsContactMngr. tbPersonShortLi stRow dr in
ds.tbPersonShor tList)
{
BizClass obj = new BizClass (dr.ID, dr.FName, dr.LName);
//This is where I get a compile error: 'new
EventHandler(On ModeStateChange dRaised)'
//Compile error says: 'No overload for 'OnModeStateCha ngedRaised' matches
delegate'
obj.ModeStateCh angedRaised += new
EventHandler(On ModeStateChange dRaised);
base.Add(obj);
}
}
)

Can someone shed explain what I need to do here?

Thats because the proper signature for EventHandler<T> would be
protected virtual void OnModeStateChan gedRaised(objec t o,
ModeStateChange dEventArgs e);

There is always an object parameter at the front which will contain the
object itself.
THANKS!
--
mo*******@nospa m.nospam

Apr 13 '06 #2
I've tried just about every possible syntax I can think of including what I
think you are talking about. Using my code sample, could you please write
out exactly what it would look like?

Thanks.

--
mo*******@nospa m.nospam
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eG******** ******@TK2MSFTN GP03.phx.gbl...

public void GetListFromData base()
{
base.Clear();
dsContactMngr ds = oData.PersonGet RecShort();
foreach (dsContactMngr. tbPersonShortLi stRow dr in
ds.tbPersonShor tList)
{
BizClass obj = new BizClass (dr.ID, dr.FName, dr.LName);
//This is where I get a compile error: 'new
EventHandler(On ModeStateChange dRaised)'
//Compile error says: 'No overload for 'OnModeStateCha ngedRaised' matches
delegate'
obj.ModeStateCh angedRaised += new
EventHandler(On ModeStateChange dRaised);
base.Add(obj);
}
}
)

Can someone shed explain what I need to do here?


Thats because the proper signature for EventHandler<T> would be
protected virtual void OnModeStateChan gedRaised(objec t o,
ModeStateChange dEventArgs e);

There is always an object parameter at the front which will contain the
object itself.
THANKS!
--
mo*******@nospa m.nospam


Apr 13 '06 #3
Ok I finaly figured it out. I downloaded the code for Rocky Lhotka's c#2.0
clsa and was able to understand it better by studing his code. For anyone
interested, here's what worked:
public class ModeStateChange dEventArgs : EventArgs
{
Gen.DataState myState;

public ModeStateChange dEventArgs() { }

public ModeStateChange dEventArgs(Gen. DataState val)
{
myState = val;
}

public virtual Gen.DataState MyState
{
get { return myState; }
}
}
In a business object I declare the event and use it like this:

Public class BizClass
{
public event ModeStateChange dEventHandler ModeStateChange dRaised;

protected void OnModeStateChan gedRaised(objec t sender,
ModeStateChange dEventArgs e)
{
try
{
if e!= null)
{
ModeStateChange dRaised(this, e);
}
}
catch (Exception ex)
{
ExceptionHandle r.ErrLog(ex);
}
}
//And I raise the event like this:

Void SomeMethod()
{
ModeStateChange dEventArgs arg = new
ModeStateChange dEventArgs(myMo deState);
OnModeStateChan gedRaised(this, arg);
}
}
=============== =============== =============== ============

Now in a list class I want to create instances of BizClass, assign and event
handler to them and then add them to the list.
Public class ListOfBizClass : BindingList< BizClass >, IBindingList
{
public event EventHandler<Mo deStateChangedE ventArgs>
ModeStateChange dRaised;

protected void OnModeStateChan gedRaised(objec t sender,
ModeStateChange dEventArgs e)
{
try
{
if (e != null)
{
//do something
Console.WriteLi ne("It works (protected void
OnModeStateChan gedRaised(objec t sender, ModeStateChange dEventArgs e)): " +
e.MyState.ToStr ing());
}
}
catch (Exception ex)
{
ExceptionHandle r.ErrLog(ex);
}
}
//Load this list class with a bunch of biz objects and assign the event to
each object (suto code...)
public void GetListFromData base()
{
base.Clear();
dsContactMngr ds = oData.PersonGet RecShort();

foreach (dsContactMngr. tbPersonShortLi stRow dr in
ds.tbPersonShor tList)
{
BizClass obj = new BizClass(dr.ID, dr.FName, dr.LName);
obj.ModeStateCh angedRaised += new
ModeStateChange dEventHandler(O nModeStateChang edRaised);
base.Add(obj);
}
}

//TA DA ! ! !






--
mo*******@nospa m.nospam
"moondaddy" <mo*******@nosp am.nospam> wrote in message
news:OG******** ******@TK2MSFTN GP05.phx.gbl...
I've tried just about every possible syntax I can think of including what
I think you are talking about. Using my code sample, could you please
write out exactly what it would look like?

Thanks.

--
mo*******@nospa m.nospam
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eG******** ******@TK2MSFTN GP03.phx.gbl...

public void GetListFromData base()
{
base.Clear();
dsContactMngr ds = oData.PersonGet RecShort();
foreach (dsContactMngr. tbPersonShortLi stRow dr in
ds.tbPersonShor tList)
{
BizClass obj = new BizClass (dr.ID, dr.FName, dr.LName);
//This is where I get a compile error: 'new
EventHandler(On ModeStateChange dRaised)'
//Compile error says: 'No overload for 'OnModeStateCha ngedRaised'
matches delegate'
obj.ModeStateCh angedRaised += new
EventHandler(On ModeStateChange dRaised);
base.Add(obj);
}
}
)

Can someone shed explain what I need to do here?


Thats because the proper signature for EventHandler<T> would be
protected virtual void OnModeStateChan gedRaised(objec t o,
ModeStateChange dEventArgs e);

There is always an object parameter at the front which will contain the
object itself.
THANKS!
--
mo*******@nospa m.nospam



Apr 13 '06 #4

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

Similar topics

33
2817
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d" onclick="show(this)">111111</li>
2
1547
by: D. Patterson | last post by:
Hello all. I've a bit of a problem. I would like to use a method in an object as an event handler. Unfortunately, when the method is called the "this" object references the event trigger rather than my object. For example, with the following code, an alert will be shown when the page loads where the value of my_name is "My Object" and nodeName is 'undefined". Dismissing
10
2221
by: Opa | last post by:
I have tried for two days to solve this problem with no luck. I have a singleton class which has some events declared. When I inherit from this class the events don't seem to come along with it. The event in the singleton class always evaluates to null even when I cleary subscribe to it. Does anyone have some sample code of how to do this, or is this not possible.
2
3734
by: Jose Suero | last post by:
Hi all I have a dynamically created button, I can add an event handler with: AddHandler button.click, AddressOf static_function This works great, but what I need is to create a function that takes the control, the event and the function as parameters, something like: function addevent(control as object, event as string, functionname as string)
15
6616
by: Iced Crow | last post by:
In C# I know that you can use delegates to assing multiple addresses of sub and functions to a delegate and have it fire multiple procedures... How do I do this in VB? I only know of assigning a single method to a delegate in VB.NET. I want to use it as in C#... to fire multiple events. Thanks in advance!
10
4006
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
2
6443
by: Martin Honnen | last post by:
I was playing around with canvas support in recent Safari, Mozilla and Opera (only version 9 preview) but run into issues with Safari related to the very old DOM Level 0 Image object for preloading images. When doing e.g. var img = new Image(); img.onload = function (evt) { alert(this); }; img.src = 'whatever.gif';
18
3322
by: len.hartley | last post by:
Hi, I am trying to pop-up a window when the user clicks on an image. The problem is that when the user clicks on the image and the window pops up OK, but the window underneath also proceeds to the image. The desired behavior is that when the pop-up is invoked, I want the underlying window to stay put. I don't have this problem when I run the code on my local computer but I do have it when I run the code on geocities.
6
1808
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool. One of the requirements is that the calendar will need to display a varying number of months (1..3)
0
8217
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8661
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2590
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1467
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.