How can a field variable be declared as one class or another?

  • Thread starter Thread starter Neville Lang
  • Start date Start date
N

Neville Lang

Hi all,

I have a question that I have a mental block on at the moment.

I have two classes which are similar, both containing the same method names
and properties. One class basically constructs the details for a bitmap
graphic based on circles while the other class constructs the details for a
bitmap based on rectangles.

In a controlling class, I have a field variable that can be either one or
the other class mentioned above, based on a setting. I want to use the one
field variable in various parts of the controlling class but the C# compiler
is complaining.

Here is logically what I want to achieve:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private Object aaa;

if (condition)
{
MyClass1 aaa = new MyClass1();
}
else
{
MyClass2 aaa = new MyClass2();
}
....
....

aaa.Top = 2;
aaa.Left = 2;
....
....
aaa.Method1();
....
....
aaa.Method2();
....
....
....
aaa.Method3()

and so on.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Is there a way I can only use the one field variable for the method calls?
The only other way I can think of at the moment is use IF()s everywhere in
the controlling class. I want to avoid that if possible. Is there a simpler
solution?


Regards,
Neville Lang
 
Define an interface with the common methods and implement it in the two
classes.

Cheers
Daniel
 
Back
Top