How can i access event of other class
-
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
-
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
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 )
-
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 )
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.
-
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
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) { } }
-
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) { } }
-
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) { }
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
modified on Monday, April 28, 2008 4:56 AM
-
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) { } }
-
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) { } }
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); } }
-
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); } }
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.