inheritance not working properly -- Help please!!

  • Thread starter Thread starter ThunderMusic
  • Start date Start date
T

ThunderMusic

hi,

I have two classes in C#, one is the base, the other inherits from the base
class... both are generics... Here is the definition of the second class :

public class CNetworkServer<PlayerInfos> : CNetworkClient<PlayerInfos> where
PlayerInfos : CPlayerInfos, new()
{
}

Here is the problem... when I type "public override", the intellisense
should pop with my base class methods... instead it pops with 3 methods :
Equals, GetHashCode and ToString...

Is there a problem in my definition? here's on of my base class method that
should be overrided : public void Connect()... Must I add anything to the
method so I can override it in my inherited class?

I tried to rebuild the project, but no change, still the same...

Can anyone help please?

Thanks

ThunderMusic
 
Can you post a more complete simple code to better understand your
problem?
 
ok, I'll post the base class and the inherited class (definition and 1
method, because it would be too long)

public class CNetworkClient<PlayerInfos> : IDisposable where PlayerInfos :
CPlayerInfos, new()
{
public void Connect()
{
// Do things
}
}

public class CNetworkServer<PlayerInfos> : CNetworkClient<PlayerInfos> where
PlayerInfos : CPlayerInfos, new()
{
public override // that's where it should pop with the Connect() method,
but it's not
}

Everything seems normal to me, so I don't understand why it doesn't work...
it's important to notice that the project builds (by removing the "public
override" I just added in the CNetworkServer class

What could be the cause?

Thanks

ThunderMusic
 
You need to mark Connect as virtual; in Java virtual is the default; in C#
it isn't.

Marc
 
You need to add the virtual keyword to the methods in you base class that
you want to be able to override:
public virtual void Connect()

/claes
 
ok, so I must add virtual to all the methods I want to be able to override?
Is there any behavior I should be aware of when doing this? let's say, I
have a list of CNetworkClient and among those CNetworkClient objects there
are some CNetworkServer objects. If I call a method on all the objects, will
it call the overrided method in the CNetworkServer or it will call the base
method in the CNetworkClient class? here's an example I think of :

List<CNetworkClient<CPlayerInfos>> m_List

public void Some Method()
{
foreach (CNetworkClient<CPlayerInfos> net in m_List)
{
net.Connect();
}
}

will it call the CNetworkClient's Connect() or the CNetworkServer's connect
on CNetworkServer objects located in this list?

Thanks

ThunderMusic
 
or event worst case : if I call dont override a method and this method calls
Connect(), which one will it call?

thanks

ThunderMusic
 
It will always polymorphically call the deepest subclass' method.
Otherwise most object-oriented designs wouldn't work correctly at all.

IIRC, methods in C# aren't virtual by default basically for
performance reasons.

Eq.
 
There are some performance reasons .. namely that virtual methods cannot be
inlined but the reason it does this by default has to do with design. "When
methods are cirtual by default you need to design for inheritance (which
surprisingly enough most people don't do). As such you end up with fragile
objects.

Cheers,

Greg
 
I've heard there is a way to do what I just mentionned : calling the base
class' when referenced as a base class and call the inherited's when
referenced as inherited... not that I need it, but it can be useful to
know... is it true? is it just with c++?

thanks a lot for the help

ThunderMusic
 
Yes you can do that ... use the new operator (shadows in VB.NET)

public class Foo {
public void Test() {
Console.WriteLine("Foo::Test");
}
}

public class Bar : Foo {
public new void Test() {
Console.WriteLine("Bar::Test");
}
}
class Program {
static void Main(string[] args) {
Foo f = new Foo();
f.Test();
Bar b = new Bar();
b.Test();
Foo f2 = b;
f2.Test();
}
}
 
thanks a lot... ;)


Greg Young said:
Yes you can do that ... use the new operator (shadows in VB.NET)

public class Foo {
public void Test() {
Console.WriteLine("Foo::Test");
}
}

public class Bar : Foo {
public new void Test() {
Console.WriteLine("Bar::Test");
}
}
class Program {
static void Main(string[] args) {
Foo f = new Foo();
f.Test();
Bar b = new Bar();
b.Test();
Foo f2 = b;
f2.Test();
}
}
ThunderMusic said:
I've heard there is a way to do what I just mentionned : calling the base
class' when referenced as a base class and call the inherited's when
referenced as inherited... not that I need it, but it can be useful to
know... is it true? is it just with c++?

thanks a lot for the help

ThunderMusic
 
Greg Young said:
There are some performance reasons .. namely that virtual methods cannot be
inlined but the reason it does this by default has to do with design.

Indeed - the performance issues can be sorted out using an adaptive
JIT. Sun's Java JIT (HotSpot) does exactly this - a method can be
inlined up until the point that a subclass overrides it. At that point,
all the optimisation has to be undone. Of course, this is significantly
more complicated than a single-pass JIT like .NET's.
"When methods are cirtual by default you need to design for
inheritance (which surprisingly enough most people don't do). As such
you end up with fragile objects.

Indeed. See
http://msmvps.com/jon.skeet/archive/2006/03/04/inheritancetax.aspx
for more on my views on this.
 

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

Back
Top