Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can i access event of other class

How can i access event of other class

Scheduled Pinned Locked Moved C#
helpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Naveed727
    wrote on last edited by
    #1

    I want to use the event of other class in my main clas help me out. public class ParenctClass { public delegate void myDel(string data); . public event myDel MYEVENT; public void Some Fucntion { // Thourgh function i put some data in "data" } }// end Parenct Class public class MainClass { // here how can i access "MYEVENT" event } thaks in advance for reply

    C R 2 Replies Last reply
    0
    • N Naveed727

      I want to use the event of other class in my main clas help me out. public class ParenctClass { public delegate void myDel(string data); . public event myDel MYEVENT; public void Some Fucntion { // Thourgh function i put some data in "data" } }// end Parenct Class public class MainClass { // here how can i access "MYEVENT" event } thaks in advance for reply

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Is MainClass derived from ParentClass ? Your event is raised by your class. You can override the method that does this, and/or subscribe to whatever event gets fired to do this. You can also hook up the derived class to recieve the event.

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      N 1 Reply Last reply
      0
      • C Christian Graus

        Is MainClass derived from ParentClass ? Your event is raised by your class. You can override the method that does this, and/or subscribe to whatever event gets fired to do this. You can also hook up the derived class to recieve the event.

        Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        N Offline
        N Offline
        Naveed727
        wrote on last edited by
        #3

        No sir the MainClass in not overrided from Parent class. I got your idea but the probem is that MainClass is i overrideing from "Form" class No the condition is same i have to call the ParenctCass(parent in sence i have to call my Custom Event) and use my ovent event.

        1 Reply Last reply
        0
        • N Naveed727

          I want to use the event of other class in my main clas help me out. public class ParenctClass { public delegate void myDel(string data); . public event myDel MYEVENT; public void Some Fucntion { // Thourgh function i put some data in "data" } }// end Parenct Class public class MainClass { // here how can i access "MYEVENT" event } thaks in advance for reply

          R Offline
          R Offline
          Razvan Dimescu
          wrote on last edited by
          #4

          Declare the event static in your parent class and then subscribe to that event from the main class

          public class Parent
          {
          public delegate void MyDelegate(string str);
          public static event MyDelegate MYEvent;

              public void Myunc()
              {
                  //trigger the event here
                  if (MYEvent != null)
                  {
                      MYEvent("hello listeners");
                  }
              }
          }
          
          public class MainClass
          {
              public MainClass()
              {
                  //subscribe to the event from Parent Class
                  Parent.MYEvent += new Parent.MyDelegate(Parent\_MYEvent);
              }
          
              // called when event is triggered
              public void Parent\_MYEvent(object sender, EventArgs e)
              { 
              
              }
          }
          

          blog

          J N 3 Replies Last reply
          0
          • R Razvan Dimescu

            Declare the event static in your parent class and then subscribe to that event from the main class

            public class Parent
            {
            public delegate void MyDelegate(string str);
            public static event MyDelegate MYEvent;

                public void Myunc()
                {
                    //trigger the event here
                    if (MYEvent != null)
                    {
                        MYEvent("hello listeners");
                    }
                }
            }
            
            public class MainClass
            {
                public MainClass()
                {
                    //subscribe to the event from Parent Class
                    Parent.MYEvent += new Parent.MyDelegate(Parent\_MYEvent);
                }
            
                // called when event is triggered
                public void Parent\_MYEvent(object sender, EventArgs e)
                { 
                
                }
            }
            

            blog

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            good answer but your event handler doesnt match the delegate: // called when event is triggered public void Parent_MYEvent(object sender, EventArgs e) { } should be // called when event is triggered public void Parent_MYEvent(string info) { }

            R 1 Reply Last reply
            0
            • J J4amieC

              good answer but your event handler doesnt match the delegate: // called when event is triggered public void Parent_MYEvent(object sender, EventArgs e) { } should be // called when event is triggered public void Parent_MYEvent(string info) { }

              R Offline
              R Offline
              Razvan Dimescu
              wrote on last edited by
              #6

              Indeed, I wrote the answer with an Eventhandler delegate and after I noticed he needs a custom delegate I changed the code, but forgot to change the method parameters

              blog

              modified on Monday, April 28, 2008 4:56 AM

              1 Reply Last reply
              0
              • R Razvan Dimescu

                Declare the event static in your parent class and then subscribe to that event from the main class

                public class Parent
                {
                public delegate void MyDelegate(string str);
                public static event MyDelegate MYEvent;

                    public void Myunc()
                    {
                        //trigger the event here
                        if (MYEvent != null)
                        {
                            MYEvent("hello listeners");
                        }
                    }
                }
                
                public class MainClass
                {
                    public MainClass()
                    {
                        //subscribe to the event from Parent Class
                        Parent.MYEvent += new Parent.MyDelegate(Parent\_MYEvent);
                    }
                
                    // called when event is triggered
                    public void Parent\_MYEvent(object sender, EventArgs e)
                    { 
                    
                    }
                }
                

                blog

                N Offline
                N Offline
                Naveed727
                wrote on last edited by
                #7

                thanks for replying you got 5

                1 Reply Last reply
                0
                • R Razvan Dimescu

                  Declare the event static in your parent class and then subscribe to that event from the main class

                  public class Parent
                  {
                  public delegate void MyDelegate(string str);
                  public static event MyDelegate MYEvent;

                      public void Myunc()
                      {
                          //trigger the event here
                          if (MYEvent != null)
                          {
                              MYEvent("hello listeners");
                          }
                      }
                  }
                  
                  public class MainClass
                  {
                      public MainClass()
                      {
                          //subscribe to the event from Parent Class
                          Parent.MYEvent += new Parent.MyDelegate(Parent\_MYEvent);
                      }
                  
                      // called when event is triggered
                      public void Parent\_MYEvent(object sender, EventArgs e)
                      { 
                      
                      }
                  }
                  

                  blog

                  N Offline
                  N Offline
                  Naveed727
                  wrote on last edited by
                  #8

                  Your technique is ok when i am using function in Parenct class but if i am useing event in Parent then it fail. i write the code which belew public class Parent : TreeView { public delegate void MyDelegate(string str); public static event MyDelegate MYEvent; protected override void OnBeforeExpand(TreeViewCancelEventArgse) { //trigger the event here if (MYEvent != null) //HOw can we assign value to MYEvent becouse it is allways empty { Parent.MYEvent("hello listeners"); //Null refrence exception occure } } } public class MainClass { public MainClass() { //subscribe to the event from Parent Class Parent.MYEvent += new Parent.MyDelegate(Parent_MYEvent); } // called when event is triggered public void Parent_MYEvent(string str) { MessageBox(str); } }

                  R 1 Reply Last reply
                  0
                  • N Naveed727

                    Your technique is ok when i am using function in Parenct class but if i am useing event in Parent then it fail. i write the code which belew public class Parent : TreeView { public delegate void MyDelegate(string str); public static event MyDelegate MYEvent; protected override void OnBeforeExpand(TreeViewCancelEventArgse) { //trigger the event here if (MYEvent != null) //HOw can we assign value to MYEvent becouse it is allways empty { Parent.MYEvent("hello listeners"); //Null refrence exception occure } } } public class MainClass { public MainClass() { //subscribe to the event from Parent Class Parent.MYEvent += new Parent.MyDelegate(Parent_MYEvent); } // called when event is triggered public void Parent_MYEvent(string str) { MessageBox(str); } }

                    R Offline
                    R Offline
                    Razvan Dimescu
                    wrote on last edited by
                    #9

                    MyEvent is not going to be null if somebody subscribed to this event, like you are doing in the main class

                    public MainClass()
                    {
                    //subscribe to the event from Parent Class
                    Parent.MYEvent += new Parent.MyDelegate(Parent_MYEvent);
                    }

                    that means if your OnBeforeExpand function will be hit before you subscribe to the MyEvent in the main class, this event will be always null.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups