Search code examples
c#winformsclass

Child Class Object using (or attempting to use) Parent Class Object function instead of its own


To get straight to the point, on a WinForms project, I have the following classes (All in the same namespace):

private class Operation
{
    //Empty because it's simply base class
    //A "ToStrings" is planned though
}

private class Enumer : Operation
{
    //"index" field not needed
    private String? tmpl;
    private int start;
    private int digits;
    private int step;

    public Enumer(String tmpl_, int start_, int digits_, int step_)
    {
        tmpl = tmpl_;
        start = start_;
        digits = digits_;
        step = step_;
    }

    public Enumer(Dictionary<string, object> j)
    {
        tmpl = (string)j["riTabsOpsTabsEnmTmpFld"];
        start = Decimal.ToInt32((decimal)j["riTabsOpsTabsEnmOptsStrtFld"]);
        step = Decimal.ToInt32((decimal)j["riTabsOpsTabsEnmOptsStepFld"]);
    }

    public new void ExecuteOpp(FileLst wrkngLst)
    {
        //Contents not relevant to problem.
    }
}

The Enumer class is created using the following Dictionary object and function:

private static Dictionary<string, Func<Dictionary<string, object>, Operation>> operationsDict = new Dictionary<string, Func<Dictionary<string, object>, Operation>>
{
    {"enum", (dic) => new Enumer(dic)}, 
    //{"class2", (dic) => new Class2(dic)}
};
public void executeOpp(Dictionary<string, object> opDic)
{
    Operation op = operationsDict[(string)opDic["op"]](opDic);
    op.ExecuteOpp(bigLst); //<---
}

What I am attempting to do here is dynamically create a class based on a matching item in my Dictionary object, and then execute its functions. The ultimate plan is to have a List<T> of Operations.

The problem occurs where this, (<---), is. From what I am getting from the debugger, the ExecuteOpp() function in Operation is being targeted and not the one in Enumer.

Here is what the debugger says about the op object (Which was successfully constructed with all petameters):

name value type
op {rename_inator2.Functionals.Enumer} rename_inator2.Functionals.Operation {rename_inator2.Functionals.Enumer}

I don't have much to say in regard to what I have tried. I initially blamed my own implementation of a "dynamic class factory" and looked for other implementations to try. Like this, and this. But each one led to the same problem.

I should note that the code does work if I do,

Enumer e1 = new(opDic);
e1.ExecuteOpp(bigLst); 

The solution is probably dangling right in front of me, but I probably just don't know what terms to search for. I am also open to trying a new approach to my overall plan with this. Any help is appreciated.


Solution

  • Without overriding, you are simply hiding the method declared in the base class, therefore, a variable of the base class (pointing to sub-class) will cause base class's method to execute. Try with virtual / overriding.

    Also you can search for Polymorphism for a better understanding.

    private class Operation
    {
        //Empty because it's simply base class
        //A "ToStrings" is planned though
    
        // marking the method as 'virtual' so that sub-class can override the functionality.
        public virtual void ExecuteOpp(FileLst wrkngLst)
        {
            //Contents not relevant to problem.
        }
    }
    
    private class Enumer : Operation
    {
        // --- other code ---
    
        // instead of 'new', we are using 'override'.
        public override /*new*/ void ExecuteOpp(FileLst wrkngLst)
        {
            //Contents not relevant to problem.
    
            // calling the base class's method, can be removed if not required.
            base.ExecuteOpp(wrkngLst);
        }
    }