object question

  • Thread starter Thread starter PCH
  • Start date Start date
P

PCH

posted on wrong ng :-)

Hello all,

I want to setup a program that will have several similar classes that with
have identitcal properties.

Im my main procedure I want to only declare 1 basic object that can be used
for any of the classes.

such as:

object oObj;

oObj = new Class1();

...run some code...

oObj = new Class2();

...run some code...

the problem is I cant reference any of the new objects properties.

such as:

oObj.MyProp();

I think this is because the original declaration of the oObj object is just
a standard object, and it has no definitions you can use.

is there a way to allow it to use the correct class calls.. such as
oObj(MyProp), etc etc?

my goal is to be able to loop and if the variables call for a different
class to use. i can use the same base object for any of them.

Thanks.
 
Hi PCH,

You can derive your Class1, Class2, etc from a base class that has the
method required.

public class BaseClass
{
....
public void MyProp(); // it could be also virtual
}

public class Class1: BaseClass
{
....
}
public class Class2: BaseClass
{
....
}
then you could do:

BaseClass oObj = new Class1();
oObj.MyProp();
BaseClass oObj = new Class2();
oObj.MyProp();

The other solution would be that your classes implement the same
interface...
 
thanks ill try that out!

Miha Markic said:
Hi PCH,

You can derive your Class1, Class2, etc from a base class that has the
method required.

public class BaseClass
{
...
public void MyProp(); // it could be also virtual
}

public class Class1: BaseClass
{
...
}
public class Class2: BaseClass
{
...
}
then you could do:

BaseClass oObj = new Class1();
oObj.MyProp();
BaseClass oObj = new Class2();
oObj.MyProp();

The other solution would be that your classes implement the same
interface...
 
Back
Top