Inheritance design quandry

  • Thread starter news.microsoft.com
  • Start date
N

news.microsoft.com

Hello,

I've got a design problem that I can't figure out. I need to override a
static method which happens to be referenced by methods inside nested
classes. In the sample below, in the call tst.runTest() I need the result
to display "child", however it always displays "parent". I can understand
all the reasons why it should display "parent" and not "child" but that
doesn't help me solve it.

In my real-world problem the parent class has fairly heavily integrated
child classes, and what I am trying to achieve is to allow some of the
functionality to be overridden externally. I do not want to hard-code any
overrides into the parent class itself, I want to treat it as if it were a
compiled library. The parent class can have modifications, but the nested
class relationship must remain. The way in which runTest is implemented is
pretty much the only thing that can change.

I would really appreciate any assistance.


Regards,

Troy


//-----------------------------------------
Public class someClass
{
public void someMethod()
{
childmyTest tst = new childmyTest();

tst.runTest();
}
}



//-----------------------------------------
public class childmyTest : myTest
{
public new static void docalc()
{
MessageBox.Show("child");
}
}
//-----------------------------------------


public class myTest
{
public void runTest()
{
resultClass rc = new resultClass();

rc.testMethod();
}

public static void docalc()
{
MessageBox.Show("parent");
}

public class resultClass
{
public void testMethod()
{
docalc();
}
}
}
 
J

Joanna Carter [TeamB]

"news.microsoft.com" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| I've got a design problem that I can't figure out. I need to override a
| static method which happens to be referenced by methods inside nested
| classes. In the sample below, in the call tst.runTest() I need the result
| to display "child", however it always displays "parent". I can understand
| all the reasons why it should display "parent" and not "child" but that
| doesn't help me solve it.


First of all, you need to change some of your terminology :

You are not nesting classes, you are inheriting one class from another.
Nesting means arranging classes like this :

class OuterClass
{
class InnerClass
{
...
}

...
}

I would argue that you don't yet really understand why this is not working
:)

You are declaring your method as static; this means that you can access it
without creating an instance of the class :

class Test
{
public static void DoSomething()
{
...
}
}

DoSomething can be a called like this :

{
Test.DoSomething();
}

Notice that I did not create an instance of Test by calling new.

You cannot override static methods; since you don't have an instance, you
simply call them on the appropriate class.

I believe what you are trying to do requires virtual a method, overridden in
the derived classes :

public class MyTest
{
public virutal void DoCalc()
{
MessageBox.Show("parent");
}
}

public class ChildMyTest : MyTest
{
public override void DoCalc()
{
MessageBox.Show("child");
}
}

Then you can use these classes like this :

public class SomeClass
{
public void SomeMethod()
{
MyTest tst = new ChildMyTest();

tst.DoCalc();

tst = new MyTest();

tst.DoCalc()
}
}

If you really need the method to be static and callable polymorphically, you
will have to create a metaclass that can be assigned to a variable and who's
*type* can be changed.

public class Test
{
public class Metaclass
{
internal protected Metaclass() { }

public virtual void DoSomething()
{
//...
}
}

protected Test() { }

public static Metaclass Class
{
get { return new Metaclass(); }
}
}

public class Derived : Test
{
public new class Metaclass : Test.Metaclass
{
internal protected Metaclass() { }

public override void DoSomething()
{
//...
}
}

protected Derived() { }

public static new Metaclass Class
{
get { return new Metaclass(); }
}
}

This arrangement is used like this :

{
Test.Metaclass test = Derived.Class;

test.DoSomething();
}

Does that help or confuse ? :)

Joanna
 
N

news.microsoft.com

Thanks for your reply, I am a bit confused... the terminology as I presented
it in the post is correct, as is the provided sample code:

"I need to override a static method which happens to be referenced by
methods inside nested classes."

In the sample, the parent class "myTest" contains a method "docalc". The
parent class contains a nested class "resultClass" which in turn contains a
method "testMethod". The method "testMethod" in the nested class calls the
method "docalc" in the base parent class "myTest". The new child class
"childmyTest" inherits the parent class "myTest" and attempts to override
the base parent class's method "docalc". When the new child class is
instantiated and the nested class's method "testMethod" is called via
"runTest", it runs the parent class's base method, not the new child class's
overridden (or shadowed) method. The code in the original post represents
this scenario exactly.

I can see why the parent's method is being called instead of the child's, I
am just wondering if there is a simple way around this without changing the
structure of the parent class - but the method runTest and the method docalc
can be repositioned and redefined in any way. runTest can be omitted
altogether because the method in the nested class can be made static. I
have seen a design that works, but with the amount of code I want to rework
and the amount of nested classes it would be a headache to say the least -
that's why I'm looking for the most simple solution to the problem.


Troy
 
N

news.microsoft.com

I worked out how to do it... this allows you to create a method in a base
class that may be called by nested classes in the base class, but can still
be overridden in child classes of the base - see the code below. I hope
this is helpful to someone.

/*
* The test
*/
public class someClass
{
public void someMethod()
{
childmyTest tst = new childmyTest();

tst.docalc();
}
}

/*
* The Child class
*/
public class childmyTest : myTest
{
/*
commenting this method will cause
'parent' to display
*/
public override void docalc()
{
MessageBox.Show("child");
}
}

/*
* The Parent class
*/
public class myTest
{
private static void docalc_internal()
{
MessageBox.Show("parent");
}

public virtual void docalc()
{
docalc_internal();
}

/*
Nested class
*/
public class resultClass
{
public static void testMethod1()
{
docalc_internal();
}
}
}
 
N

news.microsoft.com

Nevermind, there is an obvious flaw in that plan... I think I will need a
complete redesign.
 

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