.NET Tip: Comparing Strings Safely Atlanta GA

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

Count5 Sales Force Alignment Solutions
404-961-7350
1800 Peachtree Street Suite 444
Atlanta, GA
A.D.A.M., Inc
404-6042-757
10 10th Street NE
Atlanta, GA
SecuredPay Solutions, Inc.
404-324-3449
640 MCGILL PL NE
Atlanta, GA
VIA Networks
(702)2738210
3565 Piedmont Rd Bldg 1 Ste 525
Atlanta, GA
Arke Systems
404-812-3123
3400 Peachtree Road
Atlanta, GA
Ingenium Software
770-394-7724
1851 Peeler Road
Atlanta, GA
Ingenium Software
770-394-7724
1851 Peeler Road
Atlanta, GA
Ingenium Software
770-394-7724
1851 Peeler Road
Atlanta, GA
Inside Corp
770-828-0100
4279 Roswell Rd
Atlanta, GA
Manhattan Associates
(770) 955-7070
2300 Windy Ridge Parkway
Matthews, NC

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

Count5 Sales Force Alignment Solutions

Count5 is a software company that improves a company's top line by driving accountability to the front line.

404-961-7350
1800 Peachtree Street Suite 444
Atlanta, GA
Sales Force Alignment

Count5 is a software company that improves a company's top line by driving accountability to the front line. Count5's sales force alignment solution - Q OnDemand - raises the success rate of sales strategies, CRM and service initiatives by improving alignment of the work force with change, and for the first time, by quantifying alignment so problems can be addressed by managers early before performance suffers. Q is a unique communications tool that automates ongoing reinforcement and coaching to frontline employees on a recurring schedule while tracking user participation and retention for managers. This patent-pending process rapidly improves alignment with new initiatives, strategy and messaging. Count5® was recently named by the Technology Association of Georgia (TAG) as one of Georgia's most innovative technology companies.

Available OnDemand, Q™ can be implemented to drive your next change initiative in less than 30 days.

For more information, call us at (404) 961-7350 or visit us online at www.count5.com.

Related Local Events
Astd Atlanta Technology Based Learning Sig Event 4Th Annual Elearning Excellence Awards
Dates: 12/10/2009 - 12/10/2009
Location: Knowledge Development Centers
Atlanta, GA
View Details

TAG CRM
Dates: 11/12/2009 - 11/12/2009
Location: Oracle - Building 500
Atlanta, GA
View Details

Southeast User Group Leader Summit
Dates: 10/24/2009 - 10/24/2009
Location: Microsoft Alpharetta
Alpharetta, GA
View Details

TAG Excalibur Awards 2009
Dates: 10/23/2009 - 10/23/2009
Location: Atlanta Buckhead Marriott
Atlanta, GA
View Details

Astd Atlanta Technology Based Learning Sig Event Scorm
Dates: 10/6/2009 - 10/6/2009
Location: Knowledge Development Centers
Atlanta, GA
View Details