PC Review


Reply
Thread Tools Rate Thread

csharp IDE long lines to code to multiline

 
 
Peted
Guest
Posts: n/a
 
      9th May 2007
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
 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      9th May 2007
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


 
Reply With Quote
 
Peted
Guest
Posts: n/a
 
      9th May 2007
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



On Wed, 9 May 2007 10:45:48 +0100, "Marc Gravell"
<(E-Mail Removed)> wrote:

>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
>


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      9th May 2007
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


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      9th May 2007
<Peted> wrote:
> 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("...");

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
per9000
Guest
Posts: n/a
 
      9th May 2007
On 9 Maj, 11:45, "Marc Gravell" <marc.grav...@gmail.com> wrote:
> [...]
> 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

--

Per Erik Strandberg
Tomlab Optimization Inc.
http://tomopt.com/tomnet/

 
Reply With Quote
 
Peted
Guest
Posts: n/a
 
      9th May 2007

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

dont know how i missed all that.

thanks


Peted


On Wed, 9 May 2007 11:03:59 +0100, Jon Skeet [C# MVP]
<(E-Mail Removed)> wrote:

> <Peted> wrote:
>> 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("...");


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      9th May 2007
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


 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      9th May 2007
per9000 wrote:
> 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
_____
http://www.guffa.com
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      9th May 2007
Göran Andersson <(E-Mail Removed)> wrote:
> per9000 wrote:
> > 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.


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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LONG CODE LINES => CRASH-STARTER PACALA_BA Microsoft Access 0 4th Dec 2008 06:19 PM
How to split Lines of VB Code too Long? =?Utf-8?B?anJ0bWF4?= Microsoft Access Form Coding 3 12th Oct 2007 12:28 AM
csharp skipping lines of code after a call...? Alex Microsoft C# .NET 4 15th Dec 2006 06:20 PM
reading several lines from multiline textbox Jimbo Microsoft C# .NET 1 23rd Mar 2004 04:52 PM
Long lines of code - help! Frank Microsoft C# .NET 1 16th Oct 2003 07:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:01 AM.