object question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PCH

    #1

    object question

    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.



  • Miha Markic

    #2
    Re: object question

    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...

    --
    Miha Markic - RightHand .NET consulting & development
    miha at rthand com


    "PCH" <pch12@hotmail. com> wrote in message
    news:%23dcfE1sq DHA.2588@tk2msf tngp13.phx.gbl. ..[color=blue]
    > 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[/color]
    used[color=blue]
    > 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[/color]
    just[color=blue]
    > 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.
    >
    >
    >[/color]


    Comment

    • PCH

      #3
      Re: object question

      thanks ill try that out!

      "Miha Markic" <miha at rthand com> wrote in message
      news:%230sLhftq DHA.392@TK2MSFT NGP11.phx.gbl.. .[color=blue]
      > 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...
      >
      > --
      > Miha Markic - RightHand .NET consulting & development
      > miha at rthand com
      >
      >
      > "PCH" <pch12@hotmail. com> wrote in message
      > news:%23dcfE1sq DHA.2588@tk2msf tngp13.phx.gbl. ..[color=green]
      > > posted on wrong ng :-)
      > >
      > > Hello all,
      > >
      > > I want to setup a program that will have several similar classes that[/color][/color]
      with[color=blue][color=green]
      > > have identitcal properties.
      > >
      > > Im my main procedure I want to only declare 1 basic object that can be[/color]
      > used[color=green]
      > > 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[/color]
      > just[color=green]
      > > 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.
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...