PC Review


Reply
Thread Tools Rate Thread

Delegates for overloaded methods?

 
 
Brian Hampson
Guest
Posts: n/a
 
      8th Sep 2006
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

 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      8th Sep 2006
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Dipankar
Guest
Posts: n/a
 
      8th Sep 2006
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

Brian Hampson wrote:
> 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


 
Reply With Quote
 
Brian Hampson
Guest
Posts: n/a
 
      8th Sep 2006
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.

Mattias Sjögren wrote:
> 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
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.


 
Reply With Quote
 
Brian Hampson
Guest
Posts: n/a
 
      8th Sep 2006
I'm TOTALLY in the dark regarding "anonymous" methods. What
advantages do they have?

Dipankar wrote:
> 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
>
> Brian Hampson wrote:
> > 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


 
Reply With Quote
 
Dipankar
Guest
Posts: n/a
 
      8th Sep 2006
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

Brian Hampson wrote:
> I'm TOTALLY in the dark regarding "anonymous" methods. What
> advantages do they have?
>
> Dipankar wrote:
> > 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
> >
> > Brian Hampson wrote:
> > > 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


 
Reply With Quote
 
Christof Nordiek
Guest
Posts: n/a
 
      8th Sep 2006
Hi Brian,

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

"Brian Hampson" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
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.

Mattias Sjögren wrote:
> 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
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      8th Sep 2006
Even better, in all versions of .NET, you can use MethodInvoker for
NullDelegate.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Christof Nordiek" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Brian,
>
> you also can use ThreadStart instead of NullDelegate,
> in C#2.0 you can use Action<string> instead of String1Delegate
>
> "Brian Hampson" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
> 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.
>
> Mattias Sjögren wrote:
>> 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
>>
>> --
>> Mattias Sjögren [C# MVP] mattias @ mvps.org
>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>> Please reply only to the newsgroup.

>
>



 
Reply With Quote
 
Ben Voigt
Guest
Posts: n/a
 
      9th Sep 2006

"Brian Hampson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

>
> Thanks.
>
> Brian Hampson
> System Administrator, North America
> ALS Group
>



 
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
XML Documentation in .NET 2.0 and <see...> links to overloaded methods =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= Microsoft C# .NET 1 2nd Dec 2005 09:16 AM
Overloaded methods =?Utf-8?B?cG1jZ3VpcmU=?= Microsoft VB .NET 4 19th May 2005 03:07 PM
Can web methods be overloaded ? john bailo Microsoft C# .NET 10 27th Nov 2003 12:17 AM
Can web methods be overloaded ? john bailo Microsoft Dot NET Framework 10 27th Nov 2003 12:17 AM
Accessing C# Overloaded methods from VB6 Arijit Chakraborti Microsoft Dot NET Framework 2 18th Sep 2003 10:30 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:08 AM.