String concanation bug??

C

Casper

I am seeing some really strange behavior in what should be basic
language constucts. Sometimes, when forming a new string (a) by
concanating other strings (b + c), only the first substring (b) gets
into this new string (a).

More specifically, I am concanating elements I extract from the
Arraylist type. I run .ToString() on these extractions so this should
not pose the problem.

Never the less, when concanating and trying to use this concanation as
argument for methods, it will fail in cases where the first element is
composed of alphanumeric characters - strangely enough, concanation
using values (also made ToSting() though) works fine.

// Nick (string) + IP address (string) + Port (int) yeilds ONLY the
nick!
for(int i = 0; i < PeerListGUID.Count; i++)
lstPeers.Items.Add(PeerListNick.ToString() +
PeerListIP.ToString() + "@" + PeerListPort.ToString());

// IP address (string) + Port (int) + Nick (string) yeilds desired
result
for(int i = 0; i < PeerListGUID.Count; i++)
lstPeers.Items.Add(PeerListIP.ToString() + "@" +
PeerListPort.ToString() + " " + PeerListNick.ToString());

....debugging shows its not my arrays which are "out of sync". Its
simply the concatenations that goes wrong!

Any help would be greatly appreciated,
Casper Bang, Denmark
 
J

Jon Skeet [C# MVP]

Casper said:
I am seeing some really strange behavior in what should be basic
language constucts. Sometimes, when forming a new string (a) by
concanating other strings (b + c), only the first substring (b) gets
into this new string (a).

I'm pretty sure you'll find that what you're seeing isn't necessarily
what's going on. For instance, a null character (0) in the string can
confuse the debugger in VS 2002.

I suggest you try to write a short but complete program which
demonstrates the problem. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that. The first thing to do is have a look at what the variable below
looks like in the debugger:

using System;

public class Test
{
static void Main()
{
string x = "Hello\0there";
}
}

If it only looks like "Hello" then chances are the same problem is
coming up in your real code, and you've got nulls there that you
weren't expecting. Does the nickname part come from some code which is
giving null-terminated strings?

I very much doubt that it's actual string concatenation which is
failing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top