PC Review


Reply
Thread Tools Rate Thread

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

 
 
Neville Lang
Guest
Posts: n/a
 
      16th May 2005
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


 
Reply With Quote
 
 
 
 
Daniel Moth
Guest
Posts: n/a
 
      16th May 2005
Define an interface with the common methods and implement it in the two
classes.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


"Neville Lang" <neville@MAPS_ONnjlsoftware.com> wrote in message
news:ub%(E-Mail Removed)...
> 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
>
>


 
Reply With Quote
 
Neville Lang
Guest
Posts: n/a
 
      17th May 2005
Daniel,

Thank you for your information.

Regards,
Neville Lang



"Daniel Moth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Define an interface with the common methods and implement it in the two
> classes.
>
> Cheers
> Daniel
> --
> http://www.danielmoth.com/Blog/
>
>
> "Neville Lang" <neville@MAPS_ONnjlsoftware.com> wrote in message
> news:ub%(E-Mail Removed)...
> > 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
> >
> >

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference a variable declared in VBA James Microsoft Excel Worksheet Functions 3 12th Sep 2008 10:31 PM
Modify/Access private field from within the class through property or variable? Mark Microsoft C# .NET 1 25th Mar 2004 05:06 PM
variable type that can be declared in a base class this is accessible only to classes that inherit the base class Dan Microsoft ASP .NET 1 24th Mar 2004 09:34 PM
Scope of Variable Declared Like This? George Microsoft ASP .NET 7 17th Mar 2004 06:42 PM
Access DataGrid declared in derived class from base class?? Jonas Microsoft ASP .NET 1 12th Aug 2003 05:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:15 PM.