I am trying to utlise polymorphism for class instances inside an Object array:
ClassA a = new ClassA();
ClassB b = new ClassB();
Object[] classes = new Object[] { a, b };
An example of one of the classes would be:
public class ClassA
{
public string PrintOutput()
{
return "254,62,455,5,15,62,656";
}
}
I've tried something similar to: C# class polymorphism
The issue this person had was that they created an array of type A (one of the classes). Changing the array type to Object leads to the error: object not containing a definition for the method/no accessible extension method accepting a first argument of type object could be found.
EDIT: At the moment, I'm just trying to print the outputs from the method in each class:
foreach (Object obj in classes)
{
Console.WriteLine(obj.PrintOutput());
}
obj.PrintOutput() is where I get the above error.
classesand call method or what? Can you show the code which doesn't work and the error you get?