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. Replicate a class from an assempli and add a new constructor

Replicate a class from an assempli and add a new constructor

Scheduled Pinned Locked Moved C#
question
6 Posts 4 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.
  • P Offline
    P Offline
    PozzaVecia
    wrote on last edited by
    #1

    I need to crate a new class which replicate a class in a given assembly but adding also a new constructor and adding new methods. Is it possible?

    M D 2 Replies Last reply
    0
    • P PozzaVecia

      I need to crate a new class which replicate a class in a given assembly but adding also a new constructor and adding new methods. Is it possible?

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Yes, it's called inheritance and is a cornerstone of OOP.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • P PozzaVecia

        I need to crate a new class which replicate a class in a given assembly but adding also a new constructor and adding new methods. Is it possible?

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        If the class is a class (and not a struct) and isn't sealed then you can inherit from it:

        public class YourClass : OriginalClass
        {
        // Recreate the constructors as they aren't inherited
        public YourClass(...) : base(...)
        { }

        // Add whatever you want...
        

        }

        If it's a struct or a sealed class then you will need to create a wrapper and replicate all the constructors/properties/methods etc you require:

        public WrapperClass
        {
        private WrappedType value;

        public WrapperClass(...)
        {
            value = new WrappedType(...);
        }
        
        public void DoSomething()
        {
            value.DoSomething();
        }
        
        // Add whatever you want...
        

        }

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        P 1 Reply Last reply
        0
        • D DaveyM69

          If the class is a class (and not a struct) and isn't sealed then you can inherit from it:

          public class YourClass : OriginalClass
          {
          // Recreate the constructors as they aren't inherited
          public YourClass(...) : base(...)
          { }

          // Add whatever you want...
          

          }

          If it's a struct or a sealed class then you will need to create a wrapper and replicate all the constructors/properties/methods etc you require:

          public WrapperClass
          {
          private WrappedType value;

          public WrapperClass(...)
          {
              value = new WrappedType(...);
          }
          
          public void DoSomething()
          {
              value.DoSomething();
          }
          
          // Add whatever you want...
          

          }

          Dave
          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          P Offline
          P Offline
          PozzaVecia
          wrote on last edited by
          #4

          in may case the base class have not a parameterless constructor, while in the derived I need this one. Is it possible to istantiate the base class in later mathod?

          D A 2 Replies Last reply
          0
          • P PozzaVecia

            in may case the base class have not a parameterless constructor, while in the derived I need this one. Is it possible to istantiate the base class in later mathod?

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            Not by using inheritance - you would need to use a wrapper as in my second snippet, the base would stay null until instanciated.

            Dave
            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            1 Reply Last reply
            0
            • P PozzaVecia

              in may case the base class have not a parameterless constructor, while in the derived I need this one. Is it possible to istantiate the base class in later mathod?

              A Offline
              A Offline
              Andy411
              wrote on last edited by
              #6

              You cannot do it later in a method, but if you know the parameters, you can do it like this:

              class BaseClass
              {
              public BaseClass(int theParam)
              {
              }
              }

              class DerivedClass : BaseClass
              {
              public DerivedClass() : base(4711)
              {
              }
              }

              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