Search This Blog

Tuesday, March 11, 2008

Visual Studio 2005 debugger irritation with double backslashes

I'm sure everyone knows this but me but I couldn't find this information easily today and wasted time solving it, so I'm posting it here on the chance that a search engine spider will grab it and index it in case someone else can use it.

When stepping through a section of code that was reading records from a SQL database, I watched a variable in the Visual Studio 2005 "Immediate" window which was pulling a field from a SQL record representing a pathname.

The textfield in the database record was "C:\directory\subdir1" but in the "Immediate" window it was listed as "C:\\directory\\subdir1".

Since the error I was trying to track down was a "file not found" I was sure that problem was around there, there was something I was doing that was causing the backslashes to stutter and double up in C#.

I tried just doing a

(Variable.ToString()).Replace(@"\\",@"\");

but no good, the debugger said the double backslashes were still there. I then spent 20 minutes down memory lane handling the problem using a Regular Expression but the debugger showed that that didn't work either.

The answer is simple: the double slashes are an illusion of the debugger, they're not real. By default the debugger will display backslash characters as double backslashes since it's using the backslash as a control character. The pathname was fine, it was getting it from the SQL record exactly the way I expected, it just didn't "look" that way (the "file not found" error was a few lines away).

Just write the text out to a control on a form or out to the console ... it's fine. The debugger is still our friend.

It's merely a painful hangover from 5 years developing with VB.NET and its "vbCRLF" instead of C and its "\n"; you forget how normal languages handle control characters.

1 comment:

Claudia said...

I know! The first language(s) I learned in college was Java and C, so I was all over the \n. Halfway through my college career they had me take a nose dive into Visual Basic and I was like WTF is this crap, man?

Ever sense, I have been a VB girl. But with Sharepoint, I had to go back to my C#. :)