473,385 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

CreateDelegate in VS.NET 2003 - error on binding method

Hi,

I'm trying to delegate all events of a command button to a single
method, handlesAll. I wrote the following code in VS.NET 2005, and it
worked fine:

public void setUpDelegates()
{

MethodInfo mi = typeof(MainForm).GetMethod("handlesAll",
BindingFlags.Public | BindingFlags.Instance);

foreach (System.Reflection.EventInfo h in
typeof(Button).GetEvents())

{
h.AddEventHandler(button1,
System.Delegate.CreateDelegate(h.EventHandlerType, this, mi));

}
}

public void handlesAll(object sender, EventArgs e)

{
Console.WriteLine("Received event: " +
e.ToString());
}

As I said, this works fine in VS.NET 2005. However, in VS.NET 2003,
the overloaded version of CreateDelegate(Type, object, MethodInfo)
doesn't exist - I need to use CreateDelegate(Type, object, string), so
I tried doing

h.AddEventHandler(button1,
System.Delegate.CreateDelegate(h.EventHandlerType, this, mi.Name));

inside the loop. However, it now gives me the error message:

"An unhandled exception of type 'System.NullReferenceException'
occurred in TestGridEx.exe
Additional information: Object reference not set to an instance of an
object."

I've tried directly calling mi.Invoke() and it finds the method fine -
what am I doing wrong?

Nov 23 '06 #1
1 1629
Sorry, posted wrong error message, here is the message I get:

"An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Error binding to target method."

Can anyone help?

snibril wrote:
Hi,

I'm trying to delegate all events of a command button to a single
method, handlesAll. I wrote the following code in VS.NET 2005, and it
worked fine:

public void setUpDelegates()
{

MethodInfo mi = typeof(MainForm).GetMethod("handlesAll",
BindingFlags.Public | BindingFlags.Instance);

foreach (System.Reflection.EventInfo h in
typeof(Button).GetEvents())

{
h.AddEventHandler(button1,
System.Delegate.CreateDelegate(h.EventHandlerType, this, mi));

}
}

public void handlesAll(object sender, EventArgs e)

{
Console.WriteLine("Received event: " +
e.ToString());
}

As I said, this works fine in VS.NET 2005. However, in VS.NET 2003,
the overloaded version of CreateDelegate(Type, object, MethodInfo)
doesn't exist - I need to use CreateDelegate(Type, object, string), so
I tried doing

h.AddEventHandler(button1,
System.Delegate.CreateDelegate(h.EventHandlerType, this, mi.Name));

inside the loop. However, it now gives me the error message:

"An unhandled exception of type 'System.NullReferenceException'
occurred in TestGridEx.exe
Additional information: Object reference not set to an instance of an
object."

I've tried directly calling mi.Invoke() and it finds the method fine -
what am I doing wrong?
Nov 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nivek Eroom | last post by:
Can anyone tells me how can i add a delegate to an object remotly created as this : object patate = Activator.GetObject(tt,"http://localhost:8080/DynamicTemplateServer.soap"); ...
1
by: redhotsly | last post by:
Hi, Is is possible to create an event handler method that can handle any type of event no matter what the delegate signature is. Here is the code I have so far: public class class1 {...
7
by: Max | last post by:
I'm using late binding to automate to Outlook and I'm getting an ArgumentException when I create a delegate. The arguments I pass seem valid. This is my code: Type oType =...
0
by: JSantora | last post by:
Essentially, InsertAT is broken! For the past couple of hours, I've been getting this "Parameter name: '-2147483550' is not a valid value for 'index'." error. Apparently, its caused by having...
0
by: maflatoun | last post by:
Hi everyone, I have the following very odd problem (only happens on Windows 2003 Server and not XP) In my class I have (no namespace) class test { ... public static void method...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.