PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Custom events (C#)

 
 
Torbjorn Stavas
Guest
Posts: n/a
 
      3rd Sep 2004
I've been trying to use a custom event in one of of my classes but i get a
MethodMissingException when calling it. Been looking at
Http://www.ondotnet.com/pub/a/dotnet...15/events.html,
http://msdn.microsoft.com/library/de.../cpguide/html/
cpconprovidingeventfunctionality.asp, and
http://msdn.microsoft.com/library/de.../cpguide/html/
cpconeventsdelegates.asp.

I'll show some of the code in my classes;
namespace Rapportering
{
public class TestEventArgs: EventArgs
{
public TestEventArgs()
{
}
}

public delegate void TestEventHandler(Object sender, TestEventArgs e);

public class PanelKeyBNum : System.Windows.Forms.Panel
{
public event TestEventHandler TestEvent;
protected virtual void OnTestEvent(TestEventArgs e)
{
//This call generates a MissingMethodException
TestEvent(this, e);
}

//
// Program logic..
//

private void btnClose_Click(object sender, EventArgs e)
{
OnTestEvent(new TestEventArgs());
}
}
}

Is there anyone that knows why this generates a MissingMethodException? None the examples of
creating custom events in c# has been for CF though, so i suspect that there's something i have missed.

The class PanelKeyBNum displays a panel that lets the user write numbers to some textboxes on a form. I want
to know when the user "closes" (i.e visible=false, since it's a panel) PanelKeyBNum, so that the program knows
that it's time to save the input to the textboxes to some variables in another class.

Any suggestions on why this doesn't work is really appreciated.

//Torbjorn
 
Reply With Quote
 
 
 
 
Torbjorn Stavas
Guest
Posts: n/a
 
      6th Sep 2004
Mark Arteaga <MVP> <ng_[at]_markarteaga_[dot]_com> wrote in message news:<41E31755-9883-40C8-A794-(E-Mail Removed)>...
> Try adding a check for null in OnTestEvent
>
> protected virtual void OnTestEvent(TestEventArgs e)
> {
> //This call generates a MissingMethodException
> if(TestEvent!=null)
> TestEvent(this, e);
> }
>


Ooops, it was null. What does this mean? I have followed the "custom
events" examples, and all the code that is vital (i presume) to the
custom event in my program is listed in my original post. Anyone got
an idea?

//torbjorn



>
> "Torbjorn Stavas" wrote:
>
> > I've been trying to use a custom event in one of of my classes but i get a
> > MethodMissingException when calling it. Been looking at
> > Http://www.ondotnet.com/pub/a/dotnet...15/events.html,
> > http://msdn.microsoft.com/library/de.../cpguide/html/
> > cpconprovidingeventfunctionality.asp, and
> > http://msdn.microsoft.com/library/de.../cpguide/html/
> > cpconeventsdelegates.asp.
> >
> > I'll show some of the code in my classes;
> > namespace Rapportering
> > {
> > public class TestEventArgs: EventArgs
> > {
> > public TestEventArgs()
> > {
> > }
> > }
> >
> > public delegate void TestEventHandler(Object sender, TestEventArgs e);
> >
> > public class PanelKeyBNum : System.Windows.Forms.Panel
> > {
> > public event TestEventHandler TestEvent;
> > protected virtual void OnTestEvent(TestEventArgs e)
> > {
> > //This call generates a MissingMethodException
> > TestEvent(this, e);
> > }
> >
> > //
> > // Program logic..
> > //
> >
> > private void btnClose_Click(object sender, EventArgs e)
> > {
> > OnTestEvent(new TestEventArgs());
> > }
> > }
> > }
> >
> > Is there anyone that knows why this generates a MissingMethodException? None the examples of
> > creating custom events in c# has been for CF though, so i suspect that there's something i have missed.
> >
> > The class PanelKeyBNum displays a panel that lets the user write numbers to some textboxes on a form. I want
> > to know when the user "closes" (i.e visible=false, since it's a panel) PanelKeyBNum, so that the program knows
> > that it's time to save the input to the textboxes to some variables in another class.
> >
> > Any suggestions on why this doesn't work is really appreciated.
> >
> > //Torbjorn
> >

 
Reply With Quote
 
Torbjorn Stavas
Guest
Posts: n/a
 
      6th Sep 2004
Mark Arteaga <MVP> <ng_[at]_markarteaga_[dot]_com> wrote in message news:<41E31755-9883-40C8-A794-(E-Mail Removed)>...
> Try adding a check for null in OnTestEvent
>
> protected virtual void OnTestEvent(TestEventArgs e)
> {
> //This call generates a MissingMethodException
> if(TestEvent!=null)
> TestEvent(this, e);
> }
>


Sorry, my fault. I didn't understand that TestEvent is null when no
one is subscribing to the event. The code works like a charm now.

//Many thanks, Torbjorn



>
> "Torbjorn Stavas" wrote:
>
> > I've been trying to use a custom event in one of of my classes but i get a
> > MethodMissingException when calling it. Been looking at
> > Http://www.ondotnet.com/pub/a/dotnet...15/events.html,
> > http://msdn.microsoft.com/library/de.../cpguide/html/
> > cpconprovidingeventfunctionality.asp, and
> > http://msdn.microsoft.com/library/de.../cpguide/html/
> > cpconeventsdelegates.asp.
> >
> > I'll show some of the code in my classes;
> > namespace Rapportering
> > {
> > public class TestEventArgs: EventArgs
> > {
> > public TestEventArgs()
> > {
> > }
> > }
> >
> > public delegate void TestEventHandler(Object sender, TestEventArgs e);
> >
> > public class PanelKeyBNum : System.Windows.Forms.Panel
> > {
> > public event TestEventHandler TestEvent;
> > protected virtual void OnTestEvent(TestEventArgs e)
> > {
> > //This call generates a MissingMethodException
> > TestEvent(this, e);
> > }
> >
> > //
> > // Program logic..
> > //
> >
> > private void btnClose_Click(object sender, EventArgs e)
> > {
> > OnTestEvent(new TestEventArgs());
> > }
> > }
> > }
> >
> > Is there anyone that knows why this generates a MissingMethodException? None the examples of
> > creating custom events in c# has been for CF though, so i suspect that there's something i have missed.
> >
> > The class PanelKeyBNum displays a panel that lets the user write numbers to some textboxes on a form. I want
> > to know when the user "closes" (i.e visible=false, since it's a panel) PanelKeyBNum, so that the program knows
> > that it's time to save the input to the textboxes to some variables in another class.
> >
> > Any suggestions on why this doesn't work is really appreciated.
> >
> > //Torbjorn
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
custom events in custom controls not appear in properties window Peted Microsoft C# .NET 0 25th May 2009 09:09 AM
custom events in custom controls not appear in properties window Peted Microsoft C# .NET 0 25th May 2009 09:09 AM
.net Custom Web Events Ryan Microsoft ASP .NET 5 7th Mar 2007 06:52 PM
Custom Events versus Multicast Delegates for events(VB.NET) =?Utf-8?B?aGVyYmVydA==?= Microsoft Dot NET 2 14th Feb 2007 02:51 PM
Custom Events Theresa Smallwood Microsoft Outlook Interoperability 0 9th May 2006 04:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:14 PM.