473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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.

Feb 16 '06 #1
4 2785
GnG wrote:
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
Feb 16 '06 #2
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.

Feb 16 '06 #3
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.
br**********************@hotmail.com
Remove only "_nos_pam"
Feb 16 '06 #4
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.

Feb 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bern McCarty | last post by:
I am using MEC++ in VC 7.1. I had a method on a __gc object that looked like this: __property System::UInt32 get_MyProperty(void) { System::Byte __pin * pinBytes = &m_byteArray; // entire...
0
by: Graham | last post by:
Hello all, I have a managed C++ DLL which is used by a VB.NET project. In that DLL, I have code like this: namespace Foo { public __value const int Bar = 123; }; It appears in the object...
2
by: Zardoz | last post by:
I've created a new header file in which one will find : #using <mscorlib.dll> using namespace System; __gc class G { public: int k; int sum(int){return 0;}
6
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
15
by: Chris Saunders | last post by:
I have made a declaration like this: private const Complex I = new Complex(0.0, 1.0); When I try to build this I get the error: The expression being assigned to...
3
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
2
by: John | last post by:
I want to declare a somewhat larger string constant which doesn't fit on one line. I tried several ways without luck. Is it even possible to declare it on more than one line? Thanks, john
2
by: Pavan | last post by:
Hi, I need to create a consant array of larze size but however its elements can be calculated using an equation, say for example I need an int arry of 20 elements where each element will be arr...
3
by: longbrmb | last post by:
I'm new to C++/CLI and my main background is Java. I'm trying to create an array of constants as a static member of a class. An example is shown below: public ref class TestClass { public:...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.