473,387 Members | 1,318 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,387 software developers and data experts.

Problem assigning event handler to object.event

C# 2.0

I have declared some event arguments like this:

public class ModeStateChangedEventArgs : EventArgs
{
Gen.DataState myState;

public ModeStateChangedEventArgs() { }

public ModeStateChangedEventArgs(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<ModeStateChangedEventArgs>
ModeStateChangedRaised;
protected virtual void
OnModeStateChangedRaised(ModeStateChangedEventArgs e)
{
try
{
EventHandler<ModeStateChangedEventArgs> handler =
ModeStateChangedRaised;
if (handler != null)
{
handler(this, e);
}
}
catch (Exception ex)
{
ExceptionHandler.ErrLog(ex);
}
}

//And I raise the event like this:

Void SomeMethod()
{
ModeStateChangedEventArgs arg = new
ModeStateChangedEventArgs(myModeState);
OnModeStateChangedRaised(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<ModeStateChangedEventArgs>
ModeStateChangedRaised;
protected virtual void
OnModeStateChangedRaised(ModeStateChangedEventArgs e)
{
try
{
EventHandler<ModeStateChangedEventArgs> handler =
ModeStateChangedRaised;
if (handler != null)
{
handler(this, e);
}
}
catch (Exception ex)
{
ExceptionHandler.ErrLog(ex);
}
}
public void GetListFromDatabase()
{
base.Clear();
dsContactMngr ds = oData.PersonGetRecShort();
foreach (dsContactMngr.tbPersonShortListRow dr in
ds.tbPersonShortList)
{
BizClass obj = new BizClass (dr.ID, dr.FName, dr.LName);
//This is where I get a compile error: 'new
EventHandler(OnModeStateChangedRaised)'
//Compile error says: 'No overload for 'OnModeStateChangedRaised' matches
delegate'
obj.ModeStateChangedRaised += new
EventHandler(OnModeStateChangedRaised);
base.Add(obj);
}
}
)

Can someone shed explain what I need to do here?

THANKS!
--
mo*******@nospam.nospam
Apr 13 '06 #1
3 1547

public void GetListFromDatabase()
{
base.Clear();
dsContactMngr ds = oData.PersonGetRecShort();
foreach (dsContactMngr.tbPersonShortListRow dr in
ds.tbPersonShortList)
{
BizClass obj = new BizClass (dr.ID, dr.FName, dr.LName);
//This is where I get a compile error: 'new
EventHandler(OnModeStateChangedRaised)'
//Compile error says: 'No overload for 'OnModeStateChangedRaised' matches
delegate'
obj.ModeStateChangedRaised += new
EventHandler(OnModeStateChangedRaised);
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 OnModeStateChangedRaised(object o,
ModeStateChangedEventArgs e);

There is always an object parameter at the front which will contain the
object itself.
THANKS!
--
mo*******@nospam.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*******@nospam.nospam
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eG**************@TK2MSFTNGP03.phx.gbl...

public void GetListFromDatabase()
{
base.Clear();
dsContactMngr ds = oData.PersonGetRecShort();
foreach (dsContactMngr.tbPersonShortListRow dr in
ds.tbPersonShortList)
{
BizClass obj = new BizClass (dr.ID, dr.FName, dr.LName);
//This is where I get a compile error: 'new
EventHandler(OnModeStateChangedRaised)'
//Compile error says: 'No overload for 'OnModeStateChangedRaised' matches
delegate'
obj.ModeStateChangedRaised += new
EventHandler(OnModeStateChangedRaised);
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 OnModeStateChangedRaised(object o,
ModeStateChangedEventArgs e);

There is always an object parameter at the front which will contain the
object itself.
THANKS!
--
mo*******@nospam.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 ModeStateChangedEventArgs : EventArgs
{
Gen.DataState myState;

public ModeStateChangedEventArgs() { }

public ModeStateChangedEventArgs(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 ModeStateChangedEventHandler ModeStateChangedRaised;

protected void OnModeStateChangedRaised(object sender,
ModeStateChangedEventArgs e)
{
try
{
if e!= null)
{
ModeStateChangedRaised(this, e);
}
}
catch (Exception ex)
{
ExceptionHandler.ErrLog(ex);
}
}
//And I raise the event like this:

Void SomeMethod()
{
ModeStateChangedEventArgs arg = new
ModeStateChangedEventArgs(myModeState);
OnModeStateChangedRaised(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<ModeStateChangedEventArgs>
ModeStateChangedRaised;

protected void OnModeStateChangedRaised(object sender,
ModeStateChangedEventArgs e)
{
try
{
if (e != null)
{
//do something
Console.WriteLine("It works (protected void
OnModeStateChangedRaised(object sender, ModeStateChangedEventArgs e)): " +
e.MyState.ToString());
}
}
catch (Exception ex)
{
ExceptionHandler.ErrLog(ex);
}
}
//Load this list class with a bunch of biz objects and assign the event to
each object (suto code...)
public void GetListFromDatabase()
{
base.Clear();
dsContactMngr ds = oData.PersonGetRecShort();

foreach (dsContactMngr.tbPersonShortListRow dr in
ds.tbPersonShortList)
{
BizClass obj = new BizClass(dr.ID, dr.FName, dr.LName);
obj.ModeStateChangedRaised += new
ModeStateChangedEventHandler(OnModeStateChangedRai sed);
base.Add(obj);
}
}

//TA DA ! ! !






--
mo*******@nospam.nospam
"moondaddy" <mo*******@nospam.nospam> wrote in message
news:OG**************@TK2MSFTNGP05.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*******@nospam.nospam
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eG**************@TK2MSFTNGP03.phx.gbl...

public void GetListFromDatabase()
{
base.Clear();
dsContactMngr ds = oData.PersonGetRecShort();
foreach (dsContactMngr.tbPersonShortListRow dr in
ds.tbPersonShortList)
{
BizClass obj = new BizClass (dr.ID, dr.FName, dr.LName);
//This is where I get a compile error: 'new
EventHandler(OnModeStateChangedRaised)'
//Compile error says: 'No overload for 'OnModeStateChangedRaised'
matches delegate'
obj.ModeStateChangedRaised += new
EventHandler(OnModeStateChangedRaised);
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 OnModeStateChangedRaised(object o,
ModeStateChangedEventArgs e);

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



Apr 13 '06 #4

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

Similar topics

33
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"...
2
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...
10
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....
2
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...
15
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...
10
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...
2
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...
18
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...
6
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....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.