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. When do I need to override constructors for GetConstructor?

When do I need to override constructors for GetConstructor?

Scheduled Pinned Locked Moved C#
hardwarequestion
4 Posts 2 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.
  • R Offline
    R Offline
    RichardM1
    wrote on last edited by
    #1

    I have a class class1, which has a constructor and one method:

    public class1(TextReader tr)
    {
    InitFromTextReader(tr);
    }
    protected virtual bool InitFromTextReader(TextReader tr)
    {
    // init code goes here
    }

    It has subclass class1_1, which also has a subclass class1_1_1. These two classes both override InitFromTextReader. The text stream I am reading has class names embedded in it, just before the data for the object. So I read the class name, get the Type object for it and ask for the constructor that takes a TextReader as it's one and only parameter. GetConstructor is not finding that constructor for the subclasses. I used GetConstructors, and it just does not exist. So, all that to ask: Even though they do not do a dang thing, do I need to write constructors

    public class1_1(TextReader tr)
    : base(tr)
    {
    }

    and

    public class1_1_1(TextReader tr)
    : base(tr)
    {
    }

    so that I can find them with GetConstructor? It this kind of thing wot causes unrest!

    Learn to write self marginalizing code! Call 1-888-BAD-CODE

    T 1 Reply Last reply
    0
    • R RichardM1

      I have a class class1, which has a constructor and one method:

      public class1(TextReader tr)
      {
      InitFromTextReader(tr);
      }
      protected virtual bool InitFromTextReader(TextReader tr)
      {
      // init code goes here
      }

      It has subclass class1_1, which also has a subclass class1_1_1. These two classes both override InitFromTextReader. The text stream I am reading has class names embedded in it, just before the data for the object. So I read the class name, get the Type object for it and ask for the constructor that takes a TextReader as it's one and only parameter. GetConstructor is not finding that constructor for the subclasses. I used GetConstructors, and it just does not exist. So, all that to ask: Even though they do not do a dang thing, do I need to write constructors

      public class1_1(TextReader tr)
      : base(tr)
      {
      }

      and

      public class1_1_1(TextReader tr)
      : base(tr)
      {
      }

      so that I can find them with GetConstructor? It this kind of thing wot causes unrest!

      Learn to write self marginalizing code! Call 1-888-BAD-CODE

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #2

      Yes you would need to implement the constructors in your derived classes. The base constructor only constructs the base object. Really you don't need the InitFromTextReader because you should be able to perform the initialization in the constructors (which pass the TextReader to it's base class).

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      R 1 Reply Last reply
      0
      • T TJoe

        Yes you would need to implement the constructors in your derived classes. The base constructor only constructs the base object. Really you don't need the InitFromTextReader because you should be able to perform the initialization in the constructors (which pass the TextReader to it's base class).

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        R Offline
        R Offline
        RichardM1
        wrote on last edited by
        #3

        Thanks, I appreciate the quick response, but I was afraid of that. I needed the InitFromTextReader anyway, as I do not just use it from the constructor. So what it comes down to is you can not implicitly inherit a constructor's capabilities in c#: To access the capability, you must override it to explicitly expose it, even if there is no other functionality added.

        Learn to write self marginalizing code! Call 1-888-BAD-CODE

        T 1 Reply Last reply
        0
        • R RichardM1

          Thanks, I appreciate the quick response, but I was afraid of that. I needed the InitFromTextReader anyway, as I do not just use it from the constructor. So what it comes down to is you can not implicitly inherit a constructor's capabilities in c#: To access the capability, you must override it to explicitly expose it, even if there is no other functionality added.

          Learn to write self marginalizing code! Call 1-888-BAD-CODE

          T Offline
          T Offline
          TJoe
          wrote on last edited by
          #4

          Correct.

          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

          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