473,508 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics - A question on generics - delegates - runtime binding.

I would like to do the following and cannot figure it out.

public class A<T>
{
public delegate T GetValueDelegate(A<T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}

public class B
{
public static int GetValue(A<int> var)
{
//do something and return int
}

public static float GetValue(A<float> var)
{
//do something and return float
}

public static string GetValue(A<string> var)
{
//do something and return string
}

//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
}

Now:-----------------------------------
I was thinking when I do

....somewhere in some code....
A<int> s = new A<int>();

that on creation of this instance of A of type int that the delegate
in A would find the proper overload in class B such that:
....
int I = s.GetValue();
....
would resolve to the class B's method
....
public static int GetValue(A<int> var)
....
and this would occur dynamically at runtime.
The short is that I get a 'No Overload Error' as follows.
'No overload for 'GetValue' matches delegate 'A<T>.GetValueDelegate'

I am obviously not understanding something. Help would be greatly appriciated.

k
--
A single 20-watt compact fluorescent lamp used in place of a 75-watt
incandescent will save about 550 kilowatt-hours over its lifetime. That is
500 pounds of coal; a pile the size of your office desk.
Nov 17 '05 #1
16 1802
You have not defined a overload for GetValue in class B that takes no
parameters. But, you are calling it in class A<T>.

"bigtexan" <bi******@online.nospam> wrote in message
news:37**********************************@microsof t.com...
I would like to do the following and cannot figure it out.

public class A<T>
{
public delegate T GetValueDelegate(A<T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}

public class B
{
public static int GetValue(A<int> var)
{
//do something and return int
}

public static float GetValue(A<float> var)
{
//do something and return float
}

public static string GetValue(A<string> var)
{
//do something and return string
}

//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
}

Now:-----------------------------------
I was thinking when I do

....somewhere in some code....
A<int> s = new A<int>();

that on creation of this instance of A of type int that the delegate
in A would find the proper overload in class B such that:
....
int I = s.GetValue();
....
would resolve to the class B's method
....
public static int GetValue(A<int> var)
....
and this would occur dynamically at runtime.
The short is that I get a 'No Overload Error' as follows.
'No overload for 'GetValue' matches delegate 'A<T>.GetValueDelegate'

I am obviously not understanding something. Help would be greatly
appriciated.

k
--
A single 20-watt compact fluorescent lamp used in place of a 75-watt
incandescent will save about 550 kilowatt-hours over its lifetime. That is
500 pounds of coal; a pile the size of your office desk.
Nov 17 '05 #2
You have not defined a overload for GetValue in class B that takes no
parameters. But, you are calling it in class A<T>.

"bigtexan" <bi******@online.nospam> wrote in message
news:37**********************************@microsof t.com...
I would like to do the following and cannot figure it out.

public class A<T>
{
public delegate T GetValueDelegate(A<T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}

public class B
{
public static int GetValue(A<int> var)
{
//do something and return int
}

public static float GetValue(A<float> var)
{
//do something and return float
}

public static string GetValue(A<string> var)
{
//do something and return string
}

//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
}

Now:-----------------------------------
I was thinking when I do

....somewhere in some code....
A<int> s = new A<int>();

that on creation of this instance of A of type int that the delegate
in A would find the proper overload in class B such that:
....
int I = s.GetValue();
....
would resolve to the class B's method
....
public static int GetValue(A<int> var)
....
and this would occur dynamically at runtime.
The short is that I get a 'No Overload Error' as follows.
'No overload for 'GetValue' matches delegate 'A<T>.GetValueDelegate'

I am obviously not understanding something. Help would be greatly
appriciated.

k
--
A single 20-watt compact fluorescent lamp used in place of a 75-watt
incandescent will save about 550 kilowatt-hours over its lifetime. That is
500 pounds of coal; a pile the size of your office desk.
Nov 17 '05 #3
Thank you for the reply but I do not think that is the reason. The delegate
in class A is defined with the signature. It is this signature that should
match against the methods in class B. And the methods in B do match.

public delegate T GetValueDelegate(A<T> var);

thanks, but I am still missing something.
Nov 17 '05 #4
Thank you for the reply but I do not think that is the reason. The delegate
in class A is defined with the signature. It is this signature that should
match against the methods in class B. And the methods in B do match.

public delegate T GetValueDelegate(A<T> var);

thanks, but I am still missing something.
Nov 17 '05 #5
Hello,
//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
I think you've supposed GetValue to be a generic method but it
isn't. Therefore no signature matches the on of your delegate.

public class B
{
// ...

public static SomeType GetValue<SomeType>(A<SomeType> var)
{
// ...
}
}

Btw. the overloaded versions won't get called, whether the signature
matches or not.

greets
On Fri, 22 Jul 2005 18:05:02 +0200, bigtexan <bi******@online.nospam>
wrote: I would like to do the following and cannot figure it out.

public class A<T>
{
public delegate T GetValueDelegate(A<T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}

public class B
{
public static int GetValue(A<int> var)
{
//do something and return int
}

public static float GetValue(A<float> var)
{
//do something and return float
}

public static string GetValue(A<string> var)
{
//do something and return string
}

//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
}

Now:-----------------------------------
I was thinking when I do

...somewhere in some code....
A<int> s = new A<int>();

that on creation of this instance of A of type int that the delegate
in A would find the proper overload in class B such that:
...
int I = s.GetValue();
...
would resolve to the class B's method
...
public static int GetValue(A<int> var)
...
and this would occur dynamically at runtime.
The short is that I get a 'No Overload Error' as follows.
'No overload for 'GetValue' matches delegate 'A<T>.GetValueDelegate'

I am obviously not understanding something. Help would be greatly
appriciated.

k

Nov 17 '05 #6
Hello,
//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
I think you've supposed GetValue to be a generic method but it
isn't. Therefore no signature matches the on of your delegate.

public class B
{
// ...

public static SomeType GetValue<SomeType>(A<SomeType> var)
{
// ...
}
}

Btw. the overloaded versions won't get called, whether the signature
matches or not.

greets
On Fri, 22 Jul 2005 18:05:02 +0200, bigtexan <bi******@online.nospam>
wrote: I would like to do the following and cannot figure it out.

public class A<T>
{
public delegate T GetValueDelegate(A<T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}

public class B
{
public static int GetValue(A<int> var)
{
//do something and return int
}

public static float GetValue(A<float> var)
{
//do something and return float
}

public static string GetValue(A<string> var)
{
//do something and return string
}

//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
}

Now:-----------------------------------
I was thinking when I do

...somewhere in some code....
A<int> s = new A<int>();

that on creation of this instance of A of type int that the delegate
in A would find the proper overload in class B such that:
...
int I = s.GetValue();
...
would resolve to the class B's method
...
public static int GetValue(A<int> var)
...
and this would occur dynamically at runtime.
The short is that I get a 'No Overload Error' as follows.
'No overload for 'GetValue' matches delegate 'A<T>.GetValueDelegate'

I am obviously not understanding something. Help would be greatly
appriciated.

k

Nov 17 '05 #7
Hey bigguy!

The problem is that A<T> can be instantiated with any Type T. Your B
overloads GetValue for some distinct A<T>'s: A<int>, A<float>, A<string>,
A<SomeType> but there will always be some type T (for example FooBar) that
it does not overload. Generic classes must be able to work for any T you
want to pameterize them with.

This seems to make them nearly useless since the only operations we can
perform on any type T are the ones in the common base class "object"

Enter constraints.

Constraints let us add operations that T can perform by constraining the
type of T

E.g
public class Foo<T> where T : IBar

So now we can use any operations that the IBar interface supports and in
turn we promise that whenever we instantiate a Foo we will pass a T that
implements IBar.

Now the bad news.
Interfaces don't support static members as part of their signature. So we
have no way to constrain the type to say that we will only try to create A<T>
with types that implement T GetValue(A<T>).

So what are you really trying to do?

"bigtexan" wrote:
I would like to do the following and cannot figure it out.

public class A<T>
{
public delegate T GetValueDelegate(A<T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}

public class B
{
public static int GetValue(A<int> var)
{
//do something and return int
}

public static float GetValue(A<float> var)
{
//do something and return float
}

public static string GetValue(A<string> var)
{
//do something and return string
}

//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
}

Now:-----------------------------------
I was thinking when I do

...somewhere in some code....
A<int> s = new A<int>();

that on creation of this instance of A of type int that the delegate
in A would find the proper overload in class B such that:
...
int I = s.GetValue();
...
would resolve to the class B's method
...
public static int GetValue(A<int> var)
...
and this would occur dynamically at runtime.
The short is that I get a 'No Overload Error' as follows.
'No overload for 'GetValue' matches delegate 'A<T>.GetValueDelegate'

I am obviously not understanding something. Help would be greatly appriciated.

k
--
A single 20-watt compact fluorescent lamp used in place of a 75-watt
incandescent will save about 550 kilowatt-hours over its lifetime. That is
500 pounds of coal; a pile the size of your office desk.

Nov 17 '05 #8
Hey bigguy!

The problem is that A<T> can be instantiated with any Type T. Your B
overloads GetValue for some distinct A<T>'s: A<int>, A<float>, A<string>,
A<SomeType> but there will always be some type T (for example FooBar) that
it does not overload. Generic classes must be able to work for any T you
want to pameterize them with.

This seems to make them nearly useless since the only operations we can
perform on any type T are the ones in the common base class "object"

Enter constraints.

Constraints let us add operations that T can perform by constraining the
type of T

E.g
public class Foo<T> where T : IBar

So now we can use any operations that the IBar interface supports and in
turn we promise that whenever we instantiate a Foo we will pass a T that
implements IBar.

Now the bad news.
Interfaces don't support static members as part of their signature. So we
have no way to constrain the type to say that we will only try to create A<T>
with types that implement T GetValue(A<T>).

So what are you really trying to do?

"bigtexan" wrote:
I would like to do the following and cannot figure it out.

public class A<T>
{
public delegate T GetValueDelegate(A<T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}

public class B
{
public static int GetValue(A<int> var)
{
//do something and return int
}

public static float GetValue(A<float> var)
{
//do something and return float
}

public static string GetValue(A<string> var)
{
//do something and return string
}

//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
}

Now:-----------------------------------
I was thinking when I do

...somewhere in some code....
A<int> s = new A<int>();

that on creation of this instance of A of type int that the delegate
in A would find the proper overload in class B such that:
...
int I = s.GetValue();
...
would resolve to the class B's method
...
public static int GetValue(A<int> var)
...
and this would occur dynamically at runtime.
The short is that I get a 'No Overload Error' as follows.
'No overload for 'GetValue' matches delegate 'A<T>.GetValueDelegate'

I am obviously not understanding something. Help would be greatly appriciated.

k
--
A single 20-watt compact fluorescent lamp used in place of a 75-watt
incandescent will save about 550 kilowatt-hours over its lifetime. That is
500 pounds of coal; a pile the size of your office desk.

Nov 17 '05 #9
Well,

Actually, I want to do this.

A<any_type> var1 = new A<any_type>();

//set up a etc...

any_type var2 = var1.GetValue();

where GetValue is dependant on var2.

Basically, I want a polymorfic collection class that does not need
boxing due to the number of objects I need to create and least of all,
I do not want to write 50 classes, I just want to write 50 GetValue()
methods that return a native type, not an object and thus have to cast
consistantly.

I find it odd that generics are so closely tied to constraints. Sort of
an oxymoronic relationship.

I understand that things need to bind with out exception, however, I
would rather chase a couple of exceptions than manage 50
implimentations of a generic idea. I get tired of subclassing.

And besides, wouldn't it be a compile time error if the type <T> didn't
have a corresponding overload available.

Thanks for your assistance BTH. It is helping me to sort out the
details here.

Nov 17 '05 #10
Well,

Actually, I want to do this.

A<any_type> var1 = new A<any_type>();

//set up a etc...

any_type var2 = var1.GetValue();

where GetValue is dependant on var2.

Basically, I want a polymorfic collection class that does not need
boxing due to the number of objects I need to create and least of all,
I do not want to write 50 classes, I just want to write 50 GetValue()
methods that return a native type, not an object and thus have to cast
consistantly.

I find it odd that generics are so closely tied to constraints. Sort of
an oxymoronic relationship.

I understand that things need to bind with out exception, however, I
would rather chase a couple of exceptions than manage 50
implimentations of a generic idea. I get tired of subclassing.

And besides, wouldn't it be a compile time error if the type <T> didn't
have a corresponding overload available.

Thanks for your assistance BTH. It is helping me to sort out the
details here.

Nov 17 '05 #11
QUICK TYPE CLARIFICATION:

ORIGIONAL
where GetValue is dependant on var2

NEW
where GetValue is in sync with the type of var2

Nov 17 '05 #12
QUICK TYPE CLARIFICATION:

ORIGIONAL
where GetValue is dependant on var2

NEW
where GetValue is in sync with the type of var2

Nov 17 '05 #13
What I am really trying to do is put all the GetValue implimentations
in a single assembly that I can dynamically swap out at runtime. A
strategy pattern.

Nov 17 '05 #14
Additionally, Is it possible to configure the constraint's list to

where T : int, float, double, etc....

It seems to be giving me errors.

Nov 17 '05 #15
dubian <co*****@bigtexansoftware.com> wrote:
Additionally, Is it possible to configure the constraint's list to

where T : int, float, double, etc....

It seems to be giving me errors.


Well, you can constrain T to be a value type:

where T : struct

The constraint you gave can't be applied because constraints are
"ANDed" - *every* constraint you specify has to be satisfied by the
candidate type, and clearly there are no types which are both ints and
floats :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #16
So I guess I have to subclass. It would be nice to have generic
delegates be able to resolve to a specialized matching overload. To
handle all other cases, a rule to require a generic function exist or
simply throw an exception would handle cases where no matching overload
was found.

This would be a nice feature and enable the following.

Sample<int> s = new Sample<int>(4,5,6,3,4,2,3,4);
Sample<char> c = new Sample<char>('a','b','c','d');

int Sum = s.GetValue();
int UniquesCount = c.GetValue();

where the method GetValue() can be routed with delegates to the a
specialized function using signature matching.

In general, when 95% of my operations on, say my Sample class, are
generic, but 5% is specialized, it would be nice to leverage that
commonality. I do not see where this implementation of generics by
Microsoft has made a big improvement of developer productivity.

Many may question my logic, but for those that have used PERL and
understand the power of indirection may see my logic. This may
introduce hard to understand code if you are coming behind someone, but
it also allows a powerful simplicity to be leveraged; and thus instead
of 2 weeks to subclass 50 items, it would take 1 day to specialize a
specific attribute of the code.

This problem could also be solved by allowing constraints of the form
where T: (int, float, char, string, classA, structB, interfaceC, etc)

I would like to know if anyone else agrees -

For example: I once wrote a packet parser where the header of the
packet aligned with the functions to handle those packet types. The
code looks like this.

$a = first4CharsOfCompletePacket(incommingPacket);
$$a(a);

If the first 4 chars (say 'ABBA') of the packet identify the
packet, then $$a(a) calls a function by the name ABBA that is
specialized to handle this specific packet. The call actually made by
$$a(a) is determined dynamically and proper error handling catches
malformed packets etc..
Otherwise, you have a big case statement to link packet type to packet
handler. When you have many packet types, that is a lot of work and
these two lines save thousands of lines in a large packet parser.

If PERL can do this on the CLR via visual PERL by ActiveState - then
so can C#.

Many programming problems naturally fit this scenario. It would be
refreshing to see a powerful language like C#, take a fresh direction
and leverage indirection formally and provide it as a tool that can be
used when the forces push us in this direction instead of adhering to
strict formalism where everything must be deterministic.

Call it bad programming or what ever, I call it meeting deadlines.

Nov 17 '05 #17

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

Similar topics

0
319
by: bigtexan | last post by:
I would like to do the following and cannot figure it out. public class A<T> { public delegate T GetValueDelegate(A<T> var); public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);...
12
2715
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
4
1548
by: Gazarsgo | last post by:
This seems to be a bit of a contradiction, but how can I construct a Generic class using a System.Type variable that is assigned at run time? Is there some parallel to the Generics concept that...
11
2469
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
9
5951
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
4
2794
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
4
3380
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
7
3242
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
3
1240
by: sloan | last post by:
I think someone posted about this before. Outside of collection type stuff, what are possible uses of a Generic. (Again, I'm cool with the collection type stuff). Thanks.............
0
7231
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
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
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
7504
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
4724
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...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.