.NET Tip: Type Casting and Comparison Using "as" and "is" Lansdale PA

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

Local Companies

CPA Technology, LLC
610-862-1998
101 W. Elm Street
Conshohocken, PA
The MediGroup, Ltd.
610-666-1955 x12
1308 Egypt Road
Oaks, PA
Deacom, Inc.
610-971-2278
950 West Valley Road, Suite 3000
Wayne, PA
Quintiq Inc.
610-964-8111
565 E Swedesford Rd #303
Wayne, PA
KasTech Consulting, Inc.
215-702-8155
116 N. Bellevue Avenue
Langhorne, PA
Helpline Center Office- Childrens Aid Society
(215) 361-6942
306 N Madison Ave
Lansdale, PA
Mousley Consulting
(215) 631-1366
608 W Main St
Lansdale, PA
Orsino Consulting
(215) 362-6302
Lansdale, PA
Adept Consulting Services Inc
(215) 855-3610
408 W Main St
Lansdale, PA
Idea Revolution
(215) 855-7045
531 S Broad St
Lansdale, PA

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

CPA Technology, LLC

Sage software, consulting & implementation, training (Sage Authorized Training Center)

610-862-1998
101 W. Elm Street
Conshohocken, PA
www.cpatechnology.net

CPA Technology is a leader in providing Sage software solutions to the construction and real estate industries in Eastern Pennsylvania, New Jersey, and Delaware.

www.cpatechnology.net

Related Local Events
Hot Java at the Watercooler
Dates: 12/23/2009 - 1/23/2010
Location: Odyssey Travel
Skippack, PA
View Details

IT Roundtable
Dates: 11/10/2009 - 11/10/2009
Location: D&E Communications
Brownstown, PA
View Details

Fraser Advanced Information Systems Mixer
Dates: 10/29/2009 - 10/29/2009
Location:
Bethlehem, PA
View Details

PREP 2009: 22nd International Symposium, Exhibit and Workshops
Dates: 7/19/2009 - 7/22/2009
Location: Loews Hotel
Philadelphia, PA
View Details