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. Problem including C++ code in C# assembly.

Problem including C++ code in C# assembly.

Scheduled Pinned Locked Moved Managed C++/CLI
csharphelpc++dotnetvisual-studio
3 Posts 2 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.
  • O Offline
    O Offline
    OrcBighter2
    wrote on last edited by
    #1

    I have a problem setting a class constant in my C# assembly to the value of a constant in a managed C++ class. The compiler does not seem to recognise that the managed C++ class constant is constant. The error message is shown below: #error = "The expression being assigned to 'MyAssembly.SMyFooBar.bahName' must be constant I am writing in Visual Studio 2003 on WinXP SP2 with .Net Framework V2.0.50727 What Have I done wrong here? The reason for this attempt is that the majority of our code is unmanaged c++, which uses a glabal header file to set values through a number of projects. I wish to include this header in our various new C# projects, and thus avoid double maintenance. If the header file changes, all that is needed is a code recompile, at least in theory. To do this I wrapped the header file inside a managed c++ class to create a dll. I then included this dll as a reference in my C# project so that I could use it to initialise some c# constants, thus: -- unmanaged C++ header // CPPHeader.h #ifndef CPPHeader_defined_hpp #define CPPHeader_defined_hpp #define MAX_FOOBARS 3 #define FOOBAR_NAME "Foo Bar" #endif // CPPHeader_defined_hpp --------------------- -- managed C++ class //MyManagedClass.h compiled to a DLL #pragma once #include "CPPheader.h" using namespace System; public __gc class SMyManagedClass { public: static const int maxFooBars = MAX_FOOBARS; static const String* FooBarName = FOOBAR_NAME; }; -------------------------- -- C# assembly //MyFooBarAsembly.cs using system; using SMyManagedClass; namespace MyAssembly { public class SMyFooBar { public const int maxBahs = SMyManagedClass.SMyManagedClass.maxFooBars; public const string bahName = SMyManagedClass.SMyManagedClass.FooBarName; } } OrcBighter2

    M 1 Reply Last reply
    0
    • O OrcBighter2

      I have a problem setting a class constant in my C# assembly to the value of a constant in a managed C++ class. The compiler does not seem to recognise that the managed C++ class constant is constant. The error message is shown below: #error = "The expression being assigned to 'MyAssembly.SMyFooBar.bahName' must be constant I am writing in Visual Studio 2003 on WinXP SP2 with .Net Framework V2.0.50727 What Have I done wrong here? The reason for this attempt is that the majority of our code is unmanaged c++, which uses a glabal header file to set values through a number of projects. I wish to include this header in our various new C# projects, and thus avoid double maintenance. If the header file changes, all that is needed is a code recompile, at least in theory. To do this I wrapped the header file inside a managed c++ class to create a dll. I then included this dll as a reference in my C# project so that I could use it to initialise some c# constants, thus: -- unmanaged C++ header // CPPHeader.h #ifndef CPPHeader_defined_hpp #define CPPHeader_defined_hpp #define MAX_FOOBARS 3 #define FOOBAR_NAME "Foo Bar" #endif // CPPHeader_defined_hpp --------------------- -- managed C++ class //MyManagedClass.h compiled to a DLL #pragma once #include "CPPheader.h" using namespace System; public __gc class SMyManagedClass { public: static const int maxFooBars = MAX_FOOBARS; static const String* FooBarName = FOOBAR_NAME; }; -------------------------- -- C# assembly //MyFooBarAsembly.cs using system; using SMyManagedClass; namespace MyAssembly { public class SMyFooBar { public const int maxBahs = SMyManagedClass.SMyManagedClass.maxFooBars; public const string bahName = SMyManagedClass.SMyManagedClass.FooBarName; } } OrcBighter2

      M Offline
      M Offline
      Milton Karimbekallil
      wrote on last edited by
      #2

      No way, your mc++ declaration says the constans are static. static are not considered as constants. Thats why teh error. static const int maxFooBars = MAX_FOOBARS; static const String* FooBarName = FOOBAR_NAME; So to overcome this, you have to remove taht static. but then you cant use the values without creating the instance. so the alternative is to declare an enum for that insted of a class. public enum class SMyManagedClass { maxFooBars = MAX_FOOBARS } But here inside enum u cant use strings. So for string items u can create a managed class and inside a hash table to host all tthe strings. then an index enum can puul the corresponding string,... cheers..Milton Kb

      O 1 Reply Last reply
      0
      • M Milton Karimbekallil

        No way, your mc++ declaration says the constans are static. static are not considered as constants. Thats why teh error. static const int maxFooBars = MAX_FOOBARS; static const String* FooBarName = FOOBAR_NAME; So to overcome this, you have to remove taht static. but then you cant use the values without creating the instance. so the alternative is to declare an enum for that insted of a class. public enum class SMyManagedClass { maxFooBars = MAX_FOOBARS } But here inside enum u cant use strings. So for string items u can create a managed class and inside a hash table to host all tthe strings. then an index enum can puul the corresponding string,... cheers..Milton Kb

        O Offline
        O Offline
        OrcBighter2
        wrote on last edited by
        #3

        Thanks for your reply. Sorry, but I don't quite see what you are getting at. The const in mc must be static in order to initialise it with a value. For me, the end result must be that a string constant defined in the C# assembly must be able to be initialised to the value of a constant from the managed C++ class. The code I provided works fine for integer constants, however, the C# does not regard the C++ string constant as a constant, and thus will not allow me to initialise the C# constant to that value.

        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