directCast

C

Crirus

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 ... ?
 
T

Trev Hunter

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.
 
T

Trev Hunter

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.
 
A

Armin Zingler

Trev Hunter said:
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.
 
G

Guest

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.
 
T

Trev Hunter

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.
 
A

Armin Zingler

Trev Hunter said:
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. ;-)
 
T

Trev Hunter

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
 
R

Ravichandran J.V.

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
 
A

Armin Zingler

Trev Hunter said:
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

:))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top