By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,368 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,368 IT Pros & Developers. It's quick & easy.

Best way to "cast" base class to specialized class

P: n/a
SA
Hi all:

I have an object of a base class that needs to be cast to an object of a
specialized class.

What is the best way to do this? (I thought about creating a constructor in
the specialized class that takes an argument of the type of the base class
and then copy property values over, but that seems like a hassle)

--
Sven.
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"SA" <in*********@freemail.nl> schrieb:
I have an object of a base class that needs to be cast to an object of a
specialized class.


We had a discussion about a similar topic going on in this group some weeks
ago:

<URL:http://groups.google.de/groups?selm=%23briLUiNFHA.2136%40TK2MSFTNGP14.phx. gbl>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

P: n/a
On 2005-04-21, SA <in*********@freemail.nl> wrote:
Hi all:

I have an object of a base class that needs to be cast to an object of a
specialized class.

What is the best way to do this? (I thought about creating a constructor in
the specialized class that takes an argument of the type of the base class
and then copy property values over, but that seems like a hassle)


The first step would be to get the nomenclature right. The above
wouldn't cast the object, it would create a new object. If that's what
you want, a constructor or some kind of factory pattern is a reasonable
way to go, a Herfried's post contains a link to lots of info about this.

If casting is really what you want, VB.Net has a cast operator:

DirectCast(MyBaseObject, MySubClass)


Nov 21 '05 #3

P: n/a
SA
David:

DirectCast (or CType) doesn't work... That only works from subclass to base
class, but not the other way around.

--
Sven.

"David" <df*****@woofix.local.dom> wrote in message
news:slrnd6k7pg.7op.df*****@woofix.local.dom...
On 2005-04-21, SA <in*********@freemail.nl> wrote:
Hi all:

I have an object of a base class that needs to be cast to an object of a
specialized class.

What is the best way to do this? (I thought about creating a constructor in the specialized class that takes an argument of the type of the base class and then copy property values over, but that seems like a hassle)


The first step would be to get the nomenclature right. The above
wouldn't cast the object, it would create a new object. If that's what
you want, a constructor or some kind of factory pattern is a reasonable
way to go, a Herfried's post contains a link to lots of info about this.

If casting is really what you want, VB.Net has a cast operator:

DirectCast(MyBaseObject, MySubClass)

Nov 21 '05 #4

P: n/a
SA
Herfried:

Thanks. Role pattern is not what I am after, so the constructor idea will
have to do then.

--
Sven.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:#X**************@tk2msftngp13.phx.gbl...
"SA" <in*********@freemail.nl> schrieb:
I have an object of a base class that needs to be cast to an object of a
specialized class.
We had a discussion about a similar topic going on in this group some

weeks ago:

<URL:http://groups.google.de/groups?selm=...TK2MSFTNGP14.p
hx.gbl>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

P: n/a
On 2005-04-23, SA <in*********@freemail.nl> wrote:
David:

DirectCast (or CType) doesn't work... That only works from subclass to base
class, but not the other way around.


No, that's not true, which is why I said you should understand the
nomenclature before trying to understand the options.

Like I said, if what you really want is a new object, then forget any
kind of analogy to "casting" and the problem gets a lot easier.

Nov 21 '05 #6

P: n/a
SA
David:

Not sure how the nomenclature could get confusing... I don't want a new
object, but I end up creating one because casting an object from the base
class to a sub class is not supported.

Public Class A
End Class

Public Class B
Inherits A
End Class

Dim objA As New A
Dim objB As New B

You can write

objA = objB
objA = DirectCast(objB, A)
objA = CType(objB, A)

but not

B = A
objB = DirectCast(objA, B)
objB = CType(objA, B)

Note that no compiler warnings occur, but there are runtime exceptions
thrown (InvalidCastException). So, yes, it *is* true that you can't cast an
object from a base class to an object of a subclass of that base class.

In the end, that makes sense, because you could get around any restrictions
on required property values that have been imposed on objects of the
subclass type of course... Still, it would be nice to be able to just cast.

--
Sven.

"David" <df*****@woofix.local.dom> wrote in message
news:slrnd6kp5l.8c7.df*****@woofix.local.dom...
On 2005-04-23, SA <in*********@freemail.nl> wrote:
David:

DirectCast (or CType) doesn't work... That only works from subclass to base class, but not the other way around.


No, that's not true, which is why I said you should understand the
nomenclature before trying to understand the options.

Like I said, if what you really want is a new object, then forget any
kind of analogy to "casting" and the problem gets a lot easier.

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.