469,286 Members | 2,539 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

C++\CLI static const System::String^ not supported?

I'm converting MC++ to C++/CLI and for some reason it's not cool with

static const System::String^

How am I supposed to have static constant strings then?

Feb 13 '07 #1
6 14896
On Feb 13, 2:47 pm, "DaTurk" <mmagd...@hotmail.comwrote:
I'm converting MC++ to C++/CLI and for some reason it's not cool with

static const System::String^

How am I supposed to have static constant strings then?
That's weird. Perhaps you can post more code. From what I
understand, String is in fact immutable, and if you intend to mutate a
lot of strings use the more efficient StringBuilder class.

For String don't forget to always instantiate with gcnew or String
^AStr = "Some Text";

RL

Feb 14 '07 #2
On Feb 13, 7:43 pm, "raylopez99" <raylope...@yahoo.comwrote:
On Feb 13, 2:47 pm, "DaTurk" <mmagd...@hotmail.comwrote:
I'm converting MC++ to C++/CLI and for some reason it's not cool with
static const System::String^
How am I supposed to have static constant strings then?

That's weird. Perhaps you can post more code. From what I
understand, String is in fact immutable, and if you intend to mutate a
lot of strings use the more efficient StringBuilder class.

For String don't forget to always instantiate with gcnew or String
^AStr = "Some Text";

RL
THis is a snippet out of the CLI header file

// P U B L I C
public:
//Constants
static const int test = 1;
static const System::String^ KEY_USERNAME = "UserName";

and the error I get is, well the warning is

warning C4400: 'const System::String ^' : const/volatile qualifiers on
this type are not supported

it's a const, so I'm not newing it myself, I'm assigning it a string
literal;

Feb 14 '07 #3
I'm converting MC++ to C++/CLI and for some reason it's not cool with

static const System::String^

How am I supposed to have static constant strings then?
Please see "literal" keyword to resolve the issue.
--
Vladimir Nesterovsky
Feb 14 '07 #4

"DaTurk" <mm******@hotmail.coma écrit dans le message de news:
11**********************@h3g2000cwc.googlegroups.c om...
I'm converting MC++ to C++/CLI and for some reason it's not cool with

static const System::String^

How am I supposed to have static constant strings then?
"static const" cannot bu used in C++/CLI. You must use "literal" isntead. I
am not sure to understand why MS introduced this new keyword ("literal"
produces some special meta-data in the class type descriptor in MSIL, but I
fail to see why they couldn't use "static const" to the same effect).

The details are explained here but I fail to see the reason for this change
: http://msdn2.microsoft.com/en-gb/lib...01(vs.80).aspx

Arnaud
MVP - VC
Feb 14 '07 #5
You have two alternatives:
1. static System::String ^ const foo = "a";
2. literal System::String ^foo = "a";

Note that the first alternative, minus the 'static', is the only way to
declare method-level string constants in C++/CLI.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter
"DaTurk" wrote:
I'm converting MC++ to C++/CLI and for some reason it's not cool with

static const System::String^

How am I supposed to have static constant strings then?

Feb 19 '07 #6

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:B1**********************************@microsof t.com...
You have two alternatives:
1. static System::String ^ const foo = "a";
2. literal System::String ^foo = "a";

Note that the first alternative, minus the 'static', is the only way to
declare method-level string constants in C++/CLI.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter
"DaTurk" wrote:
>I'm converting MC++ to C++/CLI and for some reason it's not cool with

static const System::String^

How am I supposed to have static constant strings then?

That would be a handle to a constant String. .NET does not have a concept
of a syntactically immutable object, only by semantics of the implementation
are objects immutable (and Strings are pretty much immutable without needed
to specify const). What you want is a name that always refers to the same
String objects, that's a constant handle. Dave pointed out that you would
write that as "String^ const". That's still not compile-time constant, the
value must be loaded out of the declaring assembly at JIT time. Only the
literal keyword gives you true compile-time constness.
Feb 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jan13 | last post: by
3 posts views Thread by Steven T. Hatton | last post: by
4 posts views Thread by cppsks | last post: by
10 posts views Thread by Dave | last post: by
2 posts views Thread by Drew McCormack | last post: by
5 posts views Thread by John Goche | last post: by
7 posts views Thread by Spoon | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.