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

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

Local Companies

SoloComputers.com
915-204-8163
7903 Pinebrook Dr.
San Antonio, TX
Systems Design
(210) 496-2877
12118 N North Loop Rd
San Antonio, TX
X Technologies Inc
(210) 822-8712
45 NE Loop # 800
San Antonio, TX
Akaoa Enterprises
(210) 541-8447
8026 Vantage Dr
San Antonio, TX
Teksystems
(210) 384-3100
1100 NW Loop 410 Ste 210
San Antonio, TX
Innovative Computer Solutions
(210) 525-8344
10205 Oasis St Ste 205
San Antonio, TX
Thompson Consulting Group
(210) 804-1010
7619 Vinewood Ct
San Antonio, TX
Meritec Services Inc
(210) 694-4635
12770 Cimarron Path
San Antonio, TX
Ecom-Elite Computer Consultants
(210) 495-4769
14350 Northbrook Dr Ste 200
San Antonio, TX
Insurance Automation Services
(210) 349-4642
1931 NW Military Hwy
San Antonio, 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

SoloComputers.com

915-204-8163
7903 Pinebrook Dr.
San Antonio, TX
http://SoloComputers.com

Related Local Event
2009 IEEE International Conference on Systems, Man and Cybernetics - SMC
Dates: 10/11/2009 - 10/14/2009
Location: Hyatt Regency Riverwalk
San Antonio, TX
View Details