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

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

Local Companies

Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
HarrisData
800-225-0585
13555 Bishops Court
Brookfield, WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI
Electroniclaim
262-240-9700
11357 N. Port Washington Rd
Mequon, WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI


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

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

Acumium

608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
http://www.acumium.com

Regional Articles
- .NET Tip: Type Casting and Comparison Using "as" and "is" Appleton WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Baraboo WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Beaver Dam WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Beloit WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Brookfield WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Burlington WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Cedarburg WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Chippewa Falls WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Cudahy WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" De Pere WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Delavan WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Eau Claire WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Elkhorn WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Fond Du Lac WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Fort Atkinson WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Franklin WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Green Bay WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Hartland WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Janesville WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Kaukauna WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Kenosha WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" La Crosse WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Lake Geneva WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Manitowoc WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Marinette WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Marshfield WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Menasha WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Menomonee Falls WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Menomonie WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Merrill WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Middleton WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Milwaukee WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Mosinee WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Mukwonago WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Muskego WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Neenah WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" New Berlin WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Oak Creek WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Oconomowoc WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Onalaska WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Oshkosh WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Pewaukee WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Racine WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Rhinelander WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Rice Lake WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" River Falls WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Schofield WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Shawano WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Sheboygan WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" South Milwaukee WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Stevens Point WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Sturgeon Bay WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Sun Prairie WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Superior WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Thiensville WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Two Rivers WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Watertown WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Waukesha WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Waupaca WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Wausau WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" West Bend WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Whitewater WI
- .NET Tip: Type Casting and Comparison Using "as" and "is" Wisconsin Rapids WI
Related Local Events
2008 Early Stage Symposium
Dates: 11/5/2008 - 11/6/2008
Location: Monona Terrace
Madison WI
View Details

Wisconsin Entrepreneurs' Conference
Dates: 6/9/2008 - 6/10/2008
Location: Hyatt Regency Hotel
Milwaukee WI
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Home Services Software
Business Services Entertainment Industrial Goods & Services Technology
Career Family Insurance Telecommunications
Cars Financial Services Internet Transportation & Logistics
Computer Hardware Food & Beverage Legal Travel
Construction Health Real Estate Wedding
Education Home Electronics Retail & Consumer Services