gelensb

I have a question about how to create an array of a number of different inherited classes from the same base class. Let's say I have the following classes:

//base class
Class A {
public void foo { }
}

//inherited classes
Class B : A {
public void bar { }
}

Class C : A {
public void foo { }
}

Now I want to create an array which contains elements that can be either B or C. However, when I call the function foo(), I want it to call the C::foo() when it is a C element, and A::foo() when it is a B element.

I tried instantiating it in the following way:

Class D {
public D {
m_X = new A[5];
}
public A [] m_X;
}

But now I have an array of A elements that do not have memory allocated for being B or C, so I can't treat an element of m_X as a B or C. Furthermore I have to instantiate an element of m_X as a C element in order for the C::foo() to be called when I call m_X.foo().

I have the feeling I'm overlooking something very simple, but I can't find what. Could anyone help me with this


Re: Visual C# General an array of inherited classes

Vic Vega

Do you want to create a array of Objects (in above case which will B or C )

You can use Base Class Array and add objects of B and C as elements.

You can use virtual method ( foo ) in base class ..... So when you call foo on C object, it will go to derived class ( overide in edrived class )....

If you what to runtime checking of a object, then you can typeof or Is operator.

Let me if you have issues





Re: Visual C# General an array of inherited classes

gelensb

Yes I want to create an array of objects (B and C), but if I make an array of objects, I'll have to instantiate it like this:

A[] m_X;

m_X = new A();

Which will force foo() to A::foo(), not C::foo(), even if using a virtual foo() function. (and I don't think I can assign a C object to this array).

Base Class Array is an abstract class. I could implement this, but that would be a lot of extra work. Isn't there some simpler way to make an array of inherited classes





Re: Visual C# General an array of inherited classes

Matthew Watson

No, it will work fine. Consider this code:


public abstract class Base
{
public abstract string Foo();
}

public class Derived1: Base
{
public override string Foo()
{
return "Derived1";
}
}

public class Derived2: Base
{
public override string Foo()
{
return "Derived2";
}
}

private void button_Click(object sender, EventArgs e)
{
Base[] array = new Base[2];
array[0] = new Derived1(); // Derived1 goes in here.
array[1] = new Derived2(); // Derived2 goes in here.
MyMethod(array);
}

private void MyMethod(Base[] array)
{
foreach (Base b in array)
MessageBox.Show(b.Foo());
}







Re: Visual C# General an array of inherited classes

gelensb

Simple and clean, just how I like it Smile
Thanks for the answer!

I have a follow up question though: If a derived class always builds onto a base class, the derived class will always take up more memory (since it is the base class memory needed + additional memory needed for the derived class). Now if you instantiate an array of base class objects, isn't the memory assigned too little Or put in another way: if you instantiate a base class array, isn't the memory for the derived class variable missing

For instance in your example: if a class Base object needed 10 bytes, and a Derived1 class object 16 bytes, the array Base[2] would require 10 * 2 = 20 bytes. However for 2 Derived1 objects, you'd need 2 * 16 = 32 bytes.




Re: Visual C# General an array of inherited classes

sirjis

Since objects in C# are always references, the array is internally just an array of pointers, which have a constant size (32 or 64 bits depending on the processor). So when you write array[1] = new Derived1(); it is actually allocating the memory for the Derived1 object somewhere else on the heap and setting the array slot to point to that memory location.




Re: Visual C# General an array of inherited classes

gelensb

Thanks for the information!