event handling

  • Thread starter Thread starter Marco Martin
  • Start date Start date
M

Marco Martin

Hi,

I'm a little confused. I'm trying to create a (what i thought was ) simple
example of using event handlers and I keep getting a stackoverflowException.
I know my class is called clsDelegateTest, but I've been trying to simplify
by using a regular old System.EventHandler.

Here is my code:
public class clsDelegateTest
{

public string myString = "this is my String";

public event System.EventHandler eventS;

public clsDelegateTest()

{

eventS = new System.EventHandler(this.send);

}

protected void send(object obj, System.EventArgs e)

{

object o = obj;

eventS(o, e);//get stack overflow here!!!!!



}

public void sendit() //call from my main class

{

send((object)this, new System.EventArgs());

}

}

public class form1

{

private clsDelegateTest test;

static void Main()

{

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{

test = new clsDelegateTest();

test.eventS += new EventHandler(this.doit);

test.sendit();

}

private void doit(object obj, System.EventArgs e)// it never sends me here
at all, gets stuck at cldDelegatetest.send()

{

clsDelegateTest o = (clsDelegateTest)obj;

label1.Text = o.myString;

}

}



can someone PLEASE point me in the right direction? I've been going crazy
trying to find a simple axample as this.



thanks in advance,

Marco
 
Hi,
comment out this line in the clsDelegateTest constructor.
" eventS = new System.EventHandler(this.send); "

It seems the constructor is called when you call the
event, and this creates a new event etc, etc.
cheers Mark
 
Hi Mark,

thanks alot for your help. It works fine.
So if my understanding is right, The class that actualy sends the event does
not need to have a (new) event or delegate created . It just needs to have
a handle to it. (eventS) The receiving class(class actualy dealing with the
event) will receive the args as long as the args beeing sent match the
delegate right?

Thanks again and best regards,

Marco
 
Back
Top