casting an object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey all,

given: working with the outlook object model.

Private Sub _oItems_ItemAdd(ByVal Item As Object) Handles _oItems.ItemAdd
MessageBox.Show("You've got mail.")
If TypeOf Item Is Outlook.MailItem Then
_oMsg = DirectCast(Item, Outlook.MailItem)
MessageBox.Show(_oMsg.Subject)
End If
End Sub

I was wondering if I used DirectCast in the correct context. if so, what are
the benefits of doing this?

thanks,
rodchar
 
You are using it correctly. The benefit of using directcast is it helps the
compiler know how to optimize the code, otherwise it has to figure out how
to convert the object at runtime. Depending on the rest of your program and
what you are doing, it may be better to overload the sub procedure so you
are not using late binding

Private Sub _oItems_ItemAdd(ByVal Item As Outlook.MailItem) Handles
_oItems.ItemAdd
MessageBox.Show("You've got mail.")
MessageBox.Show(Item.Subject)
End Sub

Private Sub _oItems_ItemAdd(ByVal Item As Outlook.SomeOtherItem) Handles
_oItems.ItemAdd
'Do Something Different
End Sub
 
rodchar said:
given: working with the outlook object model. .. . .
If TypeOf Item Is Outlook.MailItem Then
_oMsg = DirectCast(Item, Outlook.MailItem)
MessageBox.Show(_oMsg.Subject)
End If .. . .
I was wondering if I used DirectCast in the correct context. if so, what are
the benefits of doing this?

Perfectly correct, although you could also do

MessageBox.Show(DirectCast(Item, Outlook.MailItem).Subject)

CType() will try to "work out how" (at /run-time/) to convert the
given object from one Type to another. Whilst this is flexible, it
/can/ be slow, especially if, as in this case, you already know
exactly what type of object you're looking at.

DirectCast just says to the /compiler/,

"Look; I *know* what this is; just treat it as <this Type>"

If, at run-time, the Framework finds the value you supply "won't go",
it will still throw an Exception - but it'll [probably] do so quicker ;-)

HTH,
Phill W.
 
rodchar said:
given: working with the outlook object model.

Private Sub _oItems_ItemAdd(ByVal Item As Object) Handles
_oItems.ItemAdd
MessageBox.Show("You've got mail.")
If TypeOf Item Is Outlook.MailItem Then
_oMsg = DirectCast(Item, Outlook.MailItem)
MessageBox.Show(_oMsg.Subject)
End If
End Sub

I was wondering if I used DirectCast in the correct context. if so, what
are
the benefits of doing this?

The main benefit of doing that is that you can turn on 'Option Strict On'
and enforce strict typing. As an additional benefit, IntelliSense will show
you the methods of the 'MailItem' object and calling them will use early
binding instead of (slower) late binding.
 
Phill. W said:
MessageBox.Show(DirectCast(Item, Outlook.MailItem).Subject)

CType() will try to "work out how" (at /run-time/) to convert the
given object from one Type to another. Whilst this is flexible, it
/can/ be slow, especially if, as in this case, you already know
exactly what type of object you're looking at.

DirectCast just says to the /compiler/,

In this particular case (value types) the compiler will create the exact
same IL for both, 'CType' and 'DirectCast', so the performance is equal. I
prefer 'DirectCast' in this case to make it more obvoius that only a type
cast is going on and not a type conversion.

Just my 2 Euro cents...
 
Hi rodchar,

Yes, it's correct. The benefit is that DirectCast is faster than CType, but
can be used only when the casting object is already of the target type
(class or subclass, interface, etc.) as in you case. For example, since a
String is not an Integer, this fails:

i = DirectCast("3", Integer)

but this succeeds:

i = CType("3", Integer)

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 

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

Back
Top