none
Create a list of the current class inside the current instance of the class whe using a derived class. RRS feed

  • Question

  • Hello,

    Consider the following code.  I am using a Base Class  along with a Derived Class.  What I would like to do is create a list of my  derived class as the example shows   How can I determine the derived class type so I can declare the list<derivedclass>

    I would like to keep this generic so I can use any derived class.

    Thanks for your help.

    using System.Collections.Generic;
    
    namespace AbstractSandbox
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                TheDerivedClass1 n = new TheDerivedClass1();
                n.SomeString = "Test";
                n.aNumber = 1;
    
                n.method_one("xyz");
            }
        }
    
        public class TheDerivedClass1 : TheBaseClass
        {
            private int _aNumber;
    
            public int aNumber
            {
                get { return _aNumber; }
                set { _aNumber = value; }
            }
        }
    
        public class TheDerivedClass2 : TheBaseClass
        {
            private double _aNumber;
    
            public double aNumber
            {
                get { return _aNumber; }
                set { _aNumber = value; }
            }
        }
    
    
        public class TheBaseClass
        {
            private string _someString;
    
            public string SomeString
            {
                get { return _someString; }
                set { _someString = value; }
            }
    
            public TheBaseClass()
            {
                var alist = new List<string>();
    
    
                // Is it possible to get the type of the instance that used the base class and plug 
                // it into a list that is inside the base class so I can make a list of the derived
                // classes.  I would like this generic so any derived class can be used.
    
                var blist = new List<Want_the_derived_class_here>();
    
            }
    
            public void method_one(string somthing)
            {
    
            }
    
        }
    }

    Thursday, March 8, 2018 3:14 AM

All replies

  •   // Is it possible to get the type of the instance that used the base class and plug
               
    // it into a list that is inside the base class so I can make a list of the derived
               
    // classes.  I would like this generic so any derived class can be used.

               
    var blist = new List<Want_the_derived_class_here>();

    ---------------------------------------------------------------------------------It makes no sense, it's off the ceiling, and I suggest that you abandon this.

    Thursday, March 8, 2018 7:39 AM
  • Hello,

    Can you elaborate as to why wanting to be able to declare the class within itself is "off the ceiling"?   I was using this as an example.

    However in practice I am declaring another class from within my class that requires a Type to be passed in. For example:

    var z = new SomeOtherClass<Want_derived_class_here>(argument);

    I can successfully declare this as:

    var z = new SomeOtherClass<TheDerivedClass1>(argument);
    
    //or if I want derived class2
    
    var z = new SomeOtherClass<TheDerivedClass2>(argument);
    
    
    

    However I would like to be able to derive a new class and have it passed to the external class.  It seems like a reasonable thing to do to me.

    Thursday, March 8, 2018 1:04 PM
  • Can you elaborate as to why wanting to be able to declare the class within itself is "off the ceiling"?   I was using this as an example.

    Sometimes developers look for advise on something that is wild. But after thinking about this,  maybe this will work for you. I don't know.

    var blist = new List<AbstractSandbox.TheDerivedClass2>();

    Thursday, March 8, 2018 1:35 PM
  • It's possible. It is occasionally useful in fact.

    class BaseType<TDerived>
    {
        public BaseType ()
        {
            var list = new List<TDerived>();
        }
    }
    
    class DerivedType1 : BaseType<DerivedType1> { }
    class DerivedType2 : BaseType<DerivedType2> { }

    But your specific example wouldn't make sense to me. Why would the base type need a list of derived types? More likely you'd be using a generic type that stores T objects in a list so inheritance has nothing to do with it.

    As an academic exercise it works but please provide the real world problem you're trying to solve so we can point you in a potentially better direction. 


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, March 8, 2018 3:12 PM
    Moderator
  • Hello,

    I have working with the NamedPipeWrapper Package.

    See: https://github.com/acdvorak/named-pipe-wrapper/blob/master/ExampleCLI/MyServer.cs 

    The constructor requires the Class.

    I have a class that wraps the functionality of NamePipeWrapper to include some specific functions that my application requires.  This is all working just fine other than the fact if I want to add a new function or extend it requires a whole new class. 

    Instead of this I would like to be able to extend the class using inheritance.  To use the NamedPipeWrapper  I need to pass the Derived Class to its constructor.

    So essentially I want to ask:

    Hey "What I AM I",   and then pass What I AM to the NamedPipeServer constructor.

    var server = new NamedPipeServer<WhatIAm>(pipeName);

    That's what I am trying to do.

    Thursday, March 8, 2018 3:53 PM
  • "This is all working just fine other than the fact if I want to add a new function or extend it requires a whole new class."

    Have you looked into simply using extension methods? Alternatively aggregation works well in these types of situations to avoid a bunch of derived types.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, March 8, 2018 4:07 PM
    Moderator