casting to a derived class

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

    #1

    casting to a derived class

    Can someone tell me the correct method of casting and object at run time.
    Here is a sample of what I'm trying to do. classA acts as a base for classes
    that execute code in classes derived from ProBase. How do I property cast
    the derived classes from ProBase during runtime so that I can access their
    unique methods?

    class abstract classA
    {
    public classA(ProBase pb)
    {
    this.Execute(pb );
    }
    public abstract void Execute(ProBase bp);
    }

    class classB : classA
    {
    public classB(ProBase bp) : classA(bp) { }
    public override void Execute(ProBase bp)
    {
    System.Type type = bp.GetType();
    ((type)bp).DoIt ();
    }
    }
    class classC : classA
    {
    public classC(ProBase bp) : classA(bp) { }
    public override void Execute(ProBase bp)
    {
    System.Type type = bp.GetType();
    ((type)bp).DoTh is();
    }
    }


    class ProBase
    {
    public ProBase() { }
    }

    class ProBaseA : ProBase
    {
    public ProBaseA() : ProBase() {}
    public DoIt()
    {
    .... do something;
    }
    }

    class ProBaseB : ProBase
    {
    public ProBaseB() : ProBase() {}
    public DoThis()
    {
    .... do something;
    }
    }

    main()
    {
    classB b = new classB(new ProBaseA());
    classC c = new classC(new ProBaseB());
    }

    --
    Steve
  • Jon Skeet [C# MVP]

    #2
    Re: casting to a derived class

    Steve Teeples <SteveTeeples@d iscussions.micr osoft.com> wrote:[color=blue]
    > Can someone tell me the correct method of casting and object at run time.
    > Here is a sample of what I'm trying to do. classA acts as a base for classes
    > that execute code in classes derived from ProBase. How do I property cast
    > the derived classes from ProBase during runtime so that I can access their
    > unique methods?[/color]

    If you know the methods at compile time, you must know which class is
    involved, so just cast to that class. In classB you would do:

    ((ProBaseA)bp). DoIt();

    and in classC you would do:

    ((ProBaseB)bp). DoThis();

    Have a think about a way round needing to do this in the first place -
    there are usually better ways of working than that.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Steve Teeples

      #3
      Re: casting to a derived class

      Jon,

      There are more than 10 derived classes for ProBase so any of those classes
      could be passed into this single routine during run time in any order. Is
      there no way of doing this? If not, do you know of a better method? I'm
      always game to learn better approaches.

      "Jon Skeet [C# MVP]" wrote:
      [color=blue]
      > Steve Teeples <SteveTeeples@d iscussions.micr osoft.com> wrote:[color=green]
      > > Can someone tell me the correct method of casting and object at run time.
      > > Here is a sample of what I'm trying to do. classA acts as a base for classes
      > > that execute code in classes derived from ProBase. How do I property cast
      > > the derived classes from ProBase during runtime so that I can access their
      > > unique methods?[/color]
      >
      > If you know the methods at compile time, you must know which class is
      > involved, so just cast to that class. In classB you would do:
      >
      > ((ProBaseA)bp). DoIt();
      >
      > and in classC you would do:
      >
      > ((ProBaseB)bp). DoThis();
      >
      > Have a think about a way round needing to do this in the first place -
      > there are usually better ways of working than that.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: casting to a derived class

        Steve Teeples <SteveTeeples@d iscussions.micr osoft.com> wrote:[color=blue]
        > There are more than 10 derived classes for ProBase so any of those classes
        > could be passed into this single routine during run time in any order. Is
        > there no way of doing this? If not, do you know of a better method? I'm
        > always game to learn better approaches.[/color]

        Again, if you know which method you need to call, you must know the
        type at compile time, so just cast to that type.

        Do the methods really have no common purpose? Usually you'd put an
        abstract method in the base class, but without knowing the real
        situation, it's hard to say the best way to proceed.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Steve Teeples

          #5
          Re: casting to a derived class

          Looking at it again I realized (thanks for helping) that I can typecast each
          class to the common base class that shares the astract methods. Thanks for
          helping me take a closer look at things.

          "Jon Skeet [C# MVP]" wrote:
          [color=blue]
          > Steve Teeples <SteveTeeples@d iscussions.micr osoft.com> wrote:[color=green]
          > > There are more than 10 derived classes for ProBase so any of those classes
          > > could be passed into this single routine during run time in any order. Is
          > > there no way of doing this? If not, do you know of a better method? I'm
          > > always game to learn better approaches.[/color]
          >
          > Again, if you know which method you need to call, you must know the
          > type at compile time, so just cast to that type.
          >
          > Do the methods really have no common purpose? Usually you'd put an
          > abstract method in the base class, but without knowing the real
          > situation, it's hard to say the best way to proceed.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too
          >[/color]

          Comment

          • Rick Elbers

            #6
            Re: casting to a derived class

            Steve,

            What you are trying to do is double dispatch.
            Another implementation is to use visitor.
            Make your Probase-subclasses know its own execute method
            being it DoIt or DoThis.

            Rick


            On Tue, 17 May 2005 10:51:10 -0700, "Steve Teeples"
            <SteveTeeples@d iscussions.micr osoft.com> wrote:
            [color=blue]
            >Can someone tell me the correct method of casting and object at run time.
            >Here is a sample of what I'm trying to do. classA acts as a base for classes
            >that execute code in classes derived from ProBase. How do I property cast
            >the derived classes from ProBase during runtime so that I can access their
            >unique methods?
            >
            >class abstract classA
            >{
            > public classA(ProBase pb)
            > {
            > this.Execute(pb );
            > }
            > public abstract void Execute(ProBase bp);
            >}
            >
            >class classB : classA
            >{
            > public classB(ProBase bp) : classA(bp) { }
            > public override void Execute(ProBase bp)
            > {
            > System.Type type = bp.GetType();
            > ((type)bp).DoIt ();
            > }
            >}
            >class classC : classA
            >{
            > public classC(ProBase bp) : classA(bp) { }
            > public override void Execute(ProBase bp)
            > {
            > System.Type type = bp.GetType();
            > ((type)bp).DoTh is();
            > }
            >}
            >
            >
            >class ProBase
            >{
            > public ProBase() { }
            >}
            >
            >class ProBaseA : ProBase
            >{
            > public ProBaseA() : ProBase() {}
            > public DoIt()
            > {
            > .... do something;
            > }
            >}
            >
            >class ProBaseB : ProBase
            >{
            > public ProBaseB() : ProBase() {}
            > public DoThis()
            > {
            > .... do something;
            > }
            >}
            >
            >main()
            >{
            > classB b = new classB(new ProBaseA());
            > classC c = new classC(new ProBaseB());
            >}[/color]

            Comment

            • Steve Teeples

              #7
              Re: casting to a derived class

              Rick, what is visitor?

              "Rick Elbers" wrote:
              [color=blue]
              > Steve,
              >
              > What you are trying to do is double dispatch.
              > Another implementation is to use visitor.
              > Make your Probase-subclasses know its own execute method
              > being it DoIt or DoThis.
              >
              > Rick
              >
              >
              > On Tue, 17 May 2005 10:51:10 -0700, "Steve Teeples"
              > <SteveTeeples@d iscussions.micr osoft.com> wrote:
              >[color=green]
              > >Can someone tell me the correct method of casting and object at run time.
              > >Here is a sample of what I'm trying to do. classA acts as a base for classes
              > >that execute code in classes derived from ProBase. How do I property cast
              > >the derived classes from ProBase during runtime so that I can access their
              > >unique methods?
              > >
              > >class abstract classA
              > >{
              > > public classA(ProBase pb)
              > > {
              > > this.Execute(pb );
              > > }
              > > public abstract void Execute(ProBase bp);
              > >}
              > >
              > >class classB : classA
              > >{
              > > public classB(ProBase bp) : classA(bp) { }
              > > public override void Execute(ProBase bp)
              > > {
              > > System.Type type = bp.GetType();
              > > ((type)bp).DoIt ();
              > > }
              > >}
              > >class classC : classA
              > >{
              > > public classC(ProBase bp) : classA(bp) { }
              > > public override void Execute(ProBase bp)
              > > {
              > > System.Type type = bp.GetType();
              > > ((type)bp).DoTh is();
              > > }
              > >}
              > >
              > >
              > >class ProBase
              > >{
              > > public ProBase() { }
              > >}
              > >
              > >class ProBaseA : ProBase
              > >{
              > > public ProBaseA() : ProBase() {}
              > > public DoIt()
              > > {
              > > .... do something;
              > > }
              > >}
              > >
              > >class ProBaseB : ProBase
              > >{
              > > public ProBaseB() : ProBase() {}
              > > public DoThis()
              > > {
              > > .... do something;
              > > }
              > >}
              > >
              > >main()
              > >{
              > > classB b = new classB(new ProBaseA());
              > > classC c = new classC(new ProBaseB());
              > >}[/color]
              >
              >[/color]

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: casting to a derived class

                Steve Teeples <SteveTeeples@d iscussions.micr osoft.com> wrote:[color=blue]
                > Rick, what is visitor?[/color]

                See


                rn.htm

                (A Google search for "visitor pattern" gives a number of hits.)

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Steve Teeples

                  #9
                  Re: casting to a derived class

                  Thanks.
                  --
                  Steve


                  "Jon Skeet [C# MVP]" wrote:
                  [color=blue]
                  > Steve Teeples <SteveTeeples@d iscussions.micr osoft.com> wrote:[color=green]
                  > > Rick, what is visitor?[/color]
                  >
                  > See
                  >
                  > http://exciton.cs.oberlin.edu/javare...s/VisitorPatte
                  > rn.htm
                  >
                  > (A Google search for "visitor pattern" gives a number of hits.)
                  >
                  > --
                  > Jon Skeet - <skeet@pobox.co m>
                  > http://www.pobox.com/~skeet
                  > If replying to the group, please do not mail me too
                  >[/color]

                  Comment

                  • KJ

                    #10
                    Re: casting to a derived class

                    I'm not Rick, but Visitor is a design pattern that you don't need here.
                    You need an interface. Just cast any of the derived classes to the
                    interface, which they all implement. The actual implementation is what
                    gets called at runtime, which is unique to every class that implements.


                    You could even also use a factory method (another Design Pattern) to
                    generate the specific types of instances you want via a string. But I
                    wouldn't do that here.

                    Comment

                    • Rick Elbers

                      #11
                      Re: casting to a derived class

                      Kj,

                      You are describing another implementation of a visitor, which
                      would work if the OP has polymorph methods, which he has not
                      ( he has DoThis() and DoThat())

                      Creational Patterns have nothing to do with the problem at hand, sorry

                      Rick

                      Thats

                      On 17 May 2005 20:29:19 -0700, "KJ" <n_o_s_p_a__m@m ail.com> wrote:
                      [color=blue]
                      >I'm not Rick, but Visitor is a design pattern that you don't need here.
                      >You need an interface. Just cast any of the derived classes to the
                      >interface, which they all implement. The actual implementation is what
                      >gets called at runtime, which is unique to every class that implements.
                      >
                      >
                      >You could even also use a factory method (another Design Pattern) to
                      >generate the specific types of instances you want via a string. But I
                      >wouldn't do that here.[/color]

                      Comment

                      • KJ

                        #12
                        Re: casting to a derived class

                        How is casting to a common interface an implementation of Visitor? The
                        problem he is having is one of design.

                        Comment

                        • Steve Teeples

                          #13
                          Re: casting to a derived class

                          To address this issue I created abstract classes between the ProBase and
                          derived classes A & B. The abstract classes contain abstract methods of the
                          derived A, B classes. I cast it now the the intermediate classes and it
                          works. I'm interested in KJ's thought of using Interfaces. I'm still a
                          relatively new C# user and am not yet familiar with all its features. I'd
                          like to thank all of you for your input. I have found it very helpful!
                          --
                          Steve


                          "KJ" wrote:
                          [color=blue]
                          > How is casting to a common interface an implementation of Visitor? The
                          > problem he is having is one of design.
                          >
                          >[/color]

                          Comment

                          Working...