Question

I have one abstract class named A, and other classes (B, C, D, E, ...) that implements A. My derived classes are holding values of different types. I also have a list of A objects.

    abstract class A { }
    class B : class A
    {
      public int val {get;private set;}
    }
    class C : class A
    {
      public double val {get;private set;}
    }
    class D : class A
    {
      public string val {get;private set;}
    }

    class Program
    {
        static void Main(string[] args)
        {
          List list = new List { new B(), new C(), new D(), new E() };
          // ... 

          foreach (A item in list)
          {
            Console.WriteLine(String.Format("Value is: {0}", item.val);
          }
        }
    }

...where the .val is not known by the base-class ofc.

How can i get this dynamic behaviour? I don't want to use getType in a long switch/if-statements.

Was it helpful?

Solution 2

Try this:

using System;
using System.Collections.Generic;

abstract class A
{
    public abstract dynamic Val { get;   set; }

}
class B : A
{
    public override dynamic Val { get;  set; }
}
class C : A
{
    public override dynamic Val { get;  set; }
}
class D : A
{
    public override dynamic Val { get;  set; }
}

class Program
{
    static void Main(string[] args)
    {
        var list = new List<A> { new B(), new C(), new D() };
        // ... 

        foreach (A item in list)
        {
            Console.WriteLine(String.Format("Value is: {0}", item.Val));
        }
    }
}

OTHER TIPS

If you just want to get the string representation of val i recoment overriding ToString in each of the sub classes

public override string ToString()
{
    return val.ToString();
}

either way if you want the data in a sub class you need to represent it in the base class as some type they all have in common (like object). and you could do it like this

abstract class A 
{
    public abstract object GetValue();
}
class B : class A
{
    public int val {get;private set;}
    public override object GetValue()
    {
        return val;
    }
}
 abstract class A { public string val { get; set; } }
class B :  A
{
  public int val {get;private set;}
}
class C :  A
{
  public double val {get;private set;}
}
class D :  A
{
  public string val {get;private set;}
}

class Program
{
    static void Main(string[] args)
    {
        List<object> list = new List<object> { new B(), new C(), new D() };
      // ... 

      foreach (A item in list)
      {
        Console.WriteLine(String.Format("Value is: {0}", item.val));
      }
    }
}

If val is not a member of the base class then you can't access it on a reference of that type. Those three derived classes may all have a member named val but it is NOT the same member so it cannot be treated like it is. What you could do is declare a generic class and make that val property of the generic type but then you couldn't create a List of that type. Basically, what you want to do is not possible. It's not based on inheritance and it's not based on generics. It sounds convenient but it's not logical.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top