Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Raising static events in other classes? 1

Status
Not open for further replies.

haddaway

Programmer
Joined
Jul 6, 2005
Messages
172
Location
SE
I have a static event in another class. I can not reach that event. I have to make a function in that remote class and call the event from there.

Is this the best and only way?
 
i may not understand what it is you mean, but it looks like you're confusing a class and an instance of a class.

a method belongs to an instance of a class, a static method belongs to the class itself.

static means that you only get one for the class itself across the application. if it's not static you get one per instance.

an example:

Code:
class TheClass
{
 void Method();
 static void StaticMethod();
}
...
int Main()
{
 // create instance of class and then
 // call a method of that class
 TheClass theInstance = new TheClass();
 theInstance.Method();

 // to call a static method of the class
 // use the class itself
 TheClass.StaticMethod();
}

hope this helps,


mr s. <;)

 
You can call static methods without creating an instance. My question is if I can call a static event from another class?

Example:



public class A
{
public delegate void GUIDelegate(GUIEventType ge, object objMessage);
public static event GUIDelegate GUIEvent;
}

public class B
{
public static void DoSomething() {
A.GUIDelegate(GUIEventType.Error, null);

}
}
 
you fire an event from inside its class.

one way to do this from outside is via a method.

Code:
using System;

namespace TestApp
{
 public enum GUIEventType
 {
  None = 0,
  Error
 }

 public delegate void GUIDelegate(GUIEventType ge, object objMessage);

 public class A
 {
  public static event GUIDelegate GUIEvent;
  public static void FireEvent(GUIEventType ge)
  {
   GUIEvent(ge, null);
  }
 }

 public class B
 {
  public static void theDelegate(GUIEventType ge, object objMessage)
  {
   Console.WriteLine("event fired");
  }

  [STAThread]
  static void Main(string[] args)
  {
   A.GUIEvent += new GUIDelegate(theDelegate);
   A.FireEvent(GUIEventType.None);
  }
 }
}



mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top