Problem including C++ code in C# assembly
-
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 What Have I done wrong here? I am writing in Visual Studio 2003 on WinXP SP2 with .Net Framework V2.0.50727 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
-
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 What Have I done wrong here? I am writing in Visual Studio 2003 on WinXP SP2 with .Net Framework V2.0.50727 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
You probably need to use readonly. There are differences in lookup rules that may exclude this from working.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog