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



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.
.



Relevant Pages

  • Generics - A question on generics - delegates - runtime binding.
    ... public class A ... public delegate T GetValueDelegate(Avar); ... public static int GetValue ... 'No overload for 'GetValue' matches delegate 'A.GetValueDelegate' ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: using generics to serialize primitives.
    ... public class Sizeof ... public static int GetSize() ... BinaryFormatter serialization but it serializes objects so when I ...
    (microsoft.public.dotnet.languages.csharp)
  • function in Derby[begginer]
    ... public class Say { ... public static int say2{ ... parameter style java no sql language java ... type conversion for any parameters the method call may have. ...
    (comp.lang.java.databases)
  • function in derby[beginner]
    ... public class Say { ... public static int say2{ ... parameter style java no sql language java ... type conversion for any parameters the method call may have. ...
    (comp.lang.java.programmer)
  • function in derby[beginner]
    ... public class Say { ... public static int say2{ ... parameter style java no sql language java ... type conversion for any parameters the method call may have. ...
    (comp.lang.java.softwaretools)