Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« TTDing an interview questionThe use of descriptive variable names is forbidden »
comments

C# case sensitivity

by chopstik on Jul 15, 2009 in categories C#
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

I spent the better part of two weeks trying to troubleshoot an issue that was causing some problems. Essentially, information within the system was not being properly routed based on specific criteria. When I received the case, the issue had apparently been fixed for the customer in question so, when I attempted to recreate the problem on my local box, I was unable to reproduce it. Instead, I tried stepping through the code (which is pretty complex C# code) and then looking at the data and relevant stored procedures in the back-end SQL database. I had found a discrepancy in part of the data that had been stored, but it turned out to be ancillary to the originating cause (to be shown shortly).

Finally, someone else noted that running an update on a customer (without actually updating any information) fixed the problem. But stepping through the update functionality did not bring me any closer to finding the solution. One thing that was noticed was that it seemed to happen with customers who had alpha characters in their ID. But not all of them, only some, namely older customers. It also seemed to be related to those with lower case characters in the ID. But our database is set up to be case-insensitive. And, as it turned out, the test samples I was working with all used upper case alpha characters. When finally testing against lower case samples, the problem appeared!

But I was confused. The database is case-insensitive, so what difference does it make?! For a VB programmer (or at least one who started out with VB structure and only in the last year has moved over to C#), no difference at all. For C#, however, a great deal. I already knew that C# was sensitive to case in terms of variable names, functions, classes, etc. But it never dawned on me that strings, when compared to each other, were also case-sensitive! :roll:

They say the best lessons learned are those learned the hard way. A simple problem in the end, but one that turned out to be much harder because of my own misconceptions and assumptions. On the flip side, though, it is one lesson that will never be forgotten!

1282 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

13 comments

Comment from: Naomi [Member] Email
Can you please show a small piece of code describing the issue? You pass your strings as parameters to SQL Server, correct? Just want to understand what may be particular scenario.

Also, I would assume that comparison of strings is case-sensitive in VB.NET as well. "AB" is not the same as "ab".
15/07/09 @ 19:02
Comment from: Alex Ullrich [Member] Email
****-
Following on Naomi's question, you were doing your comparisons in C# code right? If you were using the regular equality operator in VB6 it still wouldn't have worked (IIRC, you need to use StrComp to do case-insensitive comparison?)

Still, this hasn't bitten me before so hopefully I will learn your lesson and it never will :D
15/07/09 @ 19:33
Comment from: chopstik [Member] Email
Naomi, in this case, the comparison was being done in C# code, not SQL Server (which would have found them as it doesn't care about the case sensitivity). And it may well be an issue in VB.NET, but my earlier experience was with VB, not .NET.

Alex, define "the regular equality operator in VB6". There may be some better comparison methods in .NET (like string.CompareTo()), but the end result is the same (based on what I tried).

A sample of what I did as a subsequent test is as follows (the result, of course, was "They don't match"):

private void button1_Click(object sender, EventArgs e)
{
string testCaps = "The";
string testLower = "the";
if (testCaps == testLower)
textBox1.Text = "Matched";
else
textBox1.Text = "They don't match";
}
16/07/09 @ 05:58
Comment from: chopstik [Member] Email
Something I should have pointed out was how to ignore case (I found this information at http://blog.benday.com/archive/2006/05/17/4395.aspx). Using the same format as in my previous comment.

private void button1_Click(object sender, EventArgs e)
{
bool myVal = string.Equals("the", "The", StringComparison.CurrentCultureIgnoreCase);
textBox1.Text = myVal.ToString();
}
16/07/09 @ 06:12
Comment from: Naomi [Member] Email
Interesting second solution. But I'm surprised you expected the original code to work. Does it really work the same in VB (in VB.NET it should not work too)?
16/07/09 @ 06:13
Comment from: chopstik [Member] Email
The code I posted in my comments is some sample code I was using to prove my original problem, not the code from the original problem itself. That code is significantly more complex and not as straightforward and not something I can easily post at the moment.

I can't say definitively that it works the same in VB6 - only that I never encountered this issue in VB6 and am pretty sure I had to do similar comparisons.
16/07/09 @ 06:18
Comment from: Naomi [Member] Email
In the meantime I was trying to think, which languages I know, do case - sensitive string comparison and which case insensitive. It may be an interesting topic to discuss. May be in the forum.
16/07/09 @ 06:26
Comment from: SQLDenis [Member] Email
*****
The best way to learn is the hard way.....this way you will never forget what you learned.....just saying :-)
16/07/09 @ 06:58
Comment from: Naomi [Member] Email
That's true. After I made a mistake of using where condition on a table from the right side in the LEFT JOIN 3 times, I learned the lesson :)
16/07/09 @ 07:29
Comment from: Alex Ullrich [Member] Email
Sorry, by "regular equality operator" i meant "=" (or "==" for C#)

Once you go to the actual comparison functions there are options for case-insensitive. I wasn't sure which you were using.
16/07/09 @ 08:40
Comment from: George Mastros [Member] Email
VB6 (normally) does case-sensitive comparisons. You could put "OPTION COMPARE TEXT" at the top of the source code module. This would result in case-insensitive comparisons. Just saying ;-)
16/07/09 @ 12:18
Comment from: Alex Ullrich [Member] Email
I should have known the VB master would have something to prove me wrong :)
16/07/09 @ 14:41
Comment from: Emtucifor [Member] Email
****-
or OPTION COMPARE DATABASE

And some string functions in VB6 let you specify Binary, Text or Database compare (instr?)
17/07/09 @ 01:05

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)