.NET Tip: Type Casting and Comparison Using "as" and "is" Houston TX

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

Local Companies

Entrance Software
713-357-4930
1001 McKinney
Houston, TX
ObjectWin Technology, Inc.
(713) 337-1834
2650 Fountain View Drive, Suite 405
Houston, TX
Delta Business Solutions
(281) 445-3600
525 N. Sam Houston PKWY E
Houston, TX
Silicus Techologies
(713) 353-7416
Gray Falls
Houston, TX
Palindrome Software Labs Ltd
206-366-5542
7700 WillowChase BLVD
Houston, TX
NuHire Recruiting Solutions
281-748-9990
17002 Nautical Pointe Ln
Houston, TX
Idea Integration
(713) 627-7878
5 E Greenway Plz Ste 1700
Houston, TX
Root Consulting
(713) 523-8976
2010 North Loop W
Houston, TX
Ssgi
(713) 781-6161
2900 Wilcrest Dr Ste 495
Houston, TX
Data Tracking Associates
(713) 630-0079
3100 Timmons Ln Ste 300
Houston, TX

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

Entrance Software

Entrance Software is a Software Development and Consulting company and a Microsoft Gold Certified Partner.

713-357-4930
1001 McKinney
Houston, TX
http://www.entrancesoftware.com

For mid-sized businesses that own or need software, Entrance Software is a Trusted Advisor. Unlike other contract programming or staffing companies, Entrance exclusively provides Professional Software Consultants who are not only technical geniuses, but also understand the business value of Sales, Profit, Productivity, Loyalty and Morale.

Entrance Software is a Microsoft Gold Certified Partner and develops custom software applications for businesses in a variety of industries including Oil and Gas, Exploration and Production, Manufacturing, Education, Legal and Medical Services.


Related Local Events
Deep Offshore Technology (DOT 2010)
Dates: 2/2/2010 - 2/4/2010
Location: George R. Brown Convention Center
Houston, TX
View Details

Subsea Tieback Exhibition (SSTB)
Dates: 3/2/2010 - 3/4/2010
Location: Moody Gardens Hotel & Convention Center
Galveston, TX
View Details

2009 IEEE Industry Applications Society Annual Meeting
Dates: 10/4/2009 - 10/7/2009
Location: Hyatt Regency Houston
Houston, TX
View Details