0

I am a bit confused about adding classes to a collection. In this case a Dictionary.

I got liked to a "similar" thread.. but it appears to be a totally diff rent issue. My question is about putting multiple classes in a dictionary that are inherited form a common base class. The linked thread is about storing different types like int, string, double.. etc.

class ClassName
{
    public string name { get; }
    public ClassName()
    {
        name = "name";
    }
}

class Unique : ClassName
{
    public string uName { get; }
    public Unique()
    {
        uName = "UniqueName?";
    }
}

class YAunique : ClassName
{
    public string yaName { get; }
    public YAunique()
    {
        yaName = "YetAnotherName";
    }
}

class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, ClassName> doesThisWork = new Dictionary<string, ClassName>();
        doesThisWork.Add("test", new Unique());
        doesThisWork.Add("test2", new YAunique());

        Console.WriteLine(doesThisWork["test"].name);
        Console.WriteLine(doesThisWork["test"].uName); //dose not work
        Console.WriteLine(doesThisWork["test2"].name);
        Console.WriteLine(doesThisWork["test2"].yaName); //does not work

        Unique example = new Unique();
        Console.WriteLine(example.name);
        Console.WriteLine(example.uName);

        YAunique example2 = new YAunique();
        Console.WriteLine(example2.name);
        Console.WriteLine(example2.yaName);

        // Pauses the console window
        Pause4Input();
    }

Basically in the example above I have 3 classes, 2 of which are inherited off the same class. If I initialise either of the inherited classes I can access both the base class's variable and the child class variable (the prints at the bottom of the code)....

....but what I am trying to do is place those child classes in a dictionary.

The thing is even though it "looks" kinda right... I can only access the variables in the base class using the dictionary keys.

TL;DR I am trying to work out how to have different classes added to a dictionary collection and have all the child class functions and variables and the base class functions and variables accessible.

Thanks!

aJynks
  • 677
  • 2
  • 14
  • 27
  • It wouldn't make sense to be able to access every method. Imagine if you had a dictionary with object (the most base class), you could type any method in the world in it would some how need to know at compile time if it was correct or not. In short, you will need to store a base class/object/interface and cast it, (or a dynamic and guess). – TheGeneral Jan 21 '19 at 02:19
  • The derived classes are acceptable to a dictionary of the base class because they are guaranteed to have all the properties, methods,etc. of the base class. Base class members are accessible. When you want the members of the derived class, you must cast the element back to the underlying type. – Mary Jan 21 '19 at 02:35

2 Answers2

0

Well, it looks like you got the basics of "Inheritance" but you seem to be having some trouble about what is going to be accessible to base class and what is to derived class. So you have a Dictionary<string, BaseClass>. When you access the value in this dictionary it will be type of BaseClass. Of course it may be of the any type which derives from BaseClass as well. However, unless you cast your value to the correct type you cannot access the derived types properties, methods, etc. You should be careful on how you cast though. Please consider Pattern Matching (Starting from c#7.0)

// value is BaseClass, only methods, properties of BaseClass usable  .
var value = dict["myKey"];

// Now derived classes properties etc can be accessed.
var casted = (Derived)value;
Hasan Emrah Süngü
  • 3,488
  • 1
  • 15
  • 33
0

You may try leveraging polymorphism.Instead of having different "Names" properties in each derived classes, define a common property (or common properties) in base class and override them in derived classes.

using System;
using System.Collections.Generic;

abstract class ClassName {

 public abstract string Name {get;}

}

class Unique : ClassName
{
    public override string Name {
    get{return "UniqueName?";}
    }
    public Unique()
    {
    }
}

class YAunique : ClassName
{
    public override string Name {
        get{return "YetAnotherName";}
    }
    public YAunique()
    {

    }
}

public class Program
{
    public static void Main()
    {
        Dictionary<string, ClassName> doesThisWork = new Dictionary<string, ClassName>();
        doesThisWork.Add("test", new Unique());
        doesThisWork.Add("test2", new YAunique());

        Console.WriteLine(doesThisWork["test"].Name);
        Console.WriteLine(doesThisWork["test2"].Name); 
    }
}
Yves Israel
  • 408
  • 3
  • 5