473,416 Members | 1,721 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,416 software developers and data experts.

Generic delegate implementation question

I've got these declarations:
public delegate void FormDisplayResultsDelegate<Type>(Type
displayResultsValue);
public FormDisplayResultsDelegate<stringdisplayMsgDelegat e;

instantiation:
displayMsgDelegate = DisplayStatusMessage;

implementation:
public void DisplayStatusMessage(string statusMessage)
{
StatusTextBox.Text = statusMessage;
}

however, the following seems more proper to me and also works:
public void DisplayStatusMessage<Type>(Type statusMessage)
{
StatusTextBox.Text = statusMessage.ToString();
}

can anyone explain what's happening under the covers please?
(implicit casting i guess?...)

thanks, dave

Oct 5 '07 #1
7 2027
Dave wrote:
I've got these declarations:
public delegate void FormDisplayResultsDelegate<Type>(Type
displayResultsValue);
public FormDisplayResultsDelegate<stringdisplayMsgDelegat e;

instantiation:
displayMsgDelegate = DisplayStatusMessage;

implementation:
public void DisplayStatusMessage(string statusMessage)
{
StatusTextBox.Text = statusMessage;
}

however, the following seems more proper to me and also works:
public void DisplayStatusMessage<Type>(Type statusMessage)
{
StatusTextBox.Text = statusMessage.ToString();
}

can anyone explain what's happening under the covers please?
(implicit casting i guess?...)
Can you post the code in which the second method declaration "works"?
How are you using the method? As an alternative to declaring the
delegate at all? Or are you somehow using an instance of the generic
method to initialize the delegate variable?

I can see why it would work in some cases but if you, for example, used
"int" instead of "string" as the Type parameter for the generic method,
it wouldn't. That is, you wouldn't be able to assign that concrete
instance of the generic method to the concrete "string"-based delegate type.

I'm not sure why the generic method seems more proper to you, but the
basic reason it works is that the method signature that results matches
the signature required by the delegate. A generic method can only be
used with a concrete instance of that method, and once you create that
concrete instance, it's just like any other method, with a signature
that looks just like any other method.

So as long as you create a concrete instance of your generic method that
has the same signature required by the delegate, you can use it.

However, that's not to say that's necessarily a useful way to use a
generic method. A generic method has the same basic purpose as generic
types; that is, it allows you to declare a method that can be used with
a wide variety of types without knowing the type in advance. However,
in your example, there's not necessarily anything about your method that
takes advantage of being generic.

Without knowing exactly what you're trying to do, it's hard to say which
is "better". If you really want a generic method to go along with the
generic delegate type, then I'd say the generic method is better. But
from only the code you posted, it's not really obvious why generics are
being used at all. And if generics are desirable, it may be that you
have a design where you need a specific implementation for each delegate
concrete instance and so making a new generic method for each different
type wouldn't make sense.

In other words, all possibilities are valid designs. But they
definitely don't do the same thing, so which one is best for your
purposes really depends on what you are trying to do.

Pete
Oct 5 '07 #2
On Oct 5, 12:36 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
Dave wrote:
I've got these declarations:
public delegate void FormDisplayResultsDelegate<Type>(Type
displayResultsValue);
public FormDisplayResultsDelegate<stringdisplayMsgDelegat e;
instantiation:
displayMsgDelegate = DisplayStatusMessage;
implementation:
public void DisplayStatusMessage(string statusMessage)
{
StatusTextBox.Text = statusMessage;
}
however, the following seems more proper to me and also works:
public void DisplayStatusMessage<Type>(Type statusMessage)
{
StatusTextBox.Text = statusMessage.ToString();
}
can anyone explain what's happening under the covers please?
(implicit casting i guess?...)

Can you post the code in which the second method declaration "works"?
How are you using the method? As an alternative to declaring the
delegate at all? Or are you somehow using an instance of the generic
method to initialize the delegate variable?

I can see why it would work in some cases but if you, for example, used
"int" instead of "string" as the Type parameter for the generic method,
it wouldn't. That is, you wouldn't be able to assign that concrete
instance of the generic method to the concrete "string"-based delegate type.

I'm not sure why the generic method seems more proper to you, but the
basic reason it works is that the method signature that results matches
the signature required by the delegate. A generic method can only be
used with a concrete instance of that method, and once you create that
concrete instance, it's just like any other method, with a signature
that looks just like any other method.

So as long as you create a concrete instance of your generic method that
has the same signature required by the delegate, you can use it.

However, that's not to say that's necessarily a useful way to use a
generic method. A generic method has the same basic purpose as generic
types; that is, it allows you to declare a method that can be used with
a wide variety of types without knowing the type in advance. However,
in your example, there's not necessarily anything about your method that
takes advantage of being generic.

Without knowing exactly what you're trying to do, it's hard to say which
is "better". If you really want a generic method to go along with the
generic delegate type, then I'd say the generic method is better. But
from only the code you posted, it's not really obvious why generics are
being used at all. And if generics are desirable, it may be that you
have a design where you need a specific implementation for each delegate
concrete instance and so making a new generic method for each different
type wouldn't make sense.

In other words, all possibilities are valid designs. But they
definitely don't do the same thing, so which one is best for your
purposes really depends on what you are trying to do.

Pete- Hide quoted text -

- Show quoted text -
Here is more code showing more context. I'm using a generic delegate
declaration allowing for reuse / single declaration, as shown below..

So again I'm simply wondering why the non generic method
implementation (ProgressBarSetup()) works seemlessly? I assume there
is implicit casting going on but ...?

public delegate void FormDisplayResultsDelegate<Type>(Type
displayResultsValue); // generic single value delegate
public FormDisplayResultsDelegate<stringdisplayMsgDelegat e;
public FormDisplayResultsDelegate<intprogressBarSetupMsgD elegate;
public FormDisplayResultsDelegate<intconcurrentRequestDel egate;
public FormDisplayResultsDelegate<intoutstandingRequestDe legate;
....

progressBarSetupMsgDelegate = ProgressBarSetup;
....

public void ProgressBarSetup(int outstandingConnAttemptsArg)
{
if (RequestAttemptProgressBar.InvokeRequired)
{

RequestAttemptProgressBar.BeginInvoke(progressBarS etupMsgDelegate, new
object[] { outstandingConnAttemptsArg });
}
else
{
RequestAttemptProgressBar.Refresh();
RequestAttemptProgressBar.Minimum = 1;
RequestAttemptProgressBar.Value = 1;
RequestAttemptProgressBar.Step = 1;
if (outstandingConnAttemptsArg 0)
{
RequestAttemptProgressBar.Maximum =
outstandingConnAttemptsArg;
}
else
{
RequestAttemptProgressBar.Maximum = 2;
}
RequestAttemptProgressBar.Visible = true;
}
}

Oct 5 '07 #3
Dave wrote:
[...]
Here is more code showing more context. I'm using a generic delegate
declaration allowing for reuse / single declaration, as shown below..
Sorry, but yhe code you posted fails to meet both the
"concise-but-complete" standard that is necessary for useful code
samples, as well as doesn't really provide (at least for me) insight as
to why a generic method would be desirable in your case.

Heck, the code you posted doesn't even use a generic method.
So again I'm simply wondering why the non generic method
implementation (ProgressBarSetup()) works seemlessly? I assume there
is implicit casting going on but ...?
Since you didn't post any code using the generic method, it's hard to
answer the question of what's going on in your case. However, I doubt
there's any implicit casting going on.

If you are using the generic method successfully, it is most likely
because the resulting method signature is exactly the signature required
by the delegate to which you assign the method instance. No casting
would be required, as the types would already match.

Pete
Oct 5 '07 #4
On Oct 5, 1:43 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
Dave wrote:
[...]
Here is more code showing more context. I'm using a generic delegate
declaration allowing for reuse / single declaration, as shown below..

Sorry, but yhe code you posted fails to meet both the
"concise-but-complete" standard that is necessary for useful code
samples, as well as doesn't really provide (at least for me) insight as
to why a generic method would be desirable in your case.

Heck, the code you posted doesn't even use a generic method.
So again I'm simply wondering why the non generic method
implementation (ProgressBarSetup()) works seemlessly? I assume there
is implicit casting going on but ...?

Since you didn't post any code using the generic method, it's hard to
answer the question of what's going on in your case. However, I doubt
there's any implicit casting going on.

If you are using the generic method successfully, it is most likely
because the resulting method signature is exactly the signature required
by the delegate to which you assign the method instance. No casting
would be required, as the types would already match.

Pete
I'm not looking to use a generic method, only wondering why the non
generic method works / compiles seemlessly. i'm fine using the non-
generic method.

I only wanted the generic declaration to avoid multiple / similar non-
generic delegate declarations.

i did not include the call as it didn't lend much to the discussion
but here it is (as called from another thread / object):
parentObj.ParentFormObj.ProgressBarSetup(0);

thanks for your efforts, no further info needed, dave

Oct 5 '07 #5
Dave <dj*********@gmail.comwrote:
I've got these declarations:
public delegate void FormDisplayResultsDelegate<Type>(Type
displayResultsValue);
public FormDisplayResultsDelegate<stringdisplayMsgDelegat e;

instantiation:
displayMsgDelegate = DisplayStatusMessage;

implementation:
public void DisplayStatusMessage(string statusMessage)
{
StatusTextBox.Text = statusMessage;
}

however, the following seems more proper to me and also works:
public void DisplayStatusMessage<Type>(Type statusMessage)
{
StatusTextBox.Text = statusMessage.ToString();
}

can anyone explain what's happening under the covers please?
(implicit casting i guess?...)
Eek - I would *strongly* advise against using an existing type name
("Type") as the name of a generic parameter. Let's rewrite that as:

public delegate void FormDisplayResultsDelegate<T>
(T displayResultsValue)

That's a lot easier to understand - no confusion with
System.Reflection.Type.

Now, as for why you can do:

displayMsgDelegate = DisplayStatusMessage;

with both the non-generic and generic versions - it's type inferencing
and an implicit conversion from method-group to a particular delegate
type. Method groups containing a method with a signature of

void Foo(string x)
and
void Foo<T>(T x)

can both be implicitly converted to FormDisplayResultsDelegate<string>.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 5 '07 #6
On Oct 5, 3:33 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Dave <djohanns...@gmail.comwrote:
I've got these declarations:
public delegate void FormDisplayResultsDelegate<Type>(Type
displayResultsValue);
public FormDisplayResultsDelegate<stringdisplayMsgDelegat e;
instantiation:
displayMsgDelegate = DisplayStatusMessage;
implementation:
public void DisplayStatusMessage(string statusMessage)
{
StatusTextBox.Text = statusMessage;
}
however, the following seems more proper to me and also works:
public void DisplayStatusMessage<Type>(Type statusMessage)
{
StatusTextBox.Text = statusMessage.ToString();
}
can anyone explain what's happening under the covers please?
(implicit casting i guess?...)

Eek - I would *strongly* advise against using an existing type name
("Type") as the name of a generic parameter. Let's rewrite that as:

public delegate void FormDisplayResultsDelegate<T>
(T displayResultsValue)

That's a lot easier to understand - no confusion with
System.Reflection.Type.

Now, as for why you can do:

displayMsgDelegate = DisplayStatusMessage;

with both the non-generic and generic versions - it's type inferencing
and an implicit conversion from method-group to a particular delegate
type. Method groups containing a method with a signature of

void Foo(string x)
and
void Foo<T>(T x)

can both be implicitly converted to FormDisplayResultsDelegate<string>.

--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too- Hide quoted text -

- Show quoted text -
oh good that's what i sorta thought - great to get more precisely
explained!

thanks, dave

Oct 5 '07 #7
Dave wrote:
I'm not looking to use a generic method, only wondering why the non
generic method works / compiles seemlessly. i'm fine using the non-
generic method.
Ah. Well, that was not at all clear from your post. If anything it
seemed more like you were wondering why the generic method works.

The answer is still basically the same though. The non-generic method
works because its signature matches the concrete delegate signature
exactly. That is, once you apply a specific type to the generic
delegate type, you get a signature that's the same as the non-generic
method.

There's no casting involved. You are dealing with types that already match.

Pete
Oct 5 '07 #8

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

Similar topics

3
by: Michael Rockwell | last post by:
I am new to using C# generics and I am liking what I am finding. However the examples in online help are lacking. Can someone help me with the FindAll method of the generic List class? As I...
3
by: Aquila Deus | last post by:
Hi all! I found a problem when using generic with delegate: delegate RT MethodTemplate <RT> (); delegate RT MethodTemplate <RT, AT0> (AT0 a0); delegate RT...
22
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a...
6
by: David Veeneman | last post by:
I have several events that pass a value in their event args. One event passes an int, another a string, another a DateTime, and so on. Rather than creating a separate set of event args for each...
8
by: Ympostor | last post by:
Why can't I do this (it gives a syntactic error):? foreach (System.Reflection.PropertyInfo oProp in oObject.GetType().GetProperties()) { if ((oProp.PropertyType.IsGenericType) &&...
2
by: jehugaleahsa | last post by:
Hello: I was reading another post and saw an argument (sort of) that brought up a good question. I have written a fairly large library of algorithms that occur in programming all the time. It...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
7
by: raylopez99 | last post by:
On Aug 16, 3:49 pm, Marc Gravell <marc.grav...@gmail.comwrote: If you cannot understand such a simple post, then you don't understand generic delegate types. Apparently you can only read your...
8
by: Advait Mohan Raut | last post by:
Hello, I am using VC# 2005 ; C# 2.0 I am working on the performance measurement tool. For that I need my code to call user defined method along with its parameters. For that should I use a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.