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

Tech-Archive recommends: Fix windows errors by optimizing your registry



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 <bigtexan@xxxxxxxxxxxxx> 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
.



Relevant Pages

  • Re: inferring a type
    ... This is nothing close to what var is proposing. ... > object x = new SomeType(); ... >> little redundant. ... >> that the video was posted in 2004, so I'm wondering, is this some feature ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: use of var
    ... over time var form can become "var ... somethingElse" while "Form somethingElse" is still good. ... var someType = SomeMethodReturningSomeType; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: inferring a type
    ... Perhaps it's not quite the same as using var x as in scripting languages, ... SomeType can be cast to it's inheriting types, ... the best design is achieved by removing or not designing a feature." ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Another feature request
    ... You will have to know the type of <sometype> else you can not do ... With your construct you probably could, and are faced with a bug in ... This one will be fairly easy to spot if you have the source code where ...
    (borland.public.delphi.non-technical)