Declaring a constant in managed C++ for access from C#

G

GnG

Hello all,

Someone posted a similar question a while ago but there was
no response. Does anyone know the answer?

I have a managed C++ DLL which is used by a C# project.
In that DLL, I have code like this:

namespace Foo {
public __value const int Bar = 123;
};

It appears in the object browser when I'm in the C++
project, but it's not visible from the C# project. Why
not? Is there another way I'm supposed to do this?

I could put it into an enumeration, or make it a static
property of a class, but I'd rather not do that, if at all
possible.

Thanks

GnG.
 
C

Carl Daniel [VC++ MVP]

GnG said:
Hello all,

Someone posted a similar question a while ago but there was
no response. Does anyone know the answer?

I have a managed C++ DLL which is used by a C# project.
In that DLL, I have code like this:

namespace Foo {
public __value const int Bar = 123;
};

It appears in the object browser when I'm in the C++
project, but it's not visible from the C# project. Why
not? Is there another way I'm supposed to do this?

I could put it into an enumeration, or make it a static
property of a class, but I'd rather not do that, if at all
possible.

Make it static. C# (and the.NET framework) has no real concept of a const
int. In C# when you declare a const int, you're really declaring a static
const int which always occupies memory and has an address, while in C++
you're declaring a constant, which may not even have an address.

-cd
 
G

GnG

Hi,

I have already tried that, and various other combinations, but still
could not see it. Not sure why!

Thanks for the response,

GnG.
 
B

Bruno van Dooren

Someone posted a similar question a while ago but there was
no response. Does anyone know the answer?

I have a managed C++ DLL which is used by a C# project.
In that DLL, I have code like this:

namespace Foo {
public __value const int Bar = 123;
};

It appears in the object browser when I'm in the C++
project, but it's not visible from the C# project. Why
not? Is there another way I'm supposed to do this?

I could put it into an enumeration, or make it a static
property of a class, but I'd rather not do that, if at all
possible.

I just tried this with a classlib:

namespace classlib_2003
{
public __gc class Class1
{
public:
static int CONST1 = 123;
};
static int __value CONST2 = 456;
}

CONST1 is visible in a C# application, CONST2 isn't.
I suspect that this is because C# has no notion about things existing
outside a class.
everything that is available in .NET exists in a class. probably for this
reason.

I think the best option is to declare a class called CONST and put all you
constants inside.

--

kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
G

GnG

Hi Bruno,

Thanks for the post. I had come to a similar conclusion about the
requirement to be inside a class and just continued from there.

I think the CONST class is a good idea. I am wrapping an API with
global constants used across classes so they do not fit comfortably in
any specific class.

Best regards,

GnG.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top