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();
}
}
}
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();
}
}
}