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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DaTurk

    #1

    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?

  • raylopez99

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

    On Feb 13, 2:47 pm, "DaTurk" <mmagd...@hotma il.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

    Comment

    • DaTurk

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

      On Feb 13, 7:43 pm, "raylopez99 " <raylope...@yah oo.comwrote:
      On Feb 13, 2:47 pm, "DaTurk" <mmagd...@hotma il.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;

      Comment

      • Vladimir Nesterovsky

        #4
        Re: 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?
        Please see "literal" keyword to resolve the issue.
        --
        Vladimir Nesterovsky


        Comment

        • Arnaud Debaene

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


          "DaTurk" <mmagdits@hotma il.coma écrit dans le message de news:
          1171406879.5078 03.249680@h3g20 00...legro ups.com...
          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


          Comment

          • =?Utf-8?B?RGF2aWQgQW50b24=?=

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

            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
            Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

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

            Comment

            • Ben Voigt

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


              "David Anton" <DavidAnton@dis cussions.micros oft.comwrote in message
              news:B185D504-6730-466A-98A2-2C7D86E2AD86@mi crosoft.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
              Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

              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.


              Comment

              Working...