469,348 Members | 1,682 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,348 developers. It's quick & easy.

directCast

Can I use DirectCast to convert a object to it's base or should I use CType?
I have a instance of a class as Object.
The run-time tipe is a derived of a class, but I need to refer to that
instance as to the base class

Ex: Class BaseCl
Class DerivCl
Inherits BaseCl

'Run-time
Dim thisCl as Object= new DerivCl
Can I use DirectCast(thisCl, BaseCl).baseClMethod ... ?
--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------
Nov 20 '05 #1
13 1707
AFAIK, yes.

CType does a *conversion* from one type to another (e.g. Integer to a
string)

DirectCast simply takes an object, and casts it to a derived or base or
interface type. For directcast to work, there must be a direct relationship
between the types. In your example, because "thisCl" contains "DerivC1" and
"DeriveC1" inherits from "BaseC1", there is a direct relationship.

HTH,

Trev.

"Crirus" <Cr****@datagroup.ro> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Can I use DirectCast to convert a object to it's base or should I use CType? I have a instance of a class as Object.
The run-time tipe is a derived of a class, but I need to refer to that
instance as to the base class

Ex: Class BaseCl
Class DerivCl
Inherits BaseCl

'Run-time
Dim thisCl as Object= new DerivCl
Can I use DirectCast(thisCl, BaseCl).baseClMethod ... ?
--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Nov 20 '05 #2
Can I use DirectCast to convert a object to it's base or should I use CType?


It doesn't matter which one you use in this context.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #3
I gues it does for speed sake

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Can I use DirectCast to convert a object to it's base or should I use
CType?
It doesn't matter which one you use in this context.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 20 '05 #4
"Crirus" <Cr****@datagroup.ro> schrieb
I gues it does for speed sake


Nope. If CType casts (not converts) it does *exactly* the same.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #5
Not in all cases.

Have the look at the IL for the following code (when compiled in release
mode):

-----------------
Sub Main()

Dim b As String = "hello"
Dim a As Object = b
Dim c As String

c = CType(a, String) ' Method 1
c = DirectCast(a, String) ' Method 2
c = CStr(a) ' Method 3

End Sub
------------
Both Method 1 and Method 3 do the same thing (a call to
Microsoft.VisualBasic.CompilerServices.StringType: :FromObject)

Method 2 (directcast) uses the "castclass" instruction directly.

Admittedly, the first thing
Microsoft.VisualBasic.CompilerServices.StringType: :FromObject seems to do is
check if it is already a string type and then call "castclass" and return,
but there is still a small overhead in calling the extra function in the
first place.

Not that optimizations like this will make much difference unless involved
in a mad loop of some sort. Still, I think it is better for the developer to
know the difference between when they *can* use directcast and when they
*have* to use ctype.

HTH,

Trev.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:OF**************@TK2MSFTNGP11.phx.gbl...
"Crirus" <Cr****@datagroup.ro> schrieb
I gues it does for speed sake


Nope. If CType casts (not converts) it does *exactly* the same.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6
"Trev Hunter" <hu*********@hotmail.com> schrieb
Not in all cases.


I thought it was clear that my answer is based upon the usage of the
keywords in the same situation:

In a Form:
Dim o As Object, f As Form1
o = Me
f = CType(o, Form1)
f = DirectCast(o, Form1)

The casts compile to:

IL_0002: ldloc.1
IL_0003: castclass WindowsApplication280.Form1
IL_0008: stloc.0

IL_0009: ldloc.1
IL_000a: castclass WindowsApplication280.Form1
IL_000f: stloc.0
Of course, you are right, this is not true in all cases. So, I should have
added: If it's unambigous at compile time that it will be a cast, not a
conversion, the result is the same.

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7
> If it's unambigous at compile time that it will be a cast, not a
conversion, the result is the same.


I agree. It's nice to know that the VB compiler is smart enough in these situations. I still think it's important for developers to use directcast whenever possible if for the only reason that it makes them more aware of the type conversions / castings that are taking place. IME, there are too many VB programmers out there that have taken too much advantage of the late binding, variant, option strict off nature of previous VB versions.

Just my 2 worthless euro cents ;)

Trev.

Nov 20 '05 #8
"Trev Hunter" <hu*********@hotmail.com> schrieb

Just my 2 worthless euro cents ;)


Never had as much worth as now. :)
--
Armin

Nov 20 '05 #9
> Never had as much worth as now. :)

lol. Yeah I hope their worth goes up quite a bit in the next month - I'm
moving to Canadian cents soon - the more they're worth at the beginning of
March, the better ;)

Trev.

"Armin Zingler" <az*******@freenet.de> wrote in message
news:Oc*************@tk2msftngp13.phx.gbl...
"Trev Hunter" <hu*********@hotmail.com> schrieb

Just my 2 worthless euro cents ;)


Never had as much worth as now. :)
--
Armin

Nov 20 '05 #10
"Trev Hunter" <hu*********@hotmail.com> schrieb
Never had as much worth as now. :)


lol. Yeah I hope their worth goes up quite a bit in the next month -
I'm moving to Canadian cents soon - the more they're worth at the
beginning of March, the better ;)


Get you a German car before moving! It's getting expensive over there. ;-)
--
Armin

Nov 20 '05 #11
> Get you a German car before moving! It's getting expensive over there. ;)

Lol! Definetly - no way am I gonna drive one of those floaty yank tanks
everybody loves over there - would rather get a mountain bike and ride it in
the -30 degree weather :S

I've heard it's getting expensive, but it has to be better than the rip-off
country that Ireland has become in the last few years. Thanks god my last
few pay cheques have been in British sterling over the last while ;)

Trev

"Armin Zingler" <az*******@freenet.de> wrote in message
news:e4**************@TK2MSFTNGP12.phx.gbl...
"Trev Hunter" <hu*********@hotmail.com> schrieb
Never had as much worth as now. :)
lol. Yeah I hope their worth goes up quite a bit in the next month -
I'm moving to Canadian cents soon - the more they're worth at the
beginning of March, the better ;)


Get you a German car before moving! It's getting expensive over there.

;-)

--
Armin

Nov 20 '05 #12
When you are inheriting from a base class, the methods of the base class
can automatically be obtained through the Me keyword -

Me.bclMethod()

CType("thisCl",BaseCl) will work as well returning an object of type
BaseCl.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #13
"Trev Hunter" <hu*********@hotmail.com> schrieb
Get you a German car before moving! It's getting expensive over
there. ;)


Lol! Definetly - no way am I gonna drive one of those floaty yank
tanks everybody loves over there - would rather get a mountain bike
and ride it in the -30 degree weather :S

I've heard it's getting expensive, but it has to be better than the
rip-off country that Ireland has become in the last few years. Thanks
god my last few pay cheques have been in British sterling over the
last while ;)

Trev


:-))
--
Armin

Nov 20 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Sahil Malik | last post: by
11 posts views Thread by Tubs | last post: by
6 posts views Thread by Ot | last post: by
6 posts views Thread by Mark Nethercott | last post: by
7 posts views Thread by Brian Henry | last post: by
1 post views Thread by iwdu15 | last post: by
3 posts views Thread by =?Utf-8?B?TWlrZQ==?= | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.