Weird behaviour when working with strings.

  • Thread starter Thread starter Juan
  • Start date Start date
J

Juan

A very simple concatenation stopped working. I have three variables that I
concatenate using +. When I debug I see the variables but the obtained
string just shows the first two variables.



Any idea.?



Thanks,

Juan.
 
Nicholas,

string = stringvar1 + stringvar2 + stringvar3 + stringvar4



The string is concatenated until it reaches stringvar3 which is an instance
variable, after that the others variables don't get concatenated. When I
debug everything seem right but it just doesn't concatenated. It is very
odd. the code goes like this, is very simple:

class myclass{
....
private string stringvar3;
....
private void mymethod(stringvar1, stringvar2, stringvar4)
{
string = stringvar1 + stringvar2 + stringvar3 + stringvar4;
}

Thanks,
Juan.

Nicholas Paldino said:
Juan,

Can you give a sample of code that shows the error?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Juan said:
A very simple concatenation stopped working. I have three variables that I
concatenate using +. When I debug I see the variables but the obtained
string just shows the first two variables.



Any idea.?



Thanks,

Juan.
 
Juan,

Is it that the debugger isn't showing the string correctly, or that the
value when used in the program is incorrect? If there are carriage
return/newline characters in the string, then the debugger doesn't always
process them properly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Juan said:
Nicholas,

string = stringvar1 + stringvar2 + stringvar3 + stringvar4



The string is concatenated until it reaches stringvar3 which is an instance
variable, after that the others variables don't get concatenated. When I
debug everything seem right but it just doesn't concatenated. It is very
odd. the code goes like this, is very simple:

class myclass{
...
private string stringvar3;
...
private void mymethod(stringvar1, stringvar2, stringvar4)
{
string = stringvar1 + stringvar2 + stringvar3 + stringvar4;
}

Thanks,
Juan.

"Nicholas Paldino [.NET/C# MVP]" <[email protected]> escribió
en el mensaje news:[email protected]...
Juan,

Can you give a sample of code that shows the error?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Juan said:
A very simple concatenation stopped working. I have three variables
that
 
The value is correct and the debugger is showing it, but the concatenaton is
not happening! It is related with the instance variable, it stops
concatenating when it reaches that variable. If i put it at the end the
result string gets built correctly...

Nicholas Paldino said:
Juan,

Is it that the debugger isn't showing the string correctly, or that the
value when used in the program is incorrect? If there are carriage
return/newline characters in the string, then the debugger doesn't always
process them properly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Juan said:
Nicholas,

string = stringvar1 + stringvar2 + stringvar3 + stringvar4



The string is concatenated until it reaches stringvar3 which is an instance
variable, after that the others variables don't get concatenated. When I
debug everything seem right but it just doesn't concatenated. It is very
odd. the code goes like this, is very simple:

class myclass{
...
private string stringvar3;
...
private void mymethod(stringvar1, stringvar2, stringvar4)
{
string = stringvar1 + stringvar2 + stringvar3 + stringvar4;
}

Thanks,
Juan.

"Nicholas Paldino [.NET/C# MVP]" <[email protected]> escribió
en el mensaje news:[email protected]...
Juan,

Can you give a sample of code that shows the error?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

A very simple concatenation stopped working. I have three variables
that
I
concatenate using +. When I debug I see the variables but the obtained
string just shows the first two variables.



Any idea.?



Thanks,

Juan.
 
Juan,

Post a complete example that will compile and run, and I will check it
out.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Juan said:
The value is correct and the debugger is showing it, but the concatenaton is
not happening! It is related with the instance variable, it stops
concatenating when it reaches that variable. If i put it at the end the
result string gets built correctly...

"Nicholas Paldino [.NET/C# MVP]" <[email protected]> escribió
en el mensaje news:#[email protected]...
Juan,

Is it that the debugger isn't showing the string correctly, or that the
value when used in the program is incorrect? If there are carriage
return/newline characters in the string, then the debugger doesn't always
process them properly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Juan said:
Nicholas,

string = stringvar1 + stringvar2 + stringvar3 + stringvar4



The string is concatenated until it reaches stringvar3 which is an instance
variable, after that the others variables don't get concatenated. When I
debug everything seem right but it just doesn't concatenated. It is very
odd. the code goes like this, is very simple:

class myclass{
...
private string stringvar3;
...
private void mymethod(stringvar1, stringvar2, stringvar4)
{
string = stringvar1 + stringvar2 + stringvar3 + stringvar4;
}

Thanks,
Juan.

"Nicholas Paldino [.NET/C# MVP]" <[email protected]> escribió
en el mensaje Juan,

Can you give a sample of code that shows the error?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

A very simple concatenation stopped working. I have three
variables
that
I
concatenate using +. When I debug I see the variables but the obtained
string just shows the first two variables.



Any idea.?



Thanks,

Juan.
 
Juan said:
The value is correct and the debugger is showing it, but the concatenaton is
not happening! It is related with the instance variable, it stops
concatenating when it reaches that variable. If i put it at the end the
result string gets built correctly...

I suspect you'll find that string has a null character (Unicode 0) at
the end of it. The rest of the data is probably there, but the debugger
isn't showing it. You probably want to look at where the data is coming
from and work out why you've got a null character - it's usually not
something you want to keep around.
 
Thank you Nicholas, it is working now. I still don't know why was it
happening, I was using a value returned form a socket server in the instance
variable, I stopped doing it and its working correctly.

Cheers,
Juan.

Nicholas Paldino said:
Juan,

Post a complete example that will compile and run, and I will check it
out.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Juan said:
The value is correct and the debugger is showing it, but the
concatenaton
is
not happening! It is related with the instance variable, it stops
concatenating when it reaches that variable. If i put it at the end the
result string gets built correctly...

"Nicholas Paldino [.NET/C# MVP]" <[email protected]> escribió
en el mensaje news:#[email protected]...
Juan,

Is it that the debugger isn't showing the string correctly, or
that
the
value when used in the program is incorrect? If there are carriage
return/newline characters in the string, then the debugger doesn't always
process them properly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas,

string = stringvar1 + stringvar2 + stringvar3 + stringvar4



The string is concatenated until it reaches stringvar3 which is an
instance
variable, after that the others variables don't get concatenated.
When
I
debug everything seem right but it just doesn't concatenated. It is very
odd. the code goes like this, is very simple:

class myclass{
...
private string stringvar3;
...
private void mymethod(stringvar1, stringvar2, stringvar4)
{
string = stringvar1 + stringvar2 + stringvar3 + stringvar4;
}

Thanks,
Juan.

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
escribió
en el mensaje Juan,

Can you give a sample of code that shows the error?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

A very simple concatenation stopped working. I have three variables
that
I
concatenate using +. When I debug I see the variables but the obtained
string just shows the first two variables.



Any idea.?



Thanks,

Juan.
 
Jon said:
I suspect you'll find that string has a null character (Unicode 0) at
the end of it. The rest of the data is probably there, but the debugger
isn't showing it. You probably want to look at where the data is coming
from and work out why you've got a null character - it's usually not
something you want to keep around.

I've had this issue countless times with the two Yahoo! Chat protocols.
They use null bytes in the headers for padding. It's so annoying when
it comes to debugging! :-)
 
Juan said:
Thank you Nicholas, it is working now. I still don't know why was it
happening, I was using a value returned form a socket server in the instance
variable, I stopped doing it and its working correctly.

My guess is that you were assuming that the socket's Receive method
filled a buffer with as many bytes as you specified you wanted.
*Always* look at the return value (same for Stream.Read) to find out
how much real data has actually been returned.

Of course, this might not have been the problem, but it's cropped up a
lot in the past, so I thought it would be worth mentioning!
 
C# Learner said:
I've had this issue countless times with the two Yahoo! Chat protocols.
They use null bytes in the headers for padding. It's so annoying when
it comes to debugging! :-)

Then again, this only happened during the time that I was using a
temporary hack to put binary and text data together in a string.
 
Back
Top