Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trying to use child class method by overriding parent

Trying to use child class method by overriding parent

Scheduled Pinned Locked Moved C#
comquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MichCl
    wrote on last edited by
    #1

    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); //start here
    }

    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?

    OriginalGriffO 1 Reply Last reply
    0
    • M MichCl

      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); //start here
      }

      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?

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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); //start here
      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.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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); //start here
        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.

        M Offline
        M Offline
        MichCl
        wrote on last edited by
        #3

        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();
        //The following lands in my child's GetPC method
        //My parent also has a method called GetPC
        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?

        J 1 Reply Last reply
        0
        • M MichCl

          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();
          //The following lands in my child's GetPC method
          //My parent also has a method called GetPC
          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?

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          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.)

          M 1 Reply Last reply
          0
          • J jschell

            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.)

            M Offline
            M Offline
            MichCl
            wrote on last edited by
            #5

            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?

            M 1 Reply Last reply
            0
            • M MichCl

              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?

              M Offline
              M Offline
              MichCl
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups