.NET Tip: Comparing Strings Safely Austin TX

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

Lone Star Internet
512-708-8006
211 E 7th Ste 1110
Austin, TX
Adhesive Software
512-478-7349
800 Brazos St
Austin, TX
Austin Programming Solutions
512-990-2151
900 Congress Avenue
Austin, TX
Activant Solutions Inc
512-328-2300
804 Las Cimas Pkwy
Austin, TX
MicroMain Corporation
512-328-3235
5100 Bee Caves Road
Austin, TX
MicroMain Corporation
523-328-3235
5100 Bee Caves Road
Austin, TX
Terminal B Information Technology Services
512-381-4800
108 Wild Basin Road, Suite 255, Austin, TX 78746
Austin, TX
Mojica PC Solutions
512-300-9956
614 S 1st Street
Austin, TX
Parts-People.Com Inc
512-339-1990
3106 Industrial Terrace
Austin, TX
IQgistics
512.535.4165
5900 Southwest Parkway
Austin, TX

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

Lone Star Internet

512-708-8006
211 E 7th Ste 1110
Austin, TX
http://www.lone-star.net

Related Local Events
Renewable Energy World Conference & Expo North America
Dates: 2/23/2010 - 2/25/2010
Location: Austin Convention Center, Austin
Austin, TX
View Details

Lone Star Ruby Conference 2009
Dates: 8/27/2009 - 8/29/2009
Location: Norris Conference Center
Austin, TX
View Details