By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
16 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.