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 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
-
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
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 -
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 KbThanks 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.