473,322 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem with multi-line const strings and Environment.Newline

I want to declare a const multi-line string inside a method, and I am
having some problems using Environment.NewLine.

I started out with:

class foo {

public void PrintStuff() {
const string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);
}

}

Trying to compile this gives me:

"Error 'multiline' is of type 'string.' A const of reference type
other than string can only be initialized with null."

Somewhat confusing, since 'multiline' _is_ of type string so the rest
of the error message does not apply. It does underline the last of
the Environment.NewLine's which is presumably what it is objecting to.
Experimenting with taking them out shows that any Environment.NewLine
will cause a failure to compile, with the last one underlined.

This string is only used inside the method, and is never changed. It
will compile as readonly outside the method, but that makes for
useless clutter at the class level. It will compile as a non-const
string, but that is not as safe as I would like it to be. It will
compile with a hard coded '\n' in the strings, but that is not as
portable as I would want it to be.

How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?

rossum

Dec 1 '06 #1
11 6171
I may be old fashioned, but you just can't beat the old "\n", in my view:

class foo {

public void PrintStuff() {
const string multiline =
"The first line." + "\n" +
"The second line." + "\n" +
"The third line.";
Console.WriteLine(multiline);
}
}

It works for me.
Peter
"rossum" <ro******@coldmail.comwrote in message
news:31********************************@4ax.com...
>I want to declare a const multi-line string inside a method, and I am
having some problems using Environment.NewLine.

I started out with:

class foo {

public void PrintStuff() {
const string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);
}

}

Trying to compile this gives me:

"Error 'multiline' is of type 'string.' A const of reference type
other than string can only be initialized with null."

Somewhat confusing, since 'multiline' _is_ of type string so the rest
of the error message does not apply. It does underline the last of
the Environment.NewLine's which is presumably what it is objecting to.
Experimenting with taking them out shows that any Environment.NewLine
will cause a failure to compile, with the last one underlined.

This string is only used inside the method, and is never changed. It
will compile as readonly outside the method, but that makes for
useless clutter at the class level. It will compile as a non-const
string, but that is not as safe as I would like it to be. It will
compile with a hard coded '\n' in the strings, but that is not as
portable as I would want it to be.

How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?

rossum

Dec 1 '06 #2

rossum wrote:
<snip>
How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?
The short answer: you can't. Environment.NewLine is a property, not a
constant, so using it as part of the declaration of a constant would
involve the compiler having to invoke a run-time property at
compile-time, which it can't do. The C# compiler doesn't generate code
to produce const objects; if it can't compute them at compile time then
it barfs.

Dec 1 '06 #3
Hey,

You can do away with the newlines too. Just do this:

const string multiline =
@"The first line.
The second line.
The third line.";
On Fri, 01 Dec 2006 17:06:49 +0000, rossum <ro******@coldmail.com>
wrote:
>I want to declare a const multi-line string inside a method, and I am
having some problems using Environment.NewLine.

I started out with:

class foo {

public void PrintStuff() {
const string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);
}

}

Trying to compile this gives me:

"Error 'multiline' is of type 'string.' A const of reference type
other than string can only be initialized with null."

Somewhat confusing, since 'multiline' _is_ of type string so the rest
of the error message does not apply. It does underline the last of
the Environment.NewLine's which is presumably what it is objecting to.
Experimenting with taking them out shows that any Environment.NewLine
will cause a failure to compile, with the last one underlined.

This string is only used inside the method, and is never changed. It
will compile as readonly outside the method, but that makes for
useless clutter at the class level. It will compile as a non-const
string, but that is not as safe as I would like it to be. It will
compile with a hard coded '\n' in the strings, but that is not as
portable as I would want it to be.

How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?

rossum
--

Bits.Bytes.
http://bytes.thinkersroom.com
Dec 1 '06 #4
True, but the OP is looking for portability across platforms.

Using \n and the @ trick aren't portable. Using \n (or \r\n) embeds a
particular newline sequence in the string. The @ trick will (I believe)
embed newlines from the platform on which the code is compiled.

Only Environment.NewLine adapts to the platform on which the code is
run. However, it's not a constant, so you can't use it to define
constants.

Rad [Visual C# MVP] wrote:
Hey,

You can do away with the newlines too. Just do this:

const string multiline =
@"The first line.
The second line.
The third line.";
On Fri, 01 Dec 2006 17:06:49 +0000, rossum <ro******@coldmail.com>
wrote:
I want to declare a const multi-line string inside a method, and I am
having some problems using Environment.NewLine.

I started out with:

class foo {

public void PrintStuff() {
const string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);
}

}

Trying to compile this gives me:

"Error 'multiline' is of type 'string.' A const of reference type
other than string can only be initialized with null."

Somewhat confusing, since 'multiline' _is_ of type string so the rest
of the error message does not apply. It does underline the last of
the Environment.NewLine's which is presumably what it is objecting to.
Experimenting with taking them out shows that any Environment.NewLine
will cause a failure to compile, with the last one underlined.

This string is only used inside the method, and is never changed. It
will compile as readonly outside the method, but that makes for
useless clutter at the class level. It will compile as a non-const
string, but that is not as safe as I would like it to be. It will
compile with a hard coded '\n' in the strings, but that is not as
portable as I would want it to be.

How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?

rossum
--

Bits.Bytes.
http://bytes.thinkersroom.com
Dec 1 '06 #5

Bruce Wood wrote:
True, but the OP is looking for portability across platforms.

Using \n and the @ trick aren't portable. Using \n (or \r\n) embeds a
particular newline sequence in the string. The @ trick will (I believe)
embed newlines from the platform on which the code is compiled.

Only Environment.NewLine adapts to the platform on which the code is
run. However, it's not a constant, so you can't use it to define
constants.

Rad [Visual C# MVP] wrote:
Hey,

You can do away with the newlines too. Just do this:

const string multiline =
@"The first line.
The second line.
The third line.";
On Fri, 01 Dec 2006 17:06:49 +0000, rossum <ro******@coldmail.com>
wrote:
>I want to declare a const multi-line string inside a method, and I am
>having some problems using Environment.NewLine.
>
>I started out with:
>
>class foo {
>
public void PrintStuff() {
const string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);
}
>
>}
>
>Trying to compile this gives me:
>
>"Error 'multiline' is of type 'string.' A const of reference type
>other than string can only be initialized with null."
>
>Somewhat confusing, since 'multiline' _is_ of type string so the rest
>of the error message does not apply. It does underline the last of
>the Environment.NewLine's which is presumably what it is objecting to.
>Experimenting with taking them out shows that any Environment.NewLine
>will cause a failure to compile, with the last one underlined.
>
>This string is only used inside the method, and is never changed. It
>will compile as readonly outside the method, but that makes for
>useless clutter at the class level. It will compile as a non-const
>string, but that is not as safe as I would like it to be. It will
>compile with a hard coded '\n' in the strings, but that is not as
>portable as I would want it to be.
>
>How can I declare an unchanging multi-line string inside a method
>while still using Environment.NewLine as the line separator?
>
>rossum
--

Bits.Bytes.
http://bytes.thinkersroom.com
Thankyou for that painful bit of information. Now I get to look
forward to converting all my crap over to "Environment.Newline" so that
I can maintain some idiotic ideological connection to the Linux world
in my little opensource hobbyhorse apps.

Dec 1 '06 #6
Chasing cross platform compatibility is harder than it looks, and if
you really want to do it you must think of many other things like file
separators, drive access (or lack thereof), legal and illegal file
names, security etc.

Personally I would not go out of my way to do it.

But if you really really have to do it, you can omit the cost and the
code will compile and run merrily

string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);

On 1 Dec 2006 10:59:46 -0800, "Bruce Wood" <br*******@canada.com>
wrote:
>True, but the OP is looking for portability across platforms.

Using \n and the @ trick aren't portable. Using \n (or \r\n) embeds a
particular newline sequence in the string. The @ trick will (I believe)
embed newlines from the platform on which the code is compiled.

Only Environment.NewLine adapts to the platform on which the code is
run. However, it's not a constant, so you can't use it to define
constants.

Rad [Visual C# MVP] wrote:
>Hey,

You can do away with the newlines too. Just do this:

const string multiline =
@"The first line.
The second line.
The third line.";
On Fri, 01 Dec 2006 17:06:49 +0000, rossum <ro******@coldmail.com>
wrote:
>I want to declare a const multi-line string inside a method, and I am
having some problems using Environment.NewLine.

I started out with:

class foo {

public void PrintStuff() {
const string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);
}

}

Trying to compile this gives me:

"Error 'multiline' is of type 'string.' A const of reference type
other than string can only be initialized with null."

Somewhat confusing, since 'multiline' _is_ of type string so the rest
of the error message does not apply. It does underline the last of
the Environment.NewLine's which is presumably what it is objecting to.
Experimenting with taking them out shows that any Environment.NewLine
will cause a failure to compile, with the last one underlined.

This string is only used inside the method, and is never changed. It
will compile as readonly outside the method, but that makes for
useless clutter at the class level. It will compile as a non-const
string, but that is not as safe as I would like it to be. It will
compile with a hard coded '\n' in the strings, but that is not as
portable as I would want it to be.

How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?

rossum
--

Bits.Bytes.
http://bytes.thinkersroom.com
--

Bits.Bytes.
http://bytes.thinkersroom.com
Dec 1 '06 #7
On Fri, 01 Dec 2006 17:06:49 +0000, rossum <ro******@coldmail.com>
wrote:
>I want to declare a const multi-line string inside a method, and I am
having some problems using Environment.NewLine.

I started out with:

class foo {

public void PrintStuff() {
const string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);
}

}

Trying to compile this gives me:

"Error 'multiline' is of type 'string.' A const of reference type
other than string can only be initialized with null."

Somewhat confusing, since 'multiline' _is_ of type string so the rest
of the error message does not apply. It does underline the last of
the Environment.NewLine's which is presumably what it is objecting to.
Experimenting with taking them out shows that any Environment.NewLine
will cause a failure to compile, with the last one underlined.

This string is only used inside the method, and is never changed. It
will compile as readonly outside the method, but that makes for
useless clutter at the class level. It will compile as a non-const
string, but that is not as safe as I would like it to be. It will
compile with a hard coded '\n' in the strings, but that is not as
portable as I would want it to be.

How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?

rossum
Thankyou all for your replies. It looks like I am going to have to
settle for a less than ideal solution.

On a minor point, the reason for not wanting '\n' is not for Linux
compatibility, which is not really practicable at the moment, it is
for compatibility with some Windows editors (Notepad etc.) which treat
'\n' as an unknown character and put all the separate lines on one
line with an "unknown character" box where the newline should be.

The actual program is writing to disk and not the console as my small
example did, so I need to be able to edit the output easily.

rossum

Dec 1 '06 #8

Rad [Visual C# MVP] wrote:
Chasing cross platform compatibility is harder than it looks, and if
you really want to do it you must think of many other things like file
separators, drive access (or lack thereof), legal and illegal file
names, security etc.
Yes. You have to make sure that you use System.IO.Path for manipulating
file names, etc.
Personally I would not go out of my way to do it.
I agree. I wonder how many people are really trying to run .NET apps
under Mono,and how many will in the future.

Nonetheless, portability was the OP's stated goal, so....

Dec 1 '06 #9
rossum <ro******@coldmail.comwrote:
>On a minor point, the reason for not wanting '\n' is not for Linux
compatibility, which is not really practicable at the moment, it is
for compatibility with some Windows editors (Notepad etc.) which treat
'\n' as an unknown character and put all the separate lines on one
line with an "unknown character" box where the newline should be.
I guess it depends what you mean by "linux compatibility".

I write my cross-platform programs to work equally with \n, \r, \r\n
as line-separators. Emacs and Pico work equally with all of them. And
RFC822 for instance stipulates CRLF as its line-separator, so unix
mail programs are supposed to use \r\n.

I think we're in the age now where files are expected to work on unix
and windows and mac without any CR/LF translations: no one's using FTP
ascii mode anymore to transfer files.

--
Lucian
Dec 1 '06 #10
Lucian Wischik <lu***@wischik.comwrote:
rossum <ro******@coldmail.comwrote:
On a minor point, the reason for not wanting '\n' is not for Linux
compatibility, which is not really practicable at the moment, it is
for compatibility with some Windows editors (Notepad etc.) which treat
'\n' as an unknown character and put all the separate lines on one
line with an "unknown character" box where the newline should be.

I guess it depends what you mean by "linux compatibility".

I write my cross-platform programs to work equally with \n, \r, \r\n
as line-separators. Emacs and Pico work equally with all of them. And
RFC822 for instance stipulates CRLF as its line-separator, so unix
mail programs are supposed to use \r\n.

I think we're in the age now where files are expected to work on unix
and windows and mac without any CR/LF translations: no one's using FTP
ascii mode anymore to transfer files.
It entirely depends what you're doing. If you're writing a string which
will be displayed in a textbox or a label, I seem to remember that on
Windows with WinForms you *must* use \r\n. I've no idea what Mono does
on that front.

Likewise some network protocols specify one particular line termination
style or another.

I would use Environment.NewLine when writing a plain text file, but for
a lot of the rest of the time it's worth putting a bit of effort into
thinking where exactly the output is going, and whether that format has
a specific line termination requirement.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 1 '06 #11

rossum wrote:
On Fri, 01 Dec 2006 17:06:49 +0000, rossum <ro******@coldmail.com>
wrote:
I want to declare a const multi-line string inside a method, and I am
having some problems using Environment.NewLine.

I started out with:

class foo {

public void PrintStuff() {
const string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);
}

}

Trying to compile this gives me:

"Error 'multiline' is of type 'string.' A const of reference type
other than string can only be initialized with null."

Somewhat confusing, since 'multiline' _is_ of type string so the rest
of the error message does not apply. It does underline the last of
the Environment.NewLine's which is presumably what it is objecting to.
Experimenting with taking them out shows that any Environment.NewLine
will cause a failure to compile, with the last one underlined.

This string is only used inside the method, and is never changed. It
will compile as readonly outside the method, but that makes for
useless clutter at the class level. It will compile as a non-const
string, but that is not as safe as I would like it to be. It will
compile with a hard coded '\n' in the strings, but that is not as
portable as I would want it to be.

How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?

rossum
Thankyou all for your replies. It looks like I am going to have to
settle for a less than ideal solution.

On a minor point, the reason for not wanting '\n' is not for Linux
compatibility, which is not really practicable at the moment, it is
for compatibility with some Windows editors (Notepad etc.) which treat
'\n' as an unknown character and put all the separate lines on one
line with an "unknown character" box where the newline should be.

The actual program is writing to disk and not the console as my small
example did, so I need to be able to edit the output easily.
Have you tried embedding \r\n in the string rather than \n? \r\n is the
standard Windows line termination sequence....

Dec 1 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chung Jiho | last post by:
Hello, We have been deploying our web application over win2k servers. It is written in Python and uses Active Scripting to meet our requirements that it must be ASP application. So far, it was...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
2
by: RipTide | last post by:
Background: Using an unsupported/abandoned multi-user multi-database program that uses Access 97 and Jet 3.5. Program itself appears to have been built with PowerBuilder 6.5. Databases reside on...
2
by: google | last post by:
Hello everyone, I am having an issue using the "Multi Select" option in a list box in MS Access 2003. I am making a form that users can fill out to add an issue to the database. Each issue can...
4
by: snowweb | last post by:
I am trying to implement a CSS hierarchical unfolding menu on a site. The thing is, it needs to be dynamically populated from the results of a database query. I previously had the menu working but...
2
by: Diz | last post by:
Can anyone please help with this? when i run the following query, using SELECT*, SELECT * FROM tbl_artwork, tbl_artworkmedium, tbl_medium WHERE tbl_artwork.artworkID =...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
0
by: pvannie | last post by:
Hello there, I've been trying to install DBD::mysql on Mac OS X Server 10.3 for 2 days, but it still raises error. So I'm going to send this message to ask for help. Any help will be much...
1
by: pvannie | last post by:
Hello there, I've been trying to install DBD::mysql on Mac OS X Server 10.3 for 2 days, but it still raises error. So I'm going to send this message to ask for help. Any help will be much...
19
by: 叮叮当当 | last post by:
hi, all when a email body consist with multipart/alternative, i must know when the boundary ends to parse it, but the email lib have not provide some function to indicate the boundary end,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.