473,386 Members | 1,745 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,386 software developers and data experts.

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 15625
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jan13 | last post by:
Hi, I'm new to programming in C++ (using VC6) and ran into the following problem: I want to declare and define a class member variable as 'static const', but something seems to go wrong with...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
4
by: cppsks | last post by:
"Defining static const variables inside the class is not universally supported yet, so for now I guess you'll have to move the definition out of the body of the class. No, static const inside...
14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
10
by: Dave | last post by:
const static int ARRAY_SIZE = 4; Comeau online gives this warning: "ComeauTest.c", line 10: warning: storage class is not first const static int ARRAY_SIZE = 4; Why is static const...
2
by: Drew McCormack | last post by:
I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have a header with the following const variables with namespace scope: namespace Periphery { extern const double...
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
7
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.