Results 1 to 26 of 26

Thread: events & delegates

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    events & delegates

    hmmm i've been reading about this and i got a few questions about it...

    hmm...what is the advantage of my class inherit from EventArgs? if i do so i can't make my class inherit from anymore class so it looks like a disavantage

    and...i want to have events that has different parameters between them...do i have to have 1delegate for each "type" of parameters? maybe the answer is inheriting from EventArgs..omg..i am not getting this lol

  2. #2
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327

    Red face

    It depends what you are trying to do...

    If you are trying to supply an event procedure, like from a user control, for any custom event you want, you will need to create a class inherited from EventArgs. Actually, the way I understand this from MS is that if you want others to be consuming these events like for a class library or user control, then you should pack any custom arguments into a EventArgs subclass.

    Are delegates CLS-Compliant? I'm not sure VB has delegates...

    If however you are looking for something more like a C callback function, something that you are using internally, you should use just a delegate. The delegate object will be a bit more direct than stuffing all the arguments into your EventArgs subclass.

    hmmm....after reading your post again, I'm not sure I answered your question...

    Only arguments for custom event procedures need to be a subclass of EventArgs. If you have some simple events, like a simple notification, you can just use the standard EventArgs class.

    PHP Code:
    // simple notification:
    public delegate void ChangedEventHandler(object senderEventArgs e); 
    If you want to add some other parameters to the event, then create a class, CustomEventArgs inherited from EventArgs that have some additional public properties.

    PHP Code:
    // custom notification:
    public delegate void CustomEvent(object senderCustomEventArgs e); 
    ...let me know if that's still confusing and I will try again...
    -scott
    he he he

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Yes VB has delegates.
    Dont gain the world and lose your soul

  4. #4

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i want to make a class that has events...but how do i make that several events use the same delegate if they have different parameters?

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You cant. The method you are calling have to have the same signiture of the delegate, so each event that you want to have a diferent parameter, must match the delegate that will invoke it.

    eg:

    Code:
    public delegate int CompareNumsDelegate(int num1, int num2);
    
    //method
    int MyFunc(int num1, int num2)
    {
          if(num1 > num2)
          {
               return num1;
          }
          else
               return 0;
    }
    
    CompareNumsDelegate comDel = new CompareNumsDelegate(MyFunc);
    You can now call comDel just as if it was the function MyFunc like this:
    Code:
    comDel(5,2);
    Last edited by DevGrp; Nov 10th, 2002 at 06:46 PM.
    Dont gain the world and lose your soul

  6. #6

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    so thats why ppl put things under EventArgs?

    if yes how do i do that?

  7. #7
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    so thats why ppl put things under EventArgs?
    What do you mean?
    Dont gain the world and lose your soul

  8. #8

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i have a class for internet i want an event for Connected other for Disconnected blablabla they all have different parameters...will i have to make 300 delegates + 300 events? omg there must be another way

  9. #9
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I'm sure you wont need all 300 . What I would do is make classes inheriting from EventArgs, then extending those classed to include the functionality for events that you would like.

    eg.
    Class DisconnectedEventHandler
    Class ConnectedEventHandler

    Then group the functionality.
    Dont gain the world and lose your soul

  10. #10

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    im not getting

  11. #11
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    What are the events that you need?
    Dont gain the world and lose your soul

  12. #12

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    lets imagine i want this events:

    connected(string ip)
    disconnected(string exitMessage, string ip)

    receivedMessage(string message, string ip, string nickName)
    etc..

    will i have to make a delegate for each one of these because they all have differente params?

  13. #13
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Yes.
    Dont gain the world and lose your soul

  14. #14

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    oh great..lol

  15. #15

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ah what names do u give to the delegates?
    maybe givin them names like param1 meaning this has only 1 param would help or no? lol to me this thing of delegates is the worse **** ever...in vb.net we have delegates but dont have to use them in events what seems a LOT better

  16. #16

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm the usual when programming in C# is somewhat for each event a delegate?

  17. #17

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    how do i make to put delegates and events in the same class?

    the book only says how to put the delegates under the same namespace...

    upper:
    PHP Code:
    namespace WindowsApplication5
    {
            public 
    delegate void Event(string message); 
    class
    PHP Code:

        
    class TESTE
        
    {
            public 
    event Event teste;

            private 
    void lol()
            {
                
    teste("...");
            }

            
        } 
    form
    PHP Code:
            private void Form1_Load(object senderSystem.EventArgs e)
            {
                
    TESTE test = new TESTE();
                
    test.teste += new Event(my_teste);
                
    my_teste("lol...");
            }

            private 
    void my_teste(string mensagem)
            {
                
    MessageBox.Show(mensagem "\nlol sukas");
            }

        } 

  18. #18
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Ok, I have a little example;

    PHP Code:
    //Class with your events and delegates
    public class Tester
    {
        public 
    delegate void ConnectedHandler(string ip);
        public 
    delegate void DisconnectedHandler(string exitMsgstring ip);
        public 
    delegate void ReceivedMessageHandler(string msgstring ipstring nick);
            
        public 
    event ConnectedHandler Connected;
        public 
    event DisconnectedHandler Disconnected;
        public 
    event ReceivedMessageHandler ReceiveMessage;
            
        public 
    Tester()
        {
        }

        public 
    void Connect(string ip)
        {
            if(
    Connected != null)
            {
                
    Connected("Connected to " ip);
            }
        }
        public 
    void Disconnect(string ip)
        {
            if(
    Disconnected != null)
            {
                
    Disconnected("Disconnected from ""191.168.0.1");
            }
        }
        public 
    void Receive()
        {
            if(
    ReceiveMessage != null)
            {
                
    ReceiveMessage("How are you""191.168.0.1""Dev");
            }
        }

    Using the Events
    PHP Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace 
    TestEvents
    {
        
        public class 
    Form1 System.Windows.Forms.Form 
        
    {
            private 
    System.Windows.Forms.Button button1;
            
    /// <summary>
            /// Required designer variable.
            /// </summary>
            
    private System.ComponentModel.Container components null;
            private 
    System.Windows.Forms.FolderBrowserDialog fd;
            private 
    System.Windows.Forms.Button button2;
            private 
    System.Windows.Forms.Label label1;
            
    Tester t = new Tester();
                    
            public 
    Form1()
            {
                
    //
                // Required for Windows Form Designer support
                //
                
    InitializeComponent();
                
                
    //
                // TODO: Add any constructor code after InitializeComponent call
                //
                
    Application.EnableVisualStyles();
                
                
    t.ReceiveMessage +=new TestEvents.Tester.ReceivedMessageHandler(t_ReceiveMessage);
                
    t.Connected +=new TestEvents.Tester.ConnectedHandler(t_Connected);   
                
    t.Disconnected +=new TestEvents.Tester.DisconnectedHandler(t_Disconnected);
                            
            }
            private 
    void Form1_Load(object senderSystem.EventArgs e)
            {
                
    t.Connect("191.168.0.1");
                
    t.Disconnect("191.168.0.1");
                
    t.Receive();
            }
            private 
    void t_Connected(string ip)
            {
                
    MessageBox.Show(ip);
            }

            private 
    void t_Disconnected(string exitMsgstring ip)
            {
                
    MessageBox.Show(exitMsg ip);
            }

            private 
    void t_ReceiveMessage(string msgstring ipstring nick)
            {
                
    MessageBox.Show(msg " " nick " from " ip);
            }
        }

    Dont gain the world and lose your soul

  19. #19

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ah ill have to put this always in the form?

    t.ReceiveMessage +=new TestEvents.Tester.ReceivedMessageHandler(t_ReceiveMessage);
    t.Connected +=new TestEvents.Tester.ConnectedHandler(t_Connected);
    t.Disconnected +=new TestEvents.Tester.DisconnectedHandler(t_Disconnected);

    bleh..thats g@y

  20. #20

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    imagine i have a class that i do..then i send that class to my friend..in order to put that class + class' events working he'll have to put that

    t.ReceiveMessage +=new TestEvents.Tester.ReceivedMessageHandler(t_ReceiveMessage);
    t.Connected +=new TestEvents.Tester.ConnectedHandler(t_Connected);
    t.Disconnected +=new TestEvents.Tester.DisconnectedHandler(t_Disconnected);

    things in his form? isnt there an hit and run like vb way of doin this? it seems so complicated damn...waste of time in comp. with vb

  21. #21
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    It seems like a waste of time, but the advantages outweight the inconvienence. You can add more than one procedure to be called to the delegate if you want. This means that if you have multiple forms that must recieve the event, they can all subscribe to the event. Another way they are useful is you can turn off and on the event at run time.

    Now, if you create a control with events, the user of your control should be able to let the IDE subscribe to the event for them by clicking the Event button in the properties window and double clicking the event.

    What they provide is more flexibility.

    Sure VB hides it from you, but can you really call yourself a programmer without knowing how they really work?...lol.

  22. #22

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    it just seems damn confusing

  23. #23

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ah i think im getting in now after watching carefully DevGroup's example..but it's still confusing lolol

  24. #24

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    woohooo tested in a class now made by me and worked lol i rule

  25. #25
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Good to know that it works and you're finally getting it. Dont worry about the confusion, it will pass . Sometimes it still a little confusing for me, then I have to pull out my notes .
    Dont gain the world and lose your soul

  26. #26

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    now i saw a class i downloaded from net and only now i realised that for each event i have to make a delegate..i though there was a way to have only 1delegate for all events lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width