473,320 Members | 1,707 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,320 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 11239
"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> derived class pointer in a generalised fashion? ...
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 XSDObjectGen. The hierarchy goes like this:...
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 occurred during behind-the-scenes cleanup of some...
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 pic_Click (object sender, System.EventArgs e) In...
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 (for instance, I cannot execute DTS package using...
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 System.Windows.Forms.DataGridTextBoxColumn End Class There is...
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) & " " & arrRuleList(i).operator & " " &...
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 MsoBarPosition.toString(). But when I read the key, I get it as a...
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.