protected override inherit problem

  • Thread starter Thread starter skidz
  • Start date Start date
S

skidz

When I create a B object, and testcall in A is called, it does not
execute the OverrideMe() function in B, but instead A. Why, and how
can I get it to do this? I did a debug and "? this" while in the
A.OverrideMe() function, and it said "this" is a B.

public class A
{
protected override string OverrideMe()
{
throw new Exception("A.OverrideMe() must be overridden");
}

public virtual void testcall()
{
OverrideMe();
}
}

public class B : A
{
static B()
{
testcall();
}
protected override string OverrideMe()
{
DoSomethingKewl();
}
}


Just some other info, B is being instantiated via config:
<httpModules>
<add type="B" name="WEB_AUTHENTICATOR"/>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule,
System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/>
</httpModules>

and the function that is executing the testcall() is really an event.

Any insight would be appreciated.
 
Just fyi... had a minstake in there...

protected override string OverrideMe()
SHOULD BE
protected virtual string OverrideMe()
IN A CLASS


So...

public class A
{
protected virtual string OverrideMe()
{
throw new Exception("A.OverrideMe() must be overridden");
}

public virtual void testcall()
{
OverrideMe();
}

}

public class B : A
{
static B()
{
testcall();
}
protected override string OverrideMe()
{
DoSomethingKewl();
}

}
 
Hi,

I don't really get how can You call an instance method("testcall()") from a
static constructor.
Can You clarify this example a bit please ?
-Zsolt
 
Your right, that is stupid... but I just placed that in there to show
that testcall gets called.. not really thinking it out. Like I said
under additional info, testcall is actually an event... that gets
called fine. I just want to know now, how I can get B.OverrideMe() to
execute when called from A...

Any insight would be greatly appreciated.
 
The problem is that in class B, you are attempting to call your virtual
function in the constructor. When constructing an instance of an A, the B
instance is first construced and when its constructor is executing, the A
part of your ultimate instance has not been created yet. This is very
elequently described in
http://blogs.msdn.com/scottwil/archive/2005/01/14/353177.aspx. The main way
UI frameworks get around limitations like this is to perform very little
logic in a constructor and use an initialize pattern, having an virtual Init
method or an event framework that exposes an initialization event.

Patrice said:
Is this what you are trying to do ?

public class A
{
protected virtual void OverrideMe()
{
throw new Exception("A.OverrideMe() must be overridden");
}
public void testcall()
{
OverrideMe();
}
}
public class B : A
{
protected override void OverrideMe()
{
Console.WriteLine("OverrideMe from B");
}
}
class Program
{
static void Main(string[] args)
{
B b=new B();
b.testcall();
Console.ReadLine();
}
}

Else please provide some actual code without moving anything so that it
can compile and run...

Confused but if you really want "B.OverrideMe() to execute when called
from A" this is just not possible. A is self contained and if you call A
directly it will call whatever is defined in A. You can though call B and
the B version of testcall (whihc is unchanged from A) calls the overrideme
defined in B (not sure if this is what you meant by "called from A").
--
Patrice


"skidz" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...
Your right, that is stupid... but I just placed that in there to show
that testcall gets called.. not really thinking it out. Like I said
under additional info, testcall is actually an event... that gets
called fine. I just want to know now, how I can get B.OverrideMe() to
execute when called from A...

Any insight would be greatly appreciated.
 
Hey everyone.... thx for your answers...

Patrice... you code is 100% correct in what I am looking for, though
you last paragraph confused me a bit.... guess it just fair since I
was so confusing in the first place.

So is it possible for the code in B.OverrideMe() to execute when
calling testcall()?


public class A
{
protected virtual void OverrideMe()
{
throw new Exception("A.OverrideMe() must be overridden");
}
public void testcall()
{
OverrideMe();
}
}
public class B : A
{
protected override void OverrideMe()
{
Console.WriteLine("OverrideMe from B");
}
}
class Program
{
static void Main(string[] args)
{
B b=new B();
b.testcall(); <---------This is should
execute the B.OverrideMe(), not the A.OverrideMe()
Console.ReadLine();
}
}
 
Back
Top