473,503 Members | 3,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why does this execute the else clause

char c ='\0';
string cstr = "";
string cstr2;
cstr2 =cstr + c;
//Debugger watch on cstr2.Length shows 0
if( cstr2.Length == 0)
{
MessageBox.Show ("in if",
cstr2,MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk);
}
else
{
MessageBox.Show ("in else",
cstr2,MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk);
}

why does this execute the else clause
Nov 15 '05 #1
7 1215
noname <no****@noname.com> wrote:
char c ='\0';
string cstr = "";
string cstr2;
cstr2 =cstr + c;
//Debugger watch on cstr2.Length shows 0
if( cstr2.Length == 0)
{
MessageBox.Show ("in if",
cstr2,MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk);
}
else
{
MessageBox.Show ("in else",
cstr2,MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk);
}

why does this execute the else clause


Because the string's length isn't 0. It's 1 - it has a single
character, which is the null character. The debugger is showing the
wrong thing here, but the execution is correct.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
Because cstr2's length is 1. C# strings _can_ contain zeroes; unlike C/C++
strings they're _not_ zero-terminated. (Closest analogy would be BSTRs)

"noname" <no****@noname.com> wrote in message
news:02****************************@phx.gbl...
char c ='\0';
string cstr = "";
string cstr2;
cstr2 =cstr + c;
//Debugger watch on cstr2.Length shows 0
if( cstr2.Length == 0)
{
MessageBox.Show ("in if",
cstr2,MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk);
}
else
{
MessageBox.Show ("in else",
cstr2,MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk);
}

why does this execute the else clause

Nov 15 '05 #3
"noname" <no****@noname.com> wrote:
char c ='\0';
string cstr = "";
string cstr2;
cstr2 =cstr + c;
//Debugger watch on cstr2.Length shows 0


For me, it shows 1. Are you sure you checked the watch *after* that
statement had executed?

The else clause executes because a string containing a null character isn't
an empty string.

P.

--
www.CL4.org
Nov 15 '05 #4
Paul E Collins <fi******************@CL4.org> wrote:
"noname" <no****@noname.com> wrote:
char c ='\0';
string cstr = "";
string cstr2;
cstr2 =cstr + c;
//Debugger watch on cstr2.Length shows 0


For me, it shows 1. Are you sure you checked the watch *after* that
statement had executed?


It could be because the OP is using an unpatch version of VS.NET 2002 -
I seem to remember that there were various bugs when watching strings
in VS.NET 2002.

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

"noname" <no****@noname.com> schreef in bericht
news:02****************************@phx.gbl...
char c ='\0';
string cstr = "";
string cstr2;
cstr2 =cstr + c;
//Debugger watch on cstr2.Length shows 0


I have tested this with Borland C#Builder. The watch tells me that
cstr.Length = 1. So there is a bug in the debugger of the compiler that you
use.

Hans Kamp.
Nov 15 '05 #6

"Tu-Thach" <tu*****@yahoo.com> schreef in bericht
news:02****************************@phx.gbl...
The else is executed because cstr2.Length is greater than
0. Maybe something is strange with your debugger.


I think it is, because the debugger of the compiler that I use (C#Builder)
doesn't show this bug, and shows "1" for cstr2.Length.

Hans Kamp.
Nov 15 '05 #7

"Paul E Collins" <fi******************@CL4.org> schreef in bericht
news:bi**********@hercules.btinternet.com...
"noname" <no****@noname.com> wrote:
char c ='\0';
string cstr = "";
string cstr2;
cstr2 =cstr + c;
//Debugger watch on cstr2.Length shows 0


For me, it shows 1. Are you sure you checked the watch *after* that
statement had executed?


At the start the debugger shows:

cstr2 "null"
cstr2.Length function call terminated by exception

After:

char c ='\0';
string cstr = "";
string cstr2;

it shows:

cstr2 "null"
cstr2.Length function call terminated by exception

After:

cstr2 = cstr + c;

it shows:

cstr ""
cstr2.Length 1

Hans Kamp.
Nov 15 '05 #8

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

Similar topics

4
1696
by: shumaker | last post by:
I'm wondering how/why this query works. Trying to get my head wrapped around SQL. Basically the Query deletes from the Import table all records that are already in FooStrings so that when I do an...
2
2050
by: smauldin | last post by:
Why does the execution plan have a nested loop join for a simple select with an UDF in the where clause? Here is the query: select * from test_plan where vCol = my_udf('test') Here is the...
9
2810
by: Downstreamer | last post by:
Design is as follows: A form for the user to input criteria to be used as the where part of the report's recordsource. This includes a multiselect list box as part of the criteria selection. ...
7
2113
by: GS | last post by:
Hello, I'm receiving The state information is invalid for this page and might be corrupted error message when running code below. This happens on second post back. Why is it happening? My...
48
4195
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
25
1835
by: metaperl.etc | last post by:
A very old thread: http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gst&q=for+else&rnum=9#1542d2041257c47e discusses the optional "else:"...
1
1759
by: rcamarda | last post by:
I was looking through our vendors views, searching for something I needed for our Datawarehouse and I came across something I do not understand: I found a view that lists data when I use it in...
2
1481
by: shanazzle | last post by:
i have to do a project outlined at this link: http://pages.cpsc.ucalgary.ca/~schock/wiki/index.php/F07_CPSC_217_A2 im currently doing part 2, involving searching for information from a cat data...
12
4779
by: lali.b97 | last post by:
Somewhere in a tutorial i read that if statement has performance overheads as code within the if statement cannot take benefit of pipeling of microprocessor and also that the compiler cannot...
0
7192
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,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
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,...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4665
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...
0
3158
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...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
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 ...
0
369
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...

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.