.NET Tip: Type Casting and Comparison Using "as" and "is" Miami FL

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

Local Companies

Truelogic Software Solutions
(305) 407-9432
8345 NW 66th St.
Miami, FL
IBLESOFT INC
305.908.7957 EXT 100
7801 NW 37th Street
Doral, FL
Acl Computers Inc
(305) 826-5000
1711 W 38th Pl
Hialeah, FL
Game Rak Inc
(786) 517-1882
1790 W 49th St
Hialeah, FL
Software Development Solutions
305-274-2147
9745 SW 72nd St
Miami, FL
Home Care Software Solutions Inc
786-433-4700
9500 S Dadeland Blvd
Miami, FL
Alta Star Software Inc
305-279-8898
7700 N Kendall DR
Miami, FL
BlueClaw Group LLC
(305) 433-2006
1508 bay rd
miami beach, FL
TrueSoft
408-647-1434
911 Meridian Ave, Suite 201
Miami Beach, FL
Creative Technical Systems
(305) 512-2872
8181 NW 154th St
Hialeah, FL

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

Truelogic Software Solutions

3054079432
8345 NW 66th St.
Miami, FL
www.truelogicsoftware.com

Related Local Events
SCORE Business Counseling
Dates: 12/9/2009 - 12/9/2009
Location: The Chamber Office
Coral Springs, FL
View Details

Wake Up Coral Springs Breakfast!!
Dates: 12/15/2009 - 12/15/2009
Location: Marriott Heron Bay
Coral Springs, FL
View Details

Wednesday Evening Referral Group
Dates: 12/16/2009 - 12/16/2009
Location: WineStyles
Coral Springs, FL
View Details

Afternoon Chamber Referral Group Meeting
Dates: 1/21/2010 - 1/21/2010
Location: The Coral Springs Chamber Office
Coral Springs, FL
View Details

Wednesday Evening Referral Group
Dates: 2/3/2010 - 2/5/2010
Location: WineStyles
Coral Springs, FL
View Details