Delegates for overloaded methods?


B

Brian Hampson

I'm new to this delegate thing, and I need to use it to do UI updates
from a backgroundworker thread.

I have a method with overloaded signatures of:

void MyMethod(string MyString);
void MyMethod();

How can I define (a) delegate(s) for this function without having to
resort to:

delegate void MyMethodDelegate1(string MyString);
delegate void MyMethodDelegate2();

Thanks.

Brian Hampson
System Administrator, North America
ALS Group
 
Ad

Advertisements

M

Mattias Sjögren

Brian,
How can I define (a) delegate(s) for this function without having to
resort to:

delegate void MyMethodDelegate1(string MyString);
delegate void MyMethodDelegate2();

What's wrong with this? You need different delegate types for
different signatures.


Mattias
 
D

Dipankar

Hi Brian,

If you are using C# 2.0 you can do so using Anonymous methods.

Create a delegate like this:

public delegate void MyMethodDelegate(string myString);

Create two anonymous methods like this:

MyMethodDelegate delgt1 = delegate(string myString) {

}

MyMethodDelegate delgt2 = delegate {

}

Cheers!!
Dipankar
Bangalore, India
 
B

Brian Hampson

I have since learned a bit more about delegates, and now have them as:

delegate void String1Delegate(string MyString)
delegate void NullDelegate();

since these can be used ANYWHERE I have a method with those signatures.
Thanks.
 
B

Brian Hampson

I'm TOTALLY in the dark regarding "anonymous" methods. What
advantages do they have?
 
D

Dipankar

Hi,

Anonymous methods allow code blocks to be written "in-line" where
delegate values are expected. Anonymous methods are similar to lambda
functions in the Lisp programming language.

Look into this code:

public delegate void MyDelegate(string myString);

public void MyMethod(string myString) { Console.WriteLine(myString);.}

MyDelegate delgt = MyMethod;

Instead using anonymous method we can "in-line" the delegate and the
method block.

public delegate void MyDelegate(string myString);

MyDelegate delgt = delegate (string myString) {
Console.WriteLine(myString);
};

But one feature of anonymous method is that if the delegate declaration
expects some parameters to pass, then also we can assign an anonymous
method without any parameters to that delegate. this holds true if the
Delegate declaration does not expect any out/ref parameters.

For more info visit this URL:
http://msdn2.microsoft.com/en-us/library/0yw3tz5k.aspx

Cheers!!
Dipankar
 
Ad

Advertisements

C

Christof Nordiek

Hi Brian,

you also can use ThreadStart instead of NullDelegate,
in C#2.0 you can use Action<string> instead of String1Delegate

I have since learned a bit more about delegates, and now have them as:

delegate void String1Delegate(string MyString)
delegate void NullDelegate();

since these can be used ANYWHERE I have a method with those signatures.
Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

Even better, in all versions of .NET, you can use MethodInvoker for
NullDelegate.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Christof Nordiek said:
Hi Brian,

you also can use ThreadStart instead of NullDelegate,
in C#2.0 you can use Action<string> instead of String1Delegate

I have since learned a bit more about delegates, and now have them as:

delegate void String1Delegate(string MyString)
delegate void NullDelegate();

since these can be used ANYWHERE I have a method with those signatures.
Thanks.
 
Ad

Advertisements

B

Ben Voigt

Brian Hampson said:
I'm new to this delegate thing, and I need to use it to do UI updates
from a backgroundworker thread.

I have a method with overloaded signatures of:

void MyMethod(string MyString);
void MyMethod();

How can I define (a) delegate(s) for this function without having to
resort to:

delegate void MyMethodDelegate1(string MyString);
delegate void MyMethodDelegate2();

If you are trying to pass a method group as a delegate, then you could use
an interface instead. That would require that a set of methods with
particular parameter sets exist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top