Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. static const from MC++ to C# via DLL [modified]

static const from MC++ to C# via DLL [modified]

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++helpquestiondotnet
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ShermansLagoon
    wrote on last edited by
    #1

    I have two projects, one "Visual C++/CLR/Class Library" (DLL file) and one "Visual C#/Windows Application" The DLL file: namespace Foo { // first attempt public ref class RC { public: static const System::Int16 value = 0; }; // second attempt public enum class EC : System::Int16 { value = 0; }; } In the application I have the following problem: static class Program { static void Main() { Int16 sh = Foo.EC.value; // error CS0266; must use explicit type cast switch(sh) { case Foo.RC.value: // error CS0150; the value is not constant MessageBox.Show("Information"); break; } return 0; } I need to solve one of these problems (which one is not that important), i.e. to have implicit type casting from an enumeration to a short (or other type of integer), or to get a "static const" defined in MC++ to actually be a constant value. I have looked all over the net trying to find a solution for this, but it seems that this behaviour actually is what is intended, is it really so? Forgot to add, I am using VS 2005, i.e. .NET 2.0 -- modified at 5:37 Monday 8th January, 2007 Internet - the worlds biggest dictionary

    L L S 3 Replies Last reply
    0
    • S ShermansLagoon

      I have two projects, one "Visual C++/CLR/Class Library" (DLL file) and one "Visual C#/Windows Application" The DLL file: namespace Foo { // first attempt public ref class RC { public: static const System::Int16 value = 0; }; // second attempt public enum class EC : System::Int16 { value = 0; }; } In the application I have the following problem: static class Program { static void Main() { Int16 sh = Foo.EC.value; // error CS0266; must use explicit type cast switch(sh) { case Foo.RC.value: // error CS0150; the value is not constant MessageBox.Show("Information"); break; } return 0; } I need to solve one of these problems (which one is not that important), i.e. to have implicit type casting from an enumeration to a short (or other type of integer), or to get a "static const" defined in MC++ to actually be a constant value. I have looked all over the net trying to find a solution for this, but it seems that this behaviour actually is what is intended, is it really so? Forgot to add, I am using VS 2005, i.e. .NET 2.0 -- modified at 5:37 Monday 8th January, 2007 Internet - the worlds biggest dictionary

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The enum should not be derived from Int16. Use the enum type name in c# not Int16 and you should be right

      System.IO.Path.IsPathRooted() does not behave as I would expect

      S 1 Reply Last reply
      0
      • L Lost User

        The enum should not be derived from Int16. Use the enum type name in c# not Int16 and you should be right

        System.IO.Path.IsPathRooted() does not behave as I would expect

        S Offline
        S Offline
        ShermansLagoon
        wrote on last edited by
        #3

        Seems like I did not explain my problem good enough; What I need is some way of having a constant value in MC++ as an integer of any size (have several of them, some 16-bit, some 32-bit and some 64-bit), and I need the value to be viewed from C# as constant. When trying it as a "static const" value, as seen in my question, the compiler does not regard this as a constant value. When searching forums I found out that this is what the compiler should do, becase "static const is not const" or similar quote, and I found the suggestion of having it as a typed enumeration (i.e. public enum class NAME : TYPE), just as seen in my question. The downside on doing this is that an enum is not implicitly casted to an integer, and then I have to write: int i = (int) myEnum.value; with the explicit type cast each time. This is the same both with and without the enum being derived from an integer, I have tried both. What I am hoping is that I am not the only one who have tried this, and that someone out there has found a way of doing this, but since I have googled this it does not seem like this, so that is why I asked here.

        Internet - the worlds biggest dictionary

        1 Reply Last reply
        0
        • S ShermansLagoon

          I have two projects, one "Visual C++/CLR/Class Library" (DLL file) and one "Visual C#/Windows Application" The DLL file: namespace Foo { // first attempt public ref class RC { public: static const System::Int16 value = 0; }; // second attempt public enum class EC : System::Int16 { value = 0; }; } In the application I have the following problem: static class Program { static void Main() { Int16 sh = Foo.EC.value; // error CS0266; must use explicit type cast switch(sh) { case Foo.RC.value: // error CS0150; the value is not constant MessageBox.Show("Information"); break; } return 0; } I need to solve one of these problems (which one is not that important), i.e. to have implicit type casting from an enumeration to a short (or other type of integer), or to get a "static const" defined in MC++ to actually be a constant value. I have looked all over the net trying to find a solution for this, but it seems that this behaviour actually is what is intended, is it really so? Forgot to add, I am using VS 2005, i.e. .NET 2.0 -- modified at 5:37 Monday 8th January, 2007 Internet - the worlds biggest dictionary

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          ShermansLagoon wrote:

          get a "static const" defined in MC++ to actually be a constant value.

          It is a "const". That means it is immutable. That is not the same thing as a compiler constant that is required for a case statement. C# is not C++, it has it's own definition of required const's for a case statement. I believe you need to use a different design for whatever your goal is.

          led mike

          S 1 Reply Last reply
          0
          • L led mike

            ShermansLagoon wrote:

            get a "static const" defined in MC++ to actually be a constant value.

            It is a "const". That means it is immutable. That is not the same thing as a compiler constant that is required for a case statement. C# is not C++, it has it's own definition of required const's for a case statement. I believe you need to use a different design for whatever your goal is.

            led mike

            S Offline
            S Offline
            ShermansLagoon
            wrote on last edited by
            #5

            Ahh, ok, I understand the difference between "immutable" and "compiler constant" now. Thank you for clearing that up, lead mike. So what I am looking for then is a way of having a "compiler constant" integer value defined in MC++, encapsulated in a .DLL class library and then regarded by C# as a "compiler constant" and as an integer value. With a "static const" it is regarded as an integer value, and the "enum class" definition is "compiler constant". Is this possible, or should I just give up?

            Internet - the worlds biggest dictionary

            L 1 Reply Last reply
            0
            • S ShermansLagoon

              Ahh, ok, I understand the difference between "immutable" and "compiler constant" now. Thank you for clearing that up, lead mike. So what I am looking for then is a way of having a "compiler constant" integer value defined in MC++, encapsulated in a .DLL class library and then regarded by C# as a "compiler constant" and as an integer value. With a "static const" it is regarded as an integer value, and the "enum class" definition is "compiler constant". Is this possible, or should I just give up?

              Internet - the worlds biggest dictionary

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              ShermansLagoon wrote:

              So what I am looking for then is a way of having a "compiler constant" integer value defined in MC++

              So you say, but what is the actual problem you are trying to solve. It is very likely that a different design will solve it without that requirement.

              led mike

              S 1 Reply Last reply
              0
              • L led mike

                ShermansLagoon wrote:

                So what I am looking for then is a way of having a "compiler constant" integer value defined in MC++

                So you say, but what is the actual problem you are trying to solve. It is very likely that a different design will solve it without that requirement.

                led mike

                S Offline
                S Offline
                ShermansLagoon
                wrote on last edited by
                #7

                I have IDL files for a CORBA subset language that until recently only compiled to C++ code. Since I wanna do the client side in a nice managed language like C# the idea was to create a compiler for the IDL files that created MC++ code that is compiled into a .DLL library. In the IDL language there are several constant values declared, which is used similar to enumerations. In the C++ compiler they are defined as "compiler constant" values, i.e. it is possible to do neat switch-case code with their values. What I am looking for is a way of creating a IDL -> MC++ -> C# way that creates values that are equally "compiler constant", i.e. switch-case friendly. As shown in my first post I have found two ways, "static const" which are immutable but not compiler constant, and "enum class" which must be explicitly casted to integer values when used. I hope this give you a more clear way of knowing what I really need, and that you (or anyone else) knows if this can be done or not.

                Internet - the worlds biggest dictionary

                L 1 Reply Last reply
                0
                • S ShermansLagoon

                  I have IDL files for a CORBA subset language that until recently only compiled to C++ code. Since I wanna do the client side in a nice managed language like C# the idea was to create a compiler for the IDL files that created MC++ code that is compiled into a .DLL library. In the IDL language there are several constant values declared, which is used similar to enumerations. In the C++ compiler they are defined as "compiler constant" values, i.e. it is possible to do neat switch-case code with their values. What I am looking for is a way of creating a IDL -> MC++ -> C# way that creates values that are equally "compiler constant", i.e. switch-case friendly. As shown in my first post I have found two ways, "static const" which are immutable but not compiler constant, and "enum class" which must be explicitly casted to integer values when used. I hope this give you a more clear way of knowing what I really need, and that you (or anyone else) knows if this can be done or not.

                  Internet - the worlds biggest dictionary

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #8

                  ShermansLagoon wrote:

                  it is possible to do neat switch-case code with their values.

                  Yes because IDL is native to C++. You cannot use C++ compiler constants as C# complier constants nor can you use C++ enums as C# enums in a swtich statement. You could use C++/CLI (managed) enums in C# but that does not address your IDL code.

                  ShermansLagoon wrote:

                  I hope this give you a more clear way of knowing what I really need

                  No it doesn't. All you did was restate the same proposed approach you already posted. I want to know the actual project based problem you are facing. There is an alternate design that will solve that problem (whatever it is) that will not require using constants in a switch statement.

                  ShermansLagoon wrote:

                  create a compiler for the IDL files that created MC++ code

                  That would be a code generator not a complier, and it still does not explain what you are attempting to do with it in the C# application. As a wild guess you might need some sort of factory mechanism that would key off of the values defined in the C++/CLI module. The fact that they originate from IDL is irrelevant. It is certainly possible to accomplish this.

                  led mike

                  S 1 Reply Last reply
                  0
                  • L led mike

                    ShermansLagoon wrote:

                    it is possible to do neat switch-case code with their values.

                    Yes because IDL is native to C++. You cannot use C++ compiler constants as C# complier constants nor can you use C++ enums as C# enums in a swtich statement. You could use C++/CLI (managed) enums in C# but that does not address your IDL code.

                    ShermansLagoon wrote:

                    I hope this give you a more clear way of knowing what I really need

                    No it doesn't. All you did was restate the same proposed approach you already posted. I want to know the actual project based problem you are facing. There is an alternate design that will solve that problem (whatever it is) that will not require using constants in a switch statement.

                    ShermansLagoon wrote:

                    create a compiler for the IDL files that created MC++ code

                    That would be a code generator not a complier, and it still does not explain what you are attempting to do with it in the C# application. As a wild guess you might need some sort of factory mechanism that would key off of the values defined in the C++/CLI module. The fact that they originate from IDL is irrelevant. It is certainly possible to accomplish this.

                    led mike

                    S Offline
                    S Offline
                    ShermansLagoon
                    wrote on last edited by
                    #9

                    Thanks for showing an interest and trying to help me with my problem, but unfortunately I must inform you that it is not possible to do any other design, since it is in a locked system, so I will try to do a workaround for it.

                    Internet - the worlds biggest dictionary

                    1 Reply Last reply
                    0
                    • S ShermansLagoon

                      I have two projects, one "Visual C++/CLR/Class Library" (DLL file) and one "Visual C#/Windows Application" The DLL file: namespace Foo { // first attempt public ref class RC { public: static const System::Int16 value = 0; }; // second attempt public enum class EC : System::Int16 { value = 0; }; } In the application I have the following problem: static class Program { static void Main() { Int16 sh = Foo.EC.value; // error CS0266; must use explicit type cast switch(sh) { case Foo.RC.value: // error CS0150; the value is not constant MessageBox.Show("Information"); break; } return 0; } I need to solve one of these problems (which one is not that important), i.e. to have implicit type casting from an enumeration to a short (or other type of integer), or to get a "static const" defined in MC++ to actually be a constant value. I have looked all over the net trying to find a solution for this, but it seems that this behaviour actually is what is intended, is it really so? Forgot to add, I am using VS 2005, i.e. .NET 2.0 -- modified at 5:37 Monday 8th January, 2007 Internet - the worlds biggest dictionary

                      S Offline
                      S Offline
                      ShermansLagoon
                      wrote on last edited by
                      #10

                      I just recently solved this problem. The keyword in MC++ for this is "literal": namespace Foo { public ref class RC { public: literal System::Int16 value = 0; }; Which now can be used by C# as a compiler constant value: static class Program { static void Main() { Int16 val = 0; switch(val) { case Foo.RC.value: // no longer gives CS0150 MessageBox.Show("Information"); break; } return 0; } Just wanted to tell the world, if anybody else have had a similar problem and was looking for a way to solve it :cool:

                      Internet - the worlds biggest dictionary

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups