Overriding generic virtual method

H

Harold Howe

Is it possible to override a specific instance of a virtual, generic
method? IE:

using System;
class Base
{
public virtual void Test<U>(U u)
{
Console.WriteLine("Base.Test {0}, u);
}
}


class Derived : Base
{
public override Test(string u)
{
Console.WriteLine("Derived.Test {0}, u);
}
}

This generates a compiler error (No suitable method to override). This
would be slick as heck, but it boils down to a form of specialization,
which I don't think is allowed.

H^2
 
K

Kevin Spencer

To override a method, your method sigunature must match the method signature
of the method you're overriding. Otherwise, you're overloading.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

In order to override you need to have the exact same signature.
 
M

Michael Bray

This generates a compiler error (No suitable method to override). This
would be slick as heck, but it boils down to a form of specialization,
which I don't think is allowed.

At the risk of being obvious, you could always override the base function
and do some if/elses with type checking, doing your specialized processing
if you find a match or calling base.Test if you don't.

-mdb
 
L

Lars-Inge Tønnessen \(VJ# MVP\)

class BaseA
{
public virtual void Test<U>(U u)
{
Console.WriteLine("Base.Test {0}", u);
}
}


class Derived : BaseA
{
// Overrides
public override void Test<U>(U u)
{
Console.WriteLine("Derived.Test {0}", u);
}
// Overloads
public virtual void Test(String u)
{
Console.WriteLine("{0}", u);
}
}

Regards,
Lars-Inge Tønnessen
 
H

Harold Howe

Ignacio said:
Hi,

In order to override you need to have the exact same signature.

But I don't want to override the entire family of methods, only the one
where the type parameter is string.

It sounds like what I want is not possible.

PS: I apologize for pasting in code that wasn't even close to compiling.
Here is a semi-complete example of what I am trying to do. It still
doesn't compile, but at least the errors are germaine to the problem:

using System;
class Base
{
public virtual void Test<U>(U u)
{
Console.WriteLine("Base.Test {0}", u);
}
}


class Derived : Base
{
public override void Test(string u)
{
Console.WriteLine("Derived.Test {0}", u);
}
}

class Program
{
[STAThread]
static void Main()
{
Base b = new Base();
string str = "hello";
b.Test(str);
}
}

H
 
H

Harold Howe

Ignacio said:
In order to override you need to have the exact same signature.

But I don't want ot override the entire family of functions, just the
one that is parameterized on type string. It sounds like it is not
possible to do what I want.

PS:

I apologize for pasting in code that was not even close to compiling.
Here is a complete example. This doesn't compile either, but at least
the errors are germane to the issue:

using System;
class Base
{
public virtual void Test<U>(U u)
{
Console.WriteLine("Base.Test {0}", u);
}
}


class Derived : Base
{
public override void Test(string u)
{
Console.WriteLine("Derived.Test {0}", u);
}
}

class Program
{
[STAThread]
static void Main()
{
Base b = new Base();
string str = "hello";
b.Test(str);
}
}

H^2
 
H

Harold Howe

Ignacio said:
In order to override you need to have the exact same signature.


But I don't want ot override the entire family of functions, just the
one that is parameterized on type string. It sounds like it is not
possible to do what I want.

PS:

I apologize for pasting in code that was not even close to compiling.
Here is a complete example. This doesn't compile either, but at least
the errors are germane to the issue:

using System;
class Base
{
public virtual void Test<U>(U u)
{
Console.WriteLine("Base.Test {0}", u);
}
}


class Derived : Base
{
public override void Test(string u)
{
Console.WriteLine("Derived.Test {0}", u);
}
}

class Program
{
[STAThread]
static void Main()
{
Base b = new Derived();
string str = "hello";
b.Test(str);
}
}

H2
 
H

Harold Howe

class Derived : BaseA
{
// Overrides
public override void Test<U>(U u)
{
Console.WriteLine("Derived.Test {0}", u);
}
// Overloads
public virtual void Test(String u)
{
Console.WriteLine("{0}", u);
}
}

This does not yield the behavior that I want. The second Test method
will not be invoke via polymorphism. In fact, I could't find a situation
where it will ever get invoked. Here is the code I tried. In both cases,
the output is

Derived.Test<> hello

using System;

class Base
{
public virtual void Test<U>(U u)
{
Console.WriteLine("Base.Test<> {0}", u);
}
}

class Derived : Base
{
public override void Test<U>(U u)
{
Console.WriteLine("Derived.Test<> {0}", u);
}

public virtual void Test(string u)
{
Console.WriteLine("Derived.Test {0}", u);
}
}

class Program
{
[STAThread]
static void Main()
{
Base b = new Derived();
string str = "hello";
b.Test(str);

Derived d = b as Derived;
b.Test(str);
}
}

H^2
 
G

Guest

As the compiler errror says, no suitable method found to override, Test()
does not equal Test<T>. Even in partial template specialization. Also, you
cannot use any of the sealed types to constrain the type. However I believe
that you could write:
class Derived:Base
{
public override Test<T> (U t)
{ .... }
}
which would allow you to code separate behavior for the Derived class.
 
H

Helge Jensen

The below will accomplish what you wish:
class Derived : Base
{
public override void Test<U>(U u)
{
if ( u is string )
Test((string)u);
else
base.Test(u);
 
J

Joanna Carter [TeamB]

"Harold Howe" <[email protected]> a écrit dans le message de [email protected]...

| Is it possible to override a specific instance of a virtual, generic
| method? IE:
|
| using System;
| class Base
| {
| public virtual void Test<U>(U u)
| {
| Console.WriteLine("Base.Test {0}, u);
| }
| }
|
|
| class Derived : Base
| {
| public override Test(string u)
| {
| Console.WriteLine("Derived.Test {0}, u);
| }
| }
|
| This generates a compiler error (No suitable method to override). This
| would be slick as heck, but it boils down to a form of specialization,
| which I don't think is allowed.

You could always try the following :

class Base<U>
{
public virtual void Test(U u)
{
Console.WriteLine("Base.Test {0}", u);
}
}

class Derived : Base<string>
{
public override void Test(string u)
{
Console.WriteLine("Derived.Test {0}", u);
}
}

Joanna
 
H

Harold Howe

Helge said:
The below will accomplish what you wish:


if ( u is string )
Test((string)u);
else
base.Test(u);

I couldn't get this to compile. csc complains about the cast. If I
change to use the as operator, it works.

Also, someone else already suggested that I perform a runtime check. I
know I can make a runtime check work. What I want to know is if there is
a way to get the correct dispatching without performing runtime type
checks. I think the answer is no.

H^2
 
H

Helge Jensen

Harold said:
I couldn't get this to compile. csc complains about the cast. If I
change to use the as operator, it works.

Oh, yes -- I see. I havent really spent too much time with genericity yet.

I've been reading the C# language spec to try and find out why
conversion using "as" is usable while casting is not. I can't really
grok it.
Also, someone else already suggested that I perform a runtime check. I
know I can make a runtime check work. What I want to know is if there is
a way to get the correct dispatching without performing runtime type
checks. I think the answer is no.

What i suggested *is* a runtime-check, it just occurs only when x.f is
invoked with x type-qualified as Base.

There is no way to compile-time dispatch to Derived.f(string) instead of
Base.f<T>(T t) when the invoking object is static type-qualified as Base.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top