Extending inherited class

Trihugger

New member
Joined
Dec 3, 2017
Messages
2
Programming Experience
Beginner
Hello,

I've been learning C# for a few months. I'm currently trying to explore the capabilities of classes in C#. Especially the concepts of Inheritance, Polymorphism, and Regression.

I'm getting stumped on a case scenario and I'm hoping someone can clarify the limits and methodology of it.

The concept is this: 1) Create a base class that contains a data class to be used as the base object for other classes.
2) the second class would inherit from the base class and define/extend on the data class.

I put together an example of what I'm trying to do as basically as I could:

C#:
namespace ClassExample
{
    public class FirstClass
    {
        public class MyClass {
            //to be determine (elements of this data class) in the final class
        }


        public List<MyClass> myClass(MyClass item1, MyClass item2)
        {


            var myList = new List<MyClass> {
                item1,
                item2
            };


            return myList;
        }
    }


    public class SecondClass : FirstClass
    {
        public class MyClass2 : MyClass
        {
            public string myName;
            public string myDescription;
        }


        public SecondClass()
        {
            List<MyClass2> myList2 = new List<MyClass2>();


            MyClass2 myItem1 = new MyClass2();
            myItem1.myName = "John";
            myItem1.myDescription = "JOhn is my name";
            MyClass2 myItem2 = new MyClass2();
            myItem2.myName = "Mary";
            myItem2.myDescription = "Mary is my name";


            myList2 = myClass(myItem1, myItem2);
            
        }
    }
}

Since this is a concept used there is no other data behind it. I tried different approaches to instantiating the FirstClass, but left it out for simplicity.

Business cases could be anything that pulls data and manipulates it, a web api, a json file, or any other type.

Thank you,
 
Why are you nesting classes at all? It's very rare that doing so is a good idea and trying to inherit a nested class in a derived class really makes no sense. In your case, I can't think of a situation where what you're trying to do would be a good idea. If you want to define a class to represent data then there's no good reason for that to be declared inside another class. Basically, all four of your classes there should be defined independently with no nesting. Then, deriving MyClass2 from MyClass1 will be no different to deriving SecondClass from FirstClass.
 
That said, if you really wanted to do what you suggest then it's simply a matter of qualifying the name of the nested class when you want to inherit it:
public class FirstClass
{
    public class MyClass
    {

    }
}
public class SecondClass
{
    public class MyClass2 : FirstClass.MyClass
    {

    }
}

That's no different to any reference to MyClass outside of FirstClass. Nesting classes may look the same as declaring members but it's not. The outer class basically acts as a namespace and, just as with a namespace, you must qualify a member type in any code outside that namespace.
 
Hi jmcilhinney,

Thank you for the quick reply. I wasn't sure if I could do what I wanted, but I think you verified that my fears were correct.

I even tried playing around with the concept of defining a partial class for the Data model in the Base Class, and implement it in the derived class. As you can imagine that was a very futile, and pathetic even, attempt.

What I ended up doing was setting my base class with a T return and the derived class defining the T and passing it along. My purpose was really to have flexibility in defining the data class/model and still be able to centralize my base code.

Here's a, very simple, example of what I ended up doing:

C#:
public class BaseClass
{
     public void T getList<T>()
     {
             return defaul(T);
     }
}

C#:
public class TestModel: BaseClass
{
     public void GetList()
    {
           var myReponse = getList<List<DataModel>>();
     }

    public class DataModel
    {
           public int ID {get; set;}
           public string Name {get; set;}
    }
}

One of the reasons I wanted the data model class inside was so it could be encapsulated with the base class, and be able to use it directly in the base class. Which this does just that.

If you can see anything else that I'm missing, or something that would make it more programmatically correct. I welcome any tips, thoughts, or best practices.

Thank you,

Tri
 
One of the reasons I wanted the data model class inside was so it could be encapsulated with the base class, and be able to use it directly in the base class. Which this does just that.

That seems totally misguided. Declaring one class inside another doesn't change how you use the inner class inside the outer class at all. What it does change is using it outside the outer class. The only good reason to nest one class inside another is that you want to declare it private and use it only inside that outer class. Microsoft themselves used to nest some classes if they were closely related, e.g. ListViewItem.ListViewSubItem, but they have been recommending against doing that for several years. There's just no good reason to do what you're trying to do.
 

Latest posts

Back
Top Bottom