compiler/debugger misinterpreting variable data in IF

  • Thread starter Thread starter VMI
  • Start date Start date
V

VMI

I have a really simple IF statement that looks like this:

if (sNewString.Length == iCount)
{
MessageBox.Show (sNewString.Length.ToString() + " is equal to " +
iCount.ToString() + "*" + sNewString + "*");
}
if (sNewString.Trim() == "")
{
MessageBox.Show ("");
}
if (sNewString.Length == 0)
{
MessageBox.Show ("Length equals 0");
}
if (sNewString == null)
{
MessageBox.Show ("NULL");
}
if (sNewString.Length<iCount)
{
MessageBox.Show ("sNewString.Length<iCount");
}
if (sNewString.Length>iCount)
{
MessageBox.Show ("sNewString.Length>iCount");
}

When the debugger reaches the first IF, the debugger shows that
sNewString.Length = 0, iCount = 1, and sNewString = "". The frustrating
thing is that it only enters the first IF ("sNewString.Length == iCount")
and the MessageBox displays "1 is equal to 1*" at exactly the same time the
debugger's showing that sNewString.Length = 0 and iCount = 1. How can this
be possible? The messagebox and the debugger are showing different
information. I did notice that the MessageBox didn't display the last '*'
(anything after this instance of sNewString is truncated) but I have another
IF statement that checks for NULL but it never enters that IF.
The only thing I can think of is that, before these comparisons, I convert
this specific char into a string (the byte value of this specific char is
equal to 0) so I can do the IF comparisons. But that's it.

Any information is really appreciated.
 
I think you answered your own question:
The only thing I can think of is that, before these comparisons, I convert
this specific char into a string (the byte value of this specific char is
equal to 0) so I can do the IF comparisons. But that's it.

Your string almost certainly contains 1 character, which has ASCII value 0.
But ASCII 0 isn't a printable character, so when you look at the string
contents, you see "". Also, when you show it in the message box, the message
box interprets the character as the end of the string, which is why you
don't see the final "*".

All that said, I don't under stand why you're converting the character to a
string. Your reasoning was so that you could do the IF conparisons, but you
can use chars in IFs so I'm not sure what you mean.

Ken
 
I imagined that could've been it.
These IF statements are in another method that receive a string as
parameter. Most of the times this method is called with a string, but in
certain cases, I need to pass this char as parameter. So I convert the char
to string, and then call the method.
Once I convert this char to string, how can I distinguish between a null
string, an empty string, and this string?

Thanks again.
 
VMI said:
I imagined that could've been it.
These IF statements are in another method that receive a string as
parameter. Most of the times this method is called with a string, but in
certain cases, I need to pass this char as parameter. So I convert the char
to string, and then call the method.
Once I convert this char to string, how can I distinguish between a null
string, an empty string, and this string?

if (x==null) // Null string

if (x.Length==0) // Empty string

if (x=="\0") // String with a single character in, the nul character
 
VMI said:
When the debugger reaches the first IF, the debugger shows that
sNewString.Length = 0, iCount = 1, and sNewString = "". The frustrating
thing is that it only enters the first IF ("sNewString.Length == iCount")
and the MessageBox displays "1 is equal to 1*" at exactly the same time the
debugger's showing that sNewString.Length = 0 and iCount = 1. How can this
be possible? The messagebox and the debugger are showing different
information.

Unfortunately, the the debugger interprets strings using C language
semantics (ie., the string is terminated with a NULL character). .NET
strings can contain a NULL character, but any such string will be
displayed improperly by the debugger.

In your case, you probably have a string with a single '\0' character in it.
 
Yes. That was it.

Thanks.

Jon Skeet said:
if (x==null) // Null string

if (x.Length==0) // Empty string

if (x=="\0") // String with a single character in, the nul character
 
VMI said:
I have a really simple IF statement that looks like this:

if (sNewString.Length == iCount)
{
MessageBox.Show (sNewString.Length.ToString() + " is equal to " +
iCount.ToString() + "*" + sNewString + "*");
}
if (sNewString.Trim() == "")
{
MessageBox.Show ("");
}
if (sNewString.Length == 0)
{
MessageBox.Show ("Length equals 0");
}
if (sNewString == null)

NB This will never fire. You'll get a NullReferenceException on the first
test (property Length) if the reference is null. You need this test before
all the others.
 
Back
Top