472,328 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Best way to "cast" base class to specialized class

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
6 11118
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated>...
0
by: raca | last post by:
I am trying to create a generic SOA ServiceInvoker that will accept an XML string that will be used to deserialize an object generated by...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has...
2
by: Won Won | last post by:
I am writing a click event for an array of PictureBoxes: for (int i = 0; i < 10; i++) pic.Click += new EventHandler (pic_Click); private void...
0
by: Peter Afonin | last post by:
Hello: When I try to access a SQL server or a network share from an ASP.Net application that I run on my computer, I run into security problems...
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits...
3
by: hazz | last post by:
I get an InvalidCastException "Cast from string "6<5" to type 'Boolean is not valid. for If (CInt(objBuyInfo.Budget) & " " & ...
2
by: Matthias Langbein | last post by:
Hi all, i want to save the position of my Addin-Toolbar to the registry. It's easy to store the position as a string with...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.