.NET Tip: Comparing Strings Safely Wisconsin

The .NET Framework's built-in comparison functions don't handle nulls properly. Learn a couple of functions to take care of this for you.

Local Companies

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
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
Electroniclaim
262-240-9700
11357 N. Port Washington Rd
Mequon, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI
Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
HarrisData
800-225-0585
13555 Bishops Court
Brookfield, WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI


.NET Tip: Comparing Strings Safely

provided by: 
Originally published at Internet.com


You often need to compare strings (or other data types), but sometimes a value could be null or DBNull if it's coming from the database. The .NET Framework's built-in comparison functions don't handle nulls properly, so I wrote a couple of functions to take care of this for me.

This function, and its overloaded version, compare the string value of an object and another string: private bool SafeCompare(object test, string testValue) { return SafeCompare(test, testValue, StringComparison.CurrentCultureIgnoreCase); } private bool SafeCompare(object test, string testValue, StringComparison compareSetting) { if (test != null) { if (!Convert.IsDBNull(test)) { return String.Compare(test.ToString(), testValue, compareSetting) == 0; } else return false; } else return false; }

The function first tests the object against null, then against DBNull, and then against the string in question. Any null/DBNull value will cause the function to return false. The overloaded version also allows you to default the StringComparison value, which enables you to specify case-sensitive or case-insensitive comparisons. I prefer to do a case-insensitive comparison in most cases, so that's the default value I send if that parameter is omitted.

Using this function as a model, you could easily add other overloads for non-string data types, such as doubles or integers. Here's a version for comparing two integers "safely": private bool SafeCompare(object test, integer testValue) { if (test != null) { if (!Convert.IsDBNull(test)) { return Convert.ToInt32(test) == testValue; } else return false; } else return false; }

Because the parameter types are different, you can add this overloaded version to the other two shown above. This saves a lot of duplicated code and works around the inability to deal with nulls without throwing exceptions.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.

Author: Eric Smith

Read article at Internet.com site

Featured Local Company

Wireless Direct

1.866.707.8498
BOX 71101
shorewood, WI
http://www.prepaidwireless.2ya.com

Regional Articles
- .NET Tip: Comparing Strings Safely Appleton WI
- .NET Tip: Comparing Strings Safely Baraboo WI
- .NET Tip: Comparing Strings Safely Beaver Dam WI
- .NET Tip: Comparing Strings Safely Beloit WI
- .NET Tip: Comparing Strings Safely Brookfield WI
- .NET Tip: Comparing Strings Safely Burlington WI
- .NET Tip: Comparing Strings Safely Cedarburg WI
- .NET Tip: Comparing Strings Safely Chippewa Falls WI
- .NET Tip: Comparing Strings Safely Cudahy WI
- .NET Tip: Comparing Strings Safely De Pere WI
- .NET Tip: Comparing Strings Safely Delavan WI
- .NET Tip: Comparing Strings Safely Eau Claire WI
- .NET Tip: Comparing Strings Safely Elkhorn WI
- .NET Tip: Comparing Strings Safely Fond Du Lac WI
- .NET Tip: Comparing Strings Safely Fort Atkinson WI
- .NET Tip: Comparing Strings Safely Franklin WI
- .NET Tip: Comparing Strings Safely Green Bay WI
- .NET Tip: Comparing Strings Safely Hartland WI
- .NET Tip: Comparing Strings Safely Janesville WI
- .NET Tip: Comparing Strings Safely Kaukauna WI
- .NET Tip: Comparing Strings Safely Kenosha WI
- .NET Tip: Comparing Strings Safely La Crosse WI
- .NET Tip: Comparing Strings Safely Lake Geneva WI
- .NET Tip: Comparing Strings Safely Manitowoc WI
- .NET Tip: Comparing Strings Safely Marinette WI
- .NET Tip: Comparing Strings Safely Marshfield WI
- .NET Tip: Comparing Strings Safely Menasha WI
- .NET Tip: Comparing Strings Safely Menomonee Falls WI
- .NET Tip: Comparing Strings Safely Menomonie WI
- .NET Tip: Comparing Strings Safely Merrill WI
- .NET Tip: Comparing Strings Safely Middleton WI
- .NET Tip: Comparing Strings Safely Milwaukee WI
- .NET Tip: Comparing Strings Safely Mosinee WI
- .NET Tip: Comparing Strings Safely Mukwonago WI
- .NET Tip: Comparing Strings Safely Muskego WI
- .NET Tip: Comparing Strings Safely Neenah WI
- .NET Tip: Comparing Strings Safely New Berlin WI
- .NET Tip: Comparing Strings Safely Oak Creek WI
- .NET Tip: Comparing Strings Safely Oconomowoc WI
- .NET Tip: Comparing Strings Safely Onalaska WI
- .NET Tip: Comparing Strings Safely Oshkosh WI
- .NET Tip: Comparing Strings Safely Pewaukee WI
- .NET Tip: Comparing Strings Safely Racine WI
- .NET Tip: Comparing Strings Safely Rhinelander WI
- .NET Tip: Comparing Strings Safely Rice Lake WI
- .NET Tip: Comparing Strings Safely River Falls WI
- .NET Tip: Comparing Strings Safely Schofield WI
- .NET Tip: Comparing Strings Safely Shawano WI
- .NET Tip: Comparing Strings Safely Sheboygan WI
- .NET Tip: Comparing Strings Safely South Milwaukee WI
- .NET Tip: Comparing Strings Safely Stevens Point WI
- .NET Tip: Comparing Strings Safely Sturgeon Bay WI
- .NET Tip: Comparing Strings Safely Sun Prairie WI
- .NET Tip: Comparing Strings Safely Superior WI
- .NET Tip: Comparing Strings Safely Thiensville WI
- .NET Tip: Comparing Strings Safely Two Rivers WI
- .NET Tip: Comparing Strings Safely Watertown WI
- .NET Tip: Comparing Strings Safely Waukesha WI
- .NET Tip: Comparing Strings Safely Waupaca WI
- .NET Tip: Comparing Strings Safely Wausau WI
- .NET Tip: Comparing Strings Safely West Bend WI
- .NET Tip: Comparing Strings Safely Whitewater WI
- .NET Tip: Comparing Strings Safely 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