event handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco Martin

    #1

    event handling

    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 stackoverflowEx ception.
    I know my class is called clsDelegateTest , but I've been trying to simplify
    by using a regular old System.EventHan dler.

    Here is my code:
    public class clsDelegateTest
    {

    public string myString = "this is my String";

    public event System.EventHan dler eventS;

    public clsDelegateTest ()

    {

    eventS = new System.EventHan dler(this.send) ;

    }

    protected void send(object obj, System.EventArg s e)

    {

    object o = obj;

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



    }

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

    {

    send((object)th is, new System.EventArg s());

    }

    }

    public class form1

    {

    private clsDelegateTest test;

    static void Main()

    {

    Application.Run (new Form1());

    }

    private void button1_Click(o bject sender, System.EventArg s e)

    {

    test = new clsDelegateTest ();

    test.eventS += new EventHandler(th is.doit);

    test.sendit();

    }

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

    {

    clsDelegateTest o = (clsDelegateTes t)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


  • Mark Jones

    #2
    event handling

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

    It seems the constructor is called when you call the
    event, and this creates a new event etc, etc.
    cheers Mark[color=blue]
    >-----Original Message-----
    >Hi,
    >
    >I'm a little confused. I'm trying to create a (what i[/color]
    thought was ) simple[color=blue]
    >example of using event handlers and I keep getting a[/color]
    stackoverflowEx ception.[color=blue]
    >I know my class is called clsDelegateTest , but I've been[/color]
    trying to simplify[color=blue]
    >by using a regular old System.EventHan dler.
    >
    >Here is my code:
    >public class clsDelegateTest
    >{
    >
    >public string myString = "this is my String";
    >
    >public event System.EventHan dler eventS;
    >
    >public clsDelegateTest ()
    >
    >{
    >
    >eventS = new System.EventHan dler(this.send) ;
    >
    >}
    >
    >protected void send(object obj, System.EventArg s e)
    >
    >{
    >
    >object o = obj;
    >
    >eventS(o, e);//get stack overflow here!!!!!
    >
    >
    >
    >}
    >
    >public void sendit() //call from my main class
    >
    >{
    >
    >send((object)t his, new System.EventArg s());
    >
    >}
    >
    >}
    >
    >public class form1
    >
    >{
    >
    >private clsDelegateTest test;
    >
    >static void Main()
    >
    >{
    >
    >Application.Ru n(new Form1());
    >
    >}
    >
    >private void button1_Click(o bject sender,[/color]
    System.EventArg s e)[color=blue]
    >
    >{
    >
    >test = new clsDelegateTest ();
    >
    >test.eventS += new EventHandler(th is.doit);
    >
    >test.sendit( );
    >
    >}
    >
    >private void doit(object obj, System.EventArg s e)// it[/color]
    never sends me here[color=blue]
    >at all, gets stuck at cldDelegatetest .send()
    >
    >{
    >
    >clsDelegateTes t o = (clsDelegateTes t)obj;
    >
    >label1.Text = o.myString;
    >
    >}
    >
    >}
    >
    >
    >
    >can someone PLEASE point me in the right direction? I've[/color]
    been going crazy[color=blue]
    >trying to find a simple axample as this.
    >
    >
    >
    >thanks in advance,
    >
    >Marco
    >
    >
    >.
    >[/color]

    Comment

    • Marco Martin

      #3
      Re: event handling

      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


      "Mark Jones" <mark.jones@pra xa.com.au> wrote in message
      news:08d801c39d 0f$61581a60$a10 1280a@phx.gbl.. .[color=blue]
      > Hi,
      > comment out this line in the clsDelegateTest constructor.
      > " eventS = new System.EventHan dler(this.send) ; "
      >
      > It seems the constructor is called when you call the
      > event, and this creates a new event etc, etc.
      > cheers Mark[color=green]
      > >-----Original Message-----
      > >Hi,
      > >
      > >I'm a little confused. I'm trying to create a (what i[/color]
      > thought was ) simple[color=green]
      > >example of using event handlers and I keep getting a[/color]
      > stackoverflowEx ception.[color=green]
      > >I know my class is called clsDelegateTest , but I've been[/color]
      > trying to simplify[color=green]
      > >by using a regular old System.EventHan dler.
      > >
      > >Here is my code:
      > >public class clsDelegateTest
      > >{
      > >
      > >public string myString = "this is my String";
      > >
      > >public event System.EventHan dler eventS;
      > >
      > >public clsDelegateTest ()
      > >
      > >{
      > >
      > >eventS = new System.EventHan dler(this.send) ;
      > >
      > >}
      > >
      > >protected void send(object obj, System.EventArg s e)
      > >
      > >{
      > >
      > >object o = obj;
      > >
      > >eventS(o, e);//get stack overflow here!!!!!
      > >
      > >
      > >
      > >}
      > >
      > >public void sendit() //call from my main class
      > >
      > >{
      > >
      > >send((object)t his, new System.EventArg s());
      > >
      > >}
      > >
      > >}
      > >
      > >public class form1
      > >
      > >{
      > >
      > >private clsDelegateTest test;
      > >
      > >static void Main()
      > >
      > >{
      > >
      > >Application.Ru n(new Form1());
      > >
      > >}
      > >
      > >private void button1_Click(o bject sender,[/color]
      > System.EventArg s e)[color=green]
      > >
      > >{
      > >
      > >test = new clsDelegateTest ();
      > >
      > >test.eventS += new EventHandler(th is.doit);
      > >
      > >test.sendit( );
      > >
      > >}
      > >
      > >private void doit(object obj, System.EventArg s e)// it[/color]
      > never sends me here[color=green]
      > >at all, gets stuck at cldDelegatetest .send()
      > >
      > >{
      > >
      > >clsDelegateTes t o = (clsDelegateTes t)obj;
      > >
      > >label1.Text = o.myString;
      > >
      > >}
      > >
      > >}
      > >
      > >
      > >
      > >can someone PLEASE point me in the right direction? I've[/color]
      > been going crazy[color=green]
      > >trying to find a simple axample as this.
      > >
      > >
      > >
      > >thanks in advance,
      > >
      > >Marco
      > >
      > >
      > >.
      > >[/color][/color]


      Comment

      Working...