473,804 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compiler/debugger misinterpreting variable data in IF

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

if (sNewString.Len gth == iCount)
{
MessageBox.Show (sNewString.Len gth.ToString() + " is equal to " +
iCount.ToString () + "*" + sNewString + "*");
}
if (sNewString.Tri m() == "")
{
MessageBox.Show ("");
}
if (sNewString.Len gth == 0)
{
MessageBox.Show ("Length equals 0");
}
if (sNewString == null)
{
MessageBox.Show ("NULL");
}
if (sNewString.Len gth<iCount)
{
MessageBox.Show ("sNewString.Le ngth<iCount");
}
if (sNewString.Len gth>iCount)
{
MessageBox.Show ("sNewString.Le ngth>iCount");
}

When the debugger reaches the first IF, the debugger shows that
sNewString.Leng th = 0, iCount = 1, and sNewString = "". The frustrating
thing is that it only enters the first IF ("sNewString.Le ngth == iCount")
and the MessageBox displays "1 is equal to 1*" at exactly the same time the
debugger's showing that sNewString.Leng th = 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.
Nov 16 '05 #1
6 1151
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
"VMI" <vo******@yahoo .com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. I have a really simple IF statement that looks like this:

if (sNewString.Len gth == iCount)
{
MessageBox.Show (sNewString.Len gth.ToString() + " is equal to " +
iCount.ToString () + "*" + sNewString + "*");
}
if (sNewString.Tri m() == "")
{
MessageBox.Show ("");
}
if (sNewString.Len gth == 0)
{
MessageBox.Show ("Length equals 0");
}
if (sNewString == null)
{
MessageBox.Show ("NULL");
}
if (sNewString.Len gth<iCount)
{
MessageBox.Show ("sNewString.Le ngth<iCount");
}
if (sNewString.Len gth>iCount)
{
MessageBox.Show ("sNewString.Le ngth>iCount");
}

When the debugger reaches the first IF, the debugger shows that
sNewString.Leng th = 0, iCount = 1, and sNewString = "". The frustrating
thing is that it only enters the first IF ("sNewString.Le ngth == iCount")
and the MessageBox displays "1 is equal to 1*" at exactly the same time the debugger's showing that sNewString.Leng th = 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.

Nov 16 '05 #2
VMI
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.

Ken Kolda <ke*******@elli emae-nospamplease.co m> wrote in message
news:#Y******** *****@tk2msftng p13.phx.gbl...
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
"VMI" <vo******@yahoo .com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a really simple IF statement that looks like this:

if (sNewString.Len gth == iCount)
{
MessageBox.Show (sNewString.Len gth.ToString() + " is equal to " +
iCount.ToString () + "*" + sNewString + "*");
}
if (sNewString.Tri m() == "")
{
MessageBox.Show ("");
}
if (sNewString.Len gth == 0)
{
MessageBox.Show ("Length equals 0");
}
if (sNewString == null)
{
MessageBox.Show ("NULL");
}
if (sNewString.Len gth<iCount)
{
MessageBox.Show ("sNewString.Le ngth<iCount");
}
if (sNewString.Len gth>iCount)
{
MessageBox.Show ("sNewString.Le ngth>iCount");
}

When the debugger reaches the first IF, the debugger shows that
sNewString.Leng th = 0, iCount = 1, and sNewString = "". The frustrating
thing is that it only enters the first IF ("sNewString.Le ngth == iCount") and the MessageBox displays "1 is equal to 1*" at exactly the same time

the
debugger's showing that sNewString.Leng th = 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.


Nov 16 '05 #3
VMI <vo******@yahoo .com> wrote:
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

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
VMI wrote:

When the debugger reaches the first IF, the debugger shows that
sNewString.Leng th = 0, iCount = 1, and sNewString = "". The frustrating
thing is that it only enters the first IF ("sNewString.Le ngth == iCount")
and the MessageBox displays "1 is equal to 1*" at exactly the same time the
debugger's showing that sNewString.Leng th = 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.
--
mikeb
Nov 16 '05 #5
VMI
Yes. That was it.

Thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
VMI <vo******@yahoo .com> wrote:
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

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6

"VMI" <vo******@yahoo .com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a really simple IF statement that looks like this:

if (sNewString.Len gth == iCount)
{
MessageBox.Show (sNewString.Len gth.ToString() + " is equal to " +
iCount.ToString () + "*" + sNewString + "*");
}
if (sNewString.Tri m() == "")
{
MessageBox.Show ("");
}
if (sNewString.Len gth == 0)
{
MessageBox.Show ("Length equals 0");
}
if (sNewString == null)
NB This will never fire. You'll get a NullReferenceEx ception on the first
test (property Length) if the reference is null. You need this test before
all the others.
{
MessageBox.Show ("NULL");
}
if (sNewString.Len gth<iCount)
{
MessageBox.Show ("sNewString.Le ngth<iCount");
}
if (sNewString.Len gth>iCount)
{
MessageBox.Show ("sNewString.Le ngth>iCount");
}

When the debugger reaches the first IF, the debugger shows that
sNewString.Leng th = 0, iCount = 1, and sNewString = "". The frustrating
thing is that it only enters the first IF ("sNewString.Le ngth == iCount")
and the MessageBox displays "1 is equal to 1*" at exactly the same time the debugger's showing that sNewString.Leng th = 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.

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6389
by: thule | last post by:
Okay, thanks to John (THANK YOU SO MUCH). I switched all my header files over to using the new Standard <iostream> and included the using namespace std; . This seems to have fixed all the errors I was getting because of mixing the old and new headers. The problem I am running into now is not an error but 10 warnings when compiling the same class header implimentation file that I was working with when I was having the problems with the map...
4
2201
by: Jeremy Watts | last post by:
Hi, I'm completely new to C++ and wondered if anyone has a recommendation for a C++ compiler Thanks
8
3661
by: Rob S | last post by:
I have UDB 8.1 Personal Edition installed. I'm using Development centre to develop JAVA Stored Procedues. I am unable to debug them. I have installed IBM Distributed Debugger and have set DB2ROUTINE_DEBUG=ON. I installed Distributed Debugger after UDB. The Procs will build with debug but no debugger window appears when run in debug mode. I followed the instructions that came with Debugger regarding the installation directory etc.
2
2145
by: Jonathan Wilson | last post by:
I have an app written in native C++ using Visual C++ 2005 (pro edition). How can I set a memory breakpoint in the debugger for this app? Or alternatively, is there another source level debugger I can use that will let me set such breakpoints? Note that moving to Visual Studio 2008 is not an option.
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.