|
I'm trying to use the child class method instead of the parent class for some child class cases. For some reason, I get to the parent class method, but it never gets to the override in the child. Any ideas why? I've been referring to http://stackoverflow.com/questions/9067885/calling-child-class-method-from-parent[^]
but it's not getting to the child's method.
In my PC.cs of the child class (Ko):
public partial class PC : GenericPC
{
public override void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Ko PC::ValidateDynData");
}
}
In my GenericPC.cs of the parent class:
private int processDynData()
{
ValidateDynData(itsEnteredDynData, ref returnCode);
}
public virtual void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Generic::ValidateDynData passing off to child to validate special data");
}
Any ideas why it's not going to the child class implementation of ValidateDynData when I call ValidateDynData in my parent class? This is the only area in my code where I am trying to have a child class override a parent implementation, so I'm not sure if I'm doing something wrong?
|
|
|
|
|
I tried it myself, by pasting your code into an app, and adding a few bits to prevent compilation errors:
private void button2_Click(object sender, EventArgs e)
{
PC pc = new PC();
int i = 4;
pc.ValidateDynData(null, ref i);
}
public partial class PC : GenericPC
{
public override void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Ko PC::ValidateDynData");
}
}
public class GenericPC
{
TextBox[] itsEnteredDynData;
int returnCode;
private int processDynData()
{
ValidateDynData(itsEnteredDynData, ref returnCode);
return returnCode;
}
public virtual void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Generic::ValidateDynData passing off to child to validate special data");
}
} Ignoring the "not assigned" warning message, when I press the button I get:
Ko PC::ValidateDynData Which is what I would expect - the overloaded version.
Could it be that you are using an instance declared as the base class, rather than the derived?
GenericPC gpc = new GenericPC();
gpc.ValidateDynData(null, ref i);
would give you
Generic::ValidateDynData passing off to child to validate special data
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
It's possible that's the problem. My object instance is created with Reflection:
_assembly = Assembly.LoadFrom(programDll);
_type = _assembly.GetType("CrWriter.PC");
_objectInstance = Activator.CreateInstance(_type);
_parameters = new Object[] { cb1, programDll, templateArr, itsDll, cert, i, cb1.cbInfos[i].boxID };
controls[i] = new Control();
controls[i] = (Control)_type.InvokeMember("GetPC", BindingFlags.Default | BindingFlags.InvokeMethod, null, _objectInstance, _parameters);
controls[i].Dock = DockStyle.None;
this.Controls.Add(controls[i]);
Since my Invocation of GetPC lands in the child class and not the parent, it must be created correctly. So I'm not sure why it's not landing in the correct ValidateDynData method. Maybe I need to cast my object to the programDll somehow. When I run the program and inspect the _objectInstance it could be a problem:
variable.............................................................value
base: {GenericCrWriter.GenericPC} ......CrWriter.PC
baseInst: .........................................................GenericCrWriter.GenericPC
But then, the _assembly is referring to Ko/PC and not Generic/GenericPC.
Also, my _assembly.GetType looks good. My Generic/parent doesn't have anything named CrWriter.PC
I wonder if there is a way to cast an object obtained through reflection?
modified 1-Mar-13 15:56pm.
|
|
|
|
|
MichCl wrote: Since my Invocation of GetPC lands in the child class and not the parent, it must be created correctly. So I'm not sure why it's not landing in the correct ValidateDynData method.
Presuming the former is really true then the possibilities are.
- The child signature of ValidateDynData is wrong
- You are not running the code that you think you are (binary does not match source.)
|
|
|
|
|
Do you see any problems with my method signatures?
This is the parent, GenericPC.cs:
public virtual void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Generic::ValidateDynData passing off to child to validate special dynamic data");
}
This is the child, PC.cs:
public override void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Ko PC::ValidateDynData");
}
The parent, GenericPC.cs calls the method this way:
ValidateDynData(itsEnteredDynamicData, ref returnCode);
I checked the correct version of Generic.dll is referenced in the child's project/class. I did a clean build in the child class. Any other binaries that should be checked?
|
|
|
|
|
I've been looking into this some more, and I think the reason flow in the parent is not seeing the overridden ValidateDynData in the child is because of the way I get flow into the parent from the child class:
Is there another way to get to the parent's method without creating an instance of the parent?
GenericPC baseInst = new GenericPC();
return baseInst.GetPC(cbInst, dllSel, templ, dll, cert0, slaveIndex, BoxID);
|
|
|
|
|