My.ADVISOR.com Sign-In
ID
Password

Member Center / Sign-Up
Go to Article
Advanced Search 

ADVISOR TIPS

Type Casting in Microsoft Visual Basic .NET

Visual Basic .NET provides two options for casting. Here's what you need to know to optimize your applications.

By Molly Pell, Technical Project Manager, FMS Inc.

Visual Basic .NET provides two options for casting. CType casts or converts one type into another type. If the types don't match, coercion may be performed. DirectCast casts one type to another type with better performance than CType, but doesn't perform coercion if the types don't match.

Note that CType includes all of the VB conversion functions. These are CBool, CByte, CChar, CDate, CDec, CDbl, CInt, CLng, CObj, CShort, CSng, and CStr.

The main difference between the two is that DirectCast only works if the specified type and the runtime type of the expression are the same. This difference only appears when you're converting from an object type to a value type, or unboxing a value. For instance, the following DirectCast operation will fail because the boxed type of the object O is not an integer:

Dim O As Object = 1.5
Dim I As Integer = DirectCast(O, Integer) ' Causes a run-time error.
Dim I As Integer = CType(O, Integer) ' Does not cause a run-time error.

The DirectCast operation in this example, on the other hand, will succeed:

Dim O As Object = 1
Dim I As Integer = DirectCast(O, Integer) ' Succeeds.
Dim I As Integer = CType(O, Integer) ' Succeeds, but is slower than the DirectCast

When you're converting from an object to a value type, or unboxing, the DirectCast operator has better performance than the CType operator. The Visual Basic .NET compiler generates four lines of IL code for DirectCast. However, using CType causes the Visual Basic .NET compiler to generate a call to a conversion method that's well over 100 lines of IL code. This method in turn calls other methods. In performance critical code, the difference can be substantial. If coercion is necessary, however, you must still use the CType operation, or perform the coercion manually. For example:

Dim O As Object = 1.5
Dim f As Single = DirectCast(O, Single) ' Cast to the runtime type
Dim i as Integer = CType(f,Integer) ' CType is OK here, since this cast does not unbox 'f'

To summarize, when you're type casting from an object type to a value type, or unboxing, you should first determine whether type coercion is necessary. If no coercion is necessary (i.e., the boxed type of the value is the same as the type specified in the cast expression), use the DirectCast expression to increase application performance.

This tip was detected with FMS Total .NET Analyzer. See http://www.fmsinc.com/dotnet/analyzer for more information.

What do YOU think about this topic? Share your advice and thoughts using this form.

Your Name

REQUIRED : PUBLIC

Your E-Mail

REQUIRED : PRIVATE

Job, Company

OPTIONAL : PUBLIC

City, State, Country

OPTIONAL : PUBLIC

Your Web Site

OPTIONAL : PUBLIC

Your Comment

Please help everyone by keeping your comments on-topic, using clean language, and not defaming or making personal attacks.


Your e-mail address is required, but it will not be displayed to the public or given to anyone. See our Privacy Policy. Comments become visible after they pass our spam filter, and spammers and abusers are permanently blocked. Please report spam or abuse.

Printer-friendly
page layout

Keyword Tags: Application Development, Microsoft, Microsoft Visual Basic, Microsoft .NET, Microsoft .NET Framework, Programming

ADVISORAMA
Love is an act of endless forgiveness, a tender look which becomes a habit.
-- Peter Ustinov, actor (1921-2004)

ARTICLE INFO

DataBased Advisor

Web Edition: 2003.07.25, Doc #12798

FREE ACCESS FREE ACCESS

SUBSCRIPTION STATUS
You are not signed-in. If you are a subscriber to this publication, sign-in above to access locked articles. To subscribe or renew go to www.AdvisorStore.com.

Subscribe to FileMaker Advisor Magazine

Read the advanced guide to creating custom business database solutions with FileMaker software. Subscribe now to gain access to all the archives and downloads.

FileMaker.Advisor.com

Subscribe to Advisor Basics of FileMaker Pro

Learn the fundamentals of using FileMaker Pro software. Every issue gives you step-by-step instructions on creating the databases you need. Subscribe now!

FileMaker.AdvisorBasics.com

Showcase Your Smarts

Submit your tips, techniques and advice and let Advisor promote your business and build your career. Show the world what you know!

AdvisorTips.com

Use of this or any other site, content, product or service of Advisor Media constitutes acceptance of Terms of Use.
Portions copyright ©1983-2008 Advisor Media, Inc. All Rights Reserved.
Reuse or reproduction of any portion or quantity of Advisor Media's copyrighted content, in any form, for any purpose, requires written permission.
ADVISOR®, the ADVISOR logo, and other names and logos that incorporate ADVISOR are registered trademarks, trademarks or service marks of Advisor Media, Inc. in the United States and/or other countries.
Other trademarks are used for identification, editorial or descriptive purposes and are the property of their owners.
Hosted by Prominic.NET Website powered by
LOTUS SOFTWARE
PELMO001 posted 07/25/2003 modified 12/03/2008 03:34:20 AM ztdbms/ztdbms
domino-144.advisor.com my.advisor.com 12/03/2008 04:55:43 PM