473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

--
Steve
Nov 17 '05 #1
12 3596
Steve Teeples <St**********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
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:
Steve Teeples <St**********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
Steve Teeples <St**********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
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:
Steve Teeples <St**********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5
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"
<St**********@discussions.microsoft.com> wrote:
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());
}


Nov 17 '05 #6
Rick, what is visitor?

"Rick Elbers" wrote:
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"
<St**********@discussions.microsoft.com> wrote:
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());
}


Nov 17 '05 #7
Steve Teeples <St**********@discussions.microsoft.com> wrote:
Rick, what is visitor?


See

http://exciton.cs.oberlin.edu/javare...s/VisitorPatte
rn.htm

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Thanks.
--
Steve
"Jon Skeet [C# MVP]" wrote:
Steve Teeples <St**********@discussions.microsoft.com> wrote:
Rick, what is visitor?


See

http://exciton.cs.oberlin.edu/javare...s/VisitorPatte
rn.htm

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #9
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.

Nov 17 '05 #10
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_**********@mail.com> wrote:
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.


Nov 17 '05 #11
KJ
How is casting to a common interface an implementation of Visitor? The
problem he is having is one of design.

Nov 17 '05 #12
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:
How is casting to a common interface an implementation of Visitor? The
problem he is having is one of design.

Nov 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
7
by: James Brown | last post by:
I have two classes, a base class, and a class base { public: base(); virtual void foo() = 0; }; class derived : public base
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
1
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
5
by: Chris Capon | last post by:
Is there any way to cast a base class object to a derived class datatype when the derived class adds no new fields nor alters the object allocation in any way? The scenario would be where you...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
13
by: Rahul | last post by:
Hi Everyone, I was just playing around virtual functions and landed up with the following, class Base1 { public: virtual void sample() { printf("base::sample\n");
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.