inheritance issue

H

Hans De Schrijver

I'm trying to do the following:
Class1 (= base class)
public virtual Method1{}

Class2 : Class1
public override Method1
{
return base.Method1;
//do some more processing specific to Class2
}

Class3 : Class2
public override Method1
{
return base.Method1;
//do some more processing specific to Class3
}

Problem is I'm getting a compile error on Class3.Method1, stating that no
suitable method can be found in Class2 to override.
Question is, how can I override Class1.Method1 in Class2 yet make
Class2.Method1 virtual so Class3 can override it?
I've tried several access modifiers, but I'm getting errors each time.

Any ideas?

-- Hans De Schrijver
 
G

gani

Hans,

im really not sure if i get your problem right, but
this sample works for me:

using System;

public class MyClass
{
public static void Main()
{
Class3 c3 = new Class3();
Console.WriteLine(c3.Method1());
Console.ReadLine();
}
}
public class Class1
{
public virtual object Method1()
{
return "class1";
}
}
public class Class2 : Class1
{
public override object Method1()
{
return base.Method1();
}
}
public class Class3 : Class2
{
public override object Method1()
{
return base.Method1();
}
}

HTH,

--
gani
http://thedeveloperscorner.com.ph

-----Original Message-----
I'm trying to do the following:
Class1 (= base class)
public virtual Method1{}

Class2 : Class1
public override Method1
{
return base.Method1;
//do some more processing specific to Class2
}

Class3 : Class2
public override Method1
{
return base.Method1;
//do some more processing specific to Class3
}

Problem is I'm getting a compile error on
Class3.Method1, stating that no
 
J

Jon Skeet [C# MVP]

Hans De Schrijver said:
I'm trying to do the following:
Class1 (= base class)
public virtual Method1{}

Class2 : Class1
public override Method1
{
return base.Method1;
//do some more processing specific to Class2
}

Class3 : Class2
public override Method1
{
return base.Method1;
//do some more processing specific to Class3
}

Please post *actual code*. Your derived classes appear to be trying to
override properties rather than methods, and you haven't specified any
return types.
Problem is I'm getting a compile error on Class3.Method1, stating that no
suitable method can be found in Class2 to override.
Question is, how can I override Class1.Method1 in Class2 yet make
Class2.Method1 virtual so Class3 can override it?
I've tried several access modifiers, but I'm getting errors each time.

Any ideas?

When you post the real code, I'm sure the problem will become apparent.
 

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