Click here to Skip to main content
15,899,026 members
Home / Discussions / C#
   

C#

 
GeneralRe: How can I limit my program to running just one instance. Pin
mcgahanfl13-Nov-03 8:05
mcgahanfl13-Nov-03 8:05 
GeneralC# seting valuse in another form Pin
Anonymous11-Nov-03 8:55
Anonymous11-Nov-03 8:55 
GeneralRe: C# seting valuse in another form Pin
Hesham Amin11-Nov-03 9:43
Hesham Amin11-Nov-03 9:43 
GeneralRe: C# seting valuse in another form Pin
Member 44190911-Nov-03 10:50
Member 44190911-Nov-03 10:50 
GeneralRe: C# seting valuse in another form Pin
Hesham Amin12-Nov-03 0:33
Hesham Amin12-Nov-03 0:33 
GeneralRe: C# seting valuse in another form Pin
WuRunZhe17-Nov-13 21:31
WuRunZhe17-Nov-13 21:31 
GeneralProblem firing event and passing "this" Pin
andrewstan11-Nov-03 7:04
andrewstan11-Nov-03 7:04 
GeneralRe: Problem firing event and passing "this" Pin
Heath Stewart11-Nov-03 7:14
protectorHeath Stewart11-Nov-03 7:14 
You can't fire an event in the constructor. Here's why (and the answer to your problem). Events in .NET are based on the listener principal. Each even has a collection of listeners associated with it (by default). If there are no listeners, the event list is null. That's why you're getting the NullReference exception. You can't do this in your constructor because until your constructor finishes and an instance is created, callers must wait before attaching an event handler to the event. If you want to fire an event while your object is being created, then you should use static events which work the same way:
public class MyClass
{
  public static event EventHandler Created;
  public MyClass()
  {
    // Initialize.
    // Fire static event.
    if (Created != null) Created(this, EventArgs.Empty);
  }
}
See how I checked if the event was null (i.e., the event handler collection is empty)? There is a more common practice for this, especially with instance events. The .NET BCL (base class library) does it throughout and it's a good idea to do in your code, that way derived classes don't have to listen to the event which is a little slower. You use a protected virtual method that raises the event and simply call that:
public class MyClass
{
  public MyClass()
  {
  }
  public event EventHandler MyEvent;
  protected virtual void OnMyEvent(EventArgs e)
  {
    if (MyEvent != null) MyEvent(this, e);
  }
}
public class MyClass2 : MyClass
{
  public MyClass2()
  {
  }
  protected override void OnMyEvent(EventArgs e)
  {
    // The EventArgs (or derived class) is passed so
    // that derived classes can check specific states.
    if (e != null) DoSomething();
    // Call the base class so that listeners can handle the event.
    base.OnMyEvent(e);
  }
}


 

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
GeneralRe: Problem firing event and passing "this" Pin
andrewstan11-Nov-03 23:20
andrewstan11-Nov-03 23:20 
GeneralRe: Problem firing event and passing "this" Pin
Heath Stewart12-Nov-03 2:53
protectorHeath Stewart12-Nov-03 2:53 
GeneralRe: Problem firing event and passing "this" Pin
andrewstan12-Nov-03 3:13
andrewstan12-Nov-03 3:13 
Generaldrag window wothout titlebar Pin
troels_sorensen11-Nov-03 6:21
troels_sorensen11-Nov-03 6:21 
GeneralRe: drag window wothout titlebar Pin
Amirjalaly11-Nov-03 7:04
Amirjalaly11-Nov-03 7:04 
GeneralRe: drag window wothout titlebar Pin
troels_sorensen11-Nov-03 22:20
troels_sorensen11-Nov-03 22:20 
GeneralRe: drag window wothout titlebar Pin
Alvaro Mendez11-Nov-03 7:45
Alvaro Mendez11-Nov-03 7:45 
GeneralRe: drag window wothout titlebar Pin
Judah Gabriel Himango11-Nov-03 10:27
sponsorJudah Gabriel Himango11-Nov-03 10:27 
GeneralReading a used file Pin
j-hannemann11-Nov-03 6:16
j-hannemann11-Nov-03 6:16 
GeneralRe: Reading a used file Pin
Eric Gunnerson (msft)11-Nov-03 13:54
Eric Gunnerson (msft)11-Nov-03 13:54 
GeneralRe: Reading a used file Pin
j-hannemann11-Nov-03 23:58
j-hannemann11-Nov-03 23:58 
GeneralRe: Reading a used file Pin
Eric Gunnerson (msft)12-Nov-03 11:36
Eric Gunnerson (msft)12-Nov-03 11:36 
GeneralRe: Reading a used file Pin
j-hannemann12-Nov-03 21:16
j-hannemann12-Nov-03 21:16 
Generalproblems when copying an wmi object Pin
wfettich11-Nov-03 5:20
wfettich11-Nov-03 5:20 
GeneralNeed no setup... Pin
Amirjalaly11-Nov-03 4:56
Amirjalaly11-Nov-03 4:56 
GeneralRe: Need no setup... Pin
Judah Gabriel Himango11-Nov-03 5:11
sponsorJudah Gabriel Himango11-Nov-03 5:11 
GeneralRe: Need no setup... Pin
Heath Stewart11-Nov-03 7:06
protectorHeath Stewart11-Nov-03 7:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.