Problem assigning event handler to object.event

M

moondaddy

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!
 
D

Daniel O'Connell [C# MVP]

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.
 
M

moondaddy

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.

--
(e-mail address removed)
Daniel O'Connell said:
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.
 
M

moondaddy

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(OnModeStateChangedRaised);
base.Add(obj);
}
}

//TA DA ! ! !


















--
(e-mail address removed)
moondaddy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top