casting to a derived class

G

Guest

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).DoThis();
}
}


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());
}
 
J

Jon Skeet [C# MVP]

Steve Teeples said:
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?

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

Guest

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

Jon Skeet [C# MVP]

Steve Teeples said:
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.

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

Guest

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

Rick Elbers

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
 
G

Guest

Rick, what is visitor?

Rick Elbers said:
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
 
K

KJ

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

Rick Elbers

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
 
K

KJ

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

Guest

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top