csharp IDE long lines to code to multiline

  • Thread starter Thread starter Peted
  • Start date Start date
P

Peted

Hi,

im using csharp express 2005.

I have a problem in the IDE, when i have along line of code or a long
line of text in a static string, i want to make it multi line for
easir readability, but using <return> or <shift return> dosnt seem to
allow for breaking long code lines into mutliline and still compile

Im sure ive seen this done, but how is it done in the ide ?

thanks

Peted
 
With strings, you have two basic options:

a: use the @ prefix and include returns in the string - i.e.

string longString = @"this is a
long string that contains
line breaks";

or b: use a few strings (in the source), and let the compiler stitch
them together, i.e.

string longString = "this is a\n" +
"long string that contains\n" +
"line breaks";

Any use?

Marc
 
that should have occured to me thanks.

what about long lines of c# code, not that that will be a comman
problem, but im curious just in case i end up with a long line of
actual c# code, is there any way to make that multi line and still
compile?


thanks

Peted
 
c# is free-form; only a few things (literals, comments etc) are fussy
about line breaks; the following compiles just fine (but is ugly as
sin ;-p):
foreach
(
char
c
in
"abcdef"
) {
Console.WriteLine
(
c);
}

Marc
 
that should have occured to me thanks.

what about long lines of c# code, not that that will be a comman
problem, but im curious just in case i end up with a long line of
actual c# code, is there any way to make that multi line and still
compile?

You can generally just break it between any two parts, e.g.

someReference.CallSomeMethod("First parameter",
"Second parameter")
.CallAnotherMethod("...");
 
[...]
string longString = "this is a\n" +
"long string that contains\n" +
"line breaks";
[...]

I tend to prefer a use a string br = Environment.NewLine instead of
\n, just in case.

using System;
using System.Text;

class Program
{
static void Main(string[] args)
{
string br = Environment.NewLine;

string foo = "abra" + br
+ "kadabra" + br
+ "ala kazam";

Console.WriteLine(foo);
}
}

/Per
 
HAHA, ok i see, its only the strings i need to worry about

dont know how i missed all that.

thanks


Peted
 
Just note that this pushes concatenation into run-time rather than
compile-time, since this isn't a constant... just something to note in
a tight loop ;-p

Marc
 
per9000 said:
I tend to prefer a use a string br = Environment.NewLine instead of
\n, just in case.

I think that is good advice. In a Windows system Environment.NewLine
does not contain "\n" but "\r\n".

As Marc pointed out, the compiler will most probably not be able to
concatenate the strings at compile time.

However, it's questionable if string literals should even contain line
breaks. Perhaps an array of strings is a better way to represent data
like this, and adding the line breaks by using WriteLine to write each
string. :)
 
Göran Andersson said:
I think that is good advice. In a Windows system Environment.NewLine
does not contain "\n" but "\r\n".

As Marc pointed out, the compiler will most probably not be able to
concatenate the strings at compile time.

However, it's questionable if string literals should even contain line
breaks. Perhaps an array of strings is a better way to represent data
like this, and adding the line breaks by using WriteLine to write each
string. :)

It all depends on what you're doing with it. If you're writing to the
console, then yes - but if you're getting the data ready to go down a
socket, then:
a) It makes sense to use a single string and encode it once
b) You need to make sure you use the appropriate line termination for
the protocol, rather than using Environment.NewLine.

It's certainly not the case that everywhere you see \n or \r\n you
should replace it with a call to Environment.NewLine.
 
Hi,

im using csharp express 2005.

I have a problem in the IDE, when i have along line of code or a long
line of text in a static string, i want to make it multi line for
easir readability, but using <return> or <shift return> dosnt seem to
allow for breaking long code lines into mutliline and still compile

Im sure ive seen this done, but how is it done in the ide ?

thanks

Peted

Why not just put the string in a resource file? Goto the project
properties page, click the Resources tab. If you don't have one,
there will be a link to create one. Click it, and off you go. Then
you can reference the string like so:

MessageBox.Show( Properties.Resources.MyLongString );
 
It all depends on what you're doing with it. If you're writing to the
console, then yes - but if you're getting the data ready to go down a
socket, then:
a) It makes sense to use a single string and encode it once
b) You need to make sure you use the appropriate line termination for
the protocol, rather than using Environment.NewLine.

So, what should I use ? MyProtocol.NewLine ? ;-)
It's certainly not the case that everywhere you see \n or \r\n you
should replace it with a call to Environment.NewLine.

But when shall one use it and when not?

Christof
 
Andy said:
Why not just put the string in a resource file? Goto the project
properties page, click the Resources tab. If you don't have one,
there will be a link to create one. Click it, and off you go. Then
you can reference the string like so:

This surely is the right place for UI-Strings, wich will change with the
language/culture.
But there are other cases like RegExp-patterns SQL-Statements etc.
These shouldn't be placed in resource files. At least, this is my
expierience.

Christof
 
So, what should I use ? MyProtocol.NewLine ? ;-)

Well, you could define a NewLine constant in an appropriate class,
certainly - that wouldn't be a bad idea. Specify it to be "\r\n" or
"\n" depending on the protocol.
But when shall one use it and when not?

Use Environment.NewLine when you want the current platform's default
line terminator, e.g. when writing text files for use on the local
machine. Use a specific line terminator in the cases where you know
for certain exactly which one you want to use.

Jon
 

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

Back
Top