.NET Tip: Type Casting and Comparison Using "as" and "is" Atlanta GA

Use some of the language's lesser known features for safe type casting and comparison.

Local Companies

Count5 Sales Force Alignment Solutions
404-961-7350
1800 Peachtree Street Suite 444
Atlanta, GA
A.D.A.M., Inc
404-6042-757
10 10th Street NE
Atlanta, GA
SecuredPay Solutions, Inc.
404-324-3449
640 MCGILL PL NE
Atlanta, GA
VIA Networks
(702)2738210
3565 Piedmont Rd Bldg 1 Ste 525
Atlanta, GA
Arke Systems
404-812-3123
3400 Peachtree Road
Atlanta, GA
Ingenium Software
770-394-7724
1851 Peeler Road
Atlanta, GA
Ingenium Software
770-394-7724
1851 Peeler Road
Atlanta, GA
Ingenium Software
770-394-7724
1851 Peeler Road
Atlanta, GA
Inside Corp
770-828-0100
4279 Roswell Rd
Atlanta, GA
Manhattan Associates
(770) 955-7070
2300 Windy Ridge Parkway
Matthews, NC

provided by: 
Originally published at Internet.com


You are probably used to using C-style casts. Look at an example starting with C-style casts and see how using "as" and "is" can help your code. A common situation I run into is working with controls in an ASP.NET DataGrid. Suppose I need to work with the DataGridItem that is the parent of one of the controls in the grid. Using C-style type casting, I would do something like this: // C-style try { DataGridItem item = (DataGridItem)mycontrol.Parent; if (item.GetType().ToString() == "DataGridItem") { // Do something interesting here } } catch (InvalidCastException ex) { }

Although this method works, there is a much cleaner way. First, I'll address the type cast and exception handling using "as". With "as", there is no need to check for exceptions Because it will either return the object as the requested type or it will return null. You then can check the value against null to see whether the type conversion was successful. // Using As DataGridItem item = mycontrol.Parent as DataGridItem; if (item != null) { // Do something interesting here }

This code is much more concise and explicit than when using C-style type casting. Adding the use of "is" in the comparison makes this code more readable and is slightly more efficient than the comparison to null. The end result looks like this: // Using As and Is DataGridItem item = mycontrol.Parent as DataGridItem; if (item is DataGridItem) { // Do something interesting here }

Compare this with the original version using C-style type casting. I think that you will agree the version using "as" and "is" is easier to understand and maintain because there is no need to worry about invalid cast exceptions.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

Count5 Sales Force Alignment Solutions

Count5 is a software company that improves a company's top line by driving accountability to the front line.

404-961-7350
1800 Peachtree Street Suite 444
Atlanta, GA
Sales Force Alignment

Count5 is a software company that improves a company's top line by driving accountability to the front line. Count5's sales force alignment solution - Q OnDemand - raises the success rate of sales strategies, CRM and service initiatives by improving alignment of the work force with change, and for the first time, by quantifying alignment so problems can be addressed by managers early before performance suffers. Q is a unique communications tool that automates ongoing reinforcement and coaching to frontline employees on a recurring schedule while tracking user participation and retention for managers. This patent-pending process rapidly improves alignment with new initiatives, strategy and messaging. Count5® was recently named by the Technology Association of Georgia (TAG) as one of Georgia's most innovative technology companies.

Available OnDemand, Q™ can be implemented to drive your next change initiative in less than 30 days.

For more information, call us at (404) 961-7350 or visit us online at www.count5.com.

Related Local Events
Astd Atlanta Technology Based Learning Sig Event 4Th Annual Elearning Excellence Awards
Dates: 12/10/2009 - 12/10/2009
Location: Knowledge Development Centers
Atlanta, GA
View Details

TAG CRM
Dates: 11/12/2009 - 11/12/2009
Location: Oracle - Building 500
Atlanta, GA
View Details

Southeast User Group Leader Summit
Dates: 10/24/2009 - 10/24/2009
Location: Microsoft Alpharetta
Alpharetta, GA
View Details

TAG Excalibur Awards 2009
Dates: 10/23/2009 - 10/23/2009
Location: Atlanta Buckhead Marriott
Atlanta, GA
View Details

Astd Atlanta Technology Based Learning Sig Event Scorm
Dates: 10/6/2009 - 10/6/2009
Location: Knowledge Development Centers
Atlanta, GA
View Details