.NET Tip: Comparing Strings Safely Clover SC

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

Charlotte Web Design & Development
(704) 577-8242
Commodore point rd
Lake Wylie, SC
Oracle Corporation
(704) 423-1450
2550 West Tyvola Road
Charlotte, NC
SunGard Asset Management Systems
(704) 527-6300
PO Box 240882
Charlotte, NC
Day Consultants Inc
(843) 626-6859
1293 Professional Dr Ste D
Myrtle Beach, SC
Noblitt Brad & Amy
(864) 292-2472
1104 Halcyon Cir
Greer, SC
Icc Systems
(843) 686-6685
Arrow Ctr
Hilton Head Island, SC
Precision Business Technologies Inc
(843) 235-2907
70 Dagullah Way
Pawleys Island, SC
Strategic Technologies
(864) 281-0682
481 Garlington Rd
Greenville, SC
Technology Resources For Effective Environments
(864) 223-0640
Greenwood, SC
C-1 Tech
(843) 645-4220
124 Boardwalk Dr
Okatie, SC

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

Charlotte Web Design & Development

7045778242
Commodore point rd
Lake Wylie, SC
http://www.charlottewebdevelopment.com

Related Local Events
Automation Technology Expo South
Dates: 4/28/2010 - 4/29/2010
Location: Charlotte Convention Center
Charlotte, NC
View Details

Green Manufacturing Expo-Charlotte
Dates: 4/28/2010 - 4/29/2010
Location: Charlotte Convention Center
Charlotte, NC
View Details

Technology Summit: Technology + Innovation = Our Future
Dates: 11/18/2009 - 11/18/2009
Location: Hilton Charlotte Center City
Charlotte, NC
View Details

NCTA Congressional Briefing: Featuring U.S. Representatives Sue Myrick and Larry Kissell
Dates: 11/2/2009 - 11/2/2009
Location: Charlotte Chamber - Belk Action Center
Charlotte, NC
View Details

SOUTH-TEC
Dates: 10/6/2009 - 10/8/2009
Location: Charlotte Convention Center
Charlotte, NC
View Details