473,396 Members | 1,774 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,396 software developers and data experts.

casting an object

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
Nov 21 '05 #1
6 3609
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" <ro*****@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.com...
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

Nov 21 '05 #2
"rodchar" <ro*****@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.com...
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.
Nov 21 '05 #3
"rodchar" <ro*****@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> schrieb:
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...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
thank you everyone for being so helpful. i appreciate it :)

"rodchar" wrote:
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

Nov 21 '05 #6
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
"rodchar" <ro*****@discussions.microsoft.com> escribió en el mensaje
news:9C**********************************@microsof t.com...
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

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
1
by: madumm | last post by:
Hi all I really need a solution for the following:: ------------------------------------------------------------------------------------- Say i have a font object called 'myFont' as shown below;...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.