Problem with multi-line const strings and Environment.Newline

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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

.



Relevant Pages

  • Re: accessors
    ... void name(string const& n); ...
    (comp.lang.cpp)
  • Re: Finding a string literal in a *PGM object
    ... We work mostly on Unix where the "what" command is used to extract ... 1)Are string literals from the COBOL WORKING-STORAGE section VALUE ... QteRetrieveViewText APIs to get scannable compile listings or input ...
    (comp.sys.ibm.as400.misc)
  • Re: PRODUCT calculation in queries
    ... The Microsoft Jet database engine could not find the input table or query ... After pasting it into the code window, choose Compile on the Debug menu. ... Function Product(strField As String, strTable As String, _ ... Dim dblResult As Double ...
    (microsoft.public.access.queries)
  • Re: Help a beginner - simple lowercase to uppercase and so on function
    ... can understand everything but it should take shape with more practice ... it compiles and returns uppercase letters ... in this string!" ... to compile on both. ...
    (comp.lang.c)
  • Re: Compile time string literal substitution to external data file
    ... and these string literals may expose sensitive information ... > I want to use the compile time macros for file name, line number, and ... I'd put a macro and a function like: ...
    (microsoft.public.vc.language)