473,586 Members | 2,463 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 GetValueDelegat e(A<T> var);
public GetValueDelegat e GetValue = new GetValueDelegat e(B.GetValue);
}

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

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

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

//or in general
public static SomeType GetValue(A<Some Type> 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>.GetValueD elegate'

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 1817
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******@onlin e.nospam> wrote in message
news:37******** *************** ***********@mic rosoft.com...
I would like to do the following and cannot figure it out.

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

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

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

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

//or in general
public static SomeType GetValue(A<Some Type> 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>.GetValueD elegate'

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******@onlin e.nospam> wrote in message
news:37******** *************** ***********@mic rosoft.com...
I would like to do the following and cannot figure it out.

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

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

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

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

//or in general
public static SomeType GetValue(A<Some Type> 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>.GetValueD elegate'

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 GetValueDelegat e(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 GetValueDelegat e(A<T> var);

thanks, but I am still missing something.
Nov 17 '05 #5
Hello,
//or in general
public static SomeType GetValue(A<Some Type> 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<SomeTy pe>(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******@onlin e.nospam>
wrote: I would like to do the following and cannot figure it out.

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

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

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

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

//or in general
public static SomeType GetValue(A<Some Type> 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>.GetValueD elegate'

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<Some Type> 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<SomeTy pe>(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******@onlin e.nospam>
wrote: I would like to do the following and cannot figure it out.

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

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

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

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

//or in general
public static SomeType GetValue(A<Some Type> 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>.GetValueD elegate'

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 GetValueDelegat e(A<T> var);
public GetValueDelegat e GetValue = new GetValueDelegat e(B.GetValue);
}

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

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

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

//or in general
public static SomeType GetValue(A<Some Type> 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>.GetValueD elegate'

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 GetValueDelegat e(A<T> var);
public GetValueDelegat e GetValue = new GetValueDelegat e(B.GetValue);
}

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

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

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

//or in general
public static SomeType GetValue(A<Some Type> 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>.GetValueD elegate'

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

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); } public class B {
12
2729
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 it defines the language more completely. Great to use.
4
1552
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 extends to having strictly-typed classes at run-time? I would gladly give up the compile-time errors if I could get rid of all these CType()s :)
11
2484
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 provide an implemation of the operator +. Now I want to write another generic class Plan<DType>, which can
9
5966
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 Experience Only, Please. ...
4
2802
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 RuleDelegate<T>(T item); In my class my goal is to use a generic list collection to contain my generic delegates. This will allow me to pass this List to...
4
3384
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 'Delegate as a valid Type declaration for the Value of a Generic Dictionary. I am curious as to why I can compile and use this syntax : it seems to...
7
3245
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 System.Collections.Generic namespace. Literature I have read on this ties generics in with collections, hence articulate their examples as such. That's fine,...
3
1243
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
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6617
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3838
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.