Hiding baseclass members

  • Thread starter Thread starter Kimmo Laine
  • Start date Start date
K

Kimmo Laine

Hi,

is there a way to hide baseclass members:

class BaseClass {
public BaseClass() {}
public string Get() { return "base"; }
}

class MyClass: BaseClass {
public MyClass() :base() {}
new public string Get() { return "my"; }

}

void Foo() {
MyClass m = new MyClass();
Debug.WriteLine( m.Get() );

BaseClass b = m;
Debug.WriteLine( b.Get() );
}

This code will print "my" and then "base". Is there as way to hide the
BaseClass.Get completely?


thx

Kimmo Laine
 
Kimmo Laine said:
is there a way to hide baseclass members:

Well, you can override them instead of hiding them, if they're virtual.
That's the only way though.
 
Not that this is a solution... but I've generally found that when you start
wanting to hide base class methods, you probably need to reconsider the
design. Perhaps you need to use the decorator pattern and delegate the
exposed functions to a member variable?
 
Hi,

is there a way to hide baseclass members:

class BaseClass {
public BaseClass() {}
public string Get() { return "base"; }
}

Make your BaseClass.Get() function virtual...

public virtual string Get() { ... }
class MyClass: BaseClass {
public MyClass() :base() {}
new public string Get() { return "my"; }

}

And make your MyClass.Get override it:

public override string Get() { ... }

-mdb
 
Back
Top