views:

123

answers:

5

Trying to override a tostring in one of my classes.

 return string.Format(@" name = {0}
                         ID = {1}
                         sec nr = {2}
                         acc nr = {3}", string, int, int ,int); // types

But the thing is, the result isn't aligned when printed out:

name = test
                                   ID = 42
                                   sec nr = 11
                                   acc nr = 55

Trying to add \n just prints it out without formating. Guessing it has something to do with @"" which I'm using for multi-lining.

Would like it to print out :

name = test
ID = 42
sec nr = 11
acc nr = 55
+3  A: 

If you add spaces in front, it will be printed that way.

I normally do it like this.

   return string.Format(
@" name = {0}
 ID = {1}
 sec nr = {2}
 acc nr = {3}", string, int, int ,int); // types

Update: Perhaps a prettier alternative:

string[] lines = 
{
  " name = {0}",
  " ID = {1}",
  " sec nr = {2}",
  " acc nr = {3}"
};

return string.Format(
         string.Join(Environment.Newline, lines), 
         arg0, arg1, arg2, arg3);
leppie
That works, so it's a solution, but the code looks so ugly without indentation. Can one do this somewhat "better"?
Milan
@Zka: Not really, thats pretty much the best looking you can get it :(
leppie
Thanks for the alternative, it looks better and is quite manageable for my purposes.
Milan
+2  A: 

The @ before the string switches off standard C# string formatting, try

 return string.Format(" name = {0}\n ID = {1} \n sec nr = {2} \n acc nr = {3}", 
                      string, int, int ,int); // types

You can't use the @ and use \n, \t etc.

EDIT

This is - IMHO - as good as it gets

 return string.Format("name = {0}\n" + 
                      "ID = {1}\n" + 
                      "sec nr = {2}\n" + 
                      "acc nr = {3}", 
                       string, int, int ,int); 
Binary Worrier
\n is not very Windows friendly...
leppie
Actually yea, that works but I really need it to be multi-line as there is more information to be added making it a very long line.
Milan
@leppie: Sorry, why not Windows Friendly?
Binary Worrier
@Binary Worrier: \n is not the standard newline on Windows. I know some controls wont work unless you get them \n\r.
leppie
@leppie: Thanks, I'm mostly server side, so haven't noticed.
Binary Worrier
A: 

No, it's all the spaces you have in there.

ben
+1 neutralized. What's wrong with this Answer? It's more than a reason rather than a solution i admit however.
Veer
Yeah, should have put a little more effort into this answer. Mea culpa.
ben
+2  A: 

I believe your string includes the spaces. Since the first line only has 1 leading space, it's naturally not aligned with the other lines. If you still want multilined code, try this:

return string.Format(
    @"name = {0}\n" +
    @"ID = {1}\n" + 
    ...
    string, int, int ,int); // types

You can also use a StringBuilder and it's Append or AppendLine methods.

mafutrct
A: 

A solution from msdn:

// Sample for the Environment.NewLine property
using System;

class Sample 
{
    public static void Main() 
    {
    Console.WriteLine();
    Console.WriteLine("NewLine: {0}  first line{0}  second line{0}  third line",
                          Environment.NewLine);
    }
}
/*
This example produces the following results:

NewLine:
  first line
  second line
  third line
*/
newtover