views:

114

answers:

4

So I have this code:

/* variables already initialized:
   int numFlips
   int numAggrFlips
   double pctAggrFlips
*/

String flipsMessage = String.Format(
    "Flips: {0} / Aggr: {1} ({2})",
    numFlips, numAggrFlips, pctAggrFlips.ToString("0.0%")
);

For some mysterious reason, the output ends up being the following string:

(Flips: 0 / Aggr: 0 (0.0%

Any idea what would cause the parentheses to get all messed up like this?

To add to (or perhaps explain?) the strangeness: this problem DOESN'T occur on my development machine, using Windows XP. The string appears as expected. The problem does occur on our production machines (using the same code), using Windows Server 2008.

A: 

hmmm I'd guess you have a carriage-return sneaking in somewhere.

Jimmy
A carriage-return does not explain a leading "`(`" in the output.
Andrew Hare
I guessed he mis-typed a leading ")"
Jimmy
but I guess you're right, simply a \r would overwrite the F
Jimmy
+3  A: 

How about this:

String flipsMessage = String.Format(
    "Flips: {0} / Aggr: {1} ({2:P})",
    numFlips, numAggrFlips, pctAggrFlips
);
huseyint
+1  A: 

I suspect you are misreading something... in your example, not only is one of the parentheses moved to the beginning, but a closing parenetheses has been replaced by an opening parentheses... My best guess based on the minimal data you present is that the output you are showing is being generated by a code snippet somewhere else in your solution that you are not looking at... I Would carefully step through the code, in debug mode, line by line, and make sure that this line is actually the one that is ouputting the displayed value.

Also, stop, and see what the value of the variable flipsMessage evaluates to in debug mode.

Charles Bretana
A: 

I'm going to guess that you are outputting this message to some sort of screen control whose MaxLength property is set to the same length as your message (or perhaps a DataTable with a column type that has a MaxLength). Before the message gets output, however, the entire string is sometimes getting surrounded by parentheses.

flipsMessage = Flips: 0 / Aggr: 0 (0.0%) // length: 25
flipsWParens = (Flips: 0 / Aggr: 0 (0.0%)) // length: 27
flipsTrunced = (Flips: 0 / Aggr: 0 (0.0% // length: 25

On some machines, those extra parentheses don't get added, so you don't have the problem. I have no idea why it would be different from machine to machine, though.

DanM