Tobias Knauss
Tobias Knauss

Reputation: 3539

How to define a compile time constant in an assembly?

First of all: I am using C++-CLI, but I would like to know the solution for C# as well.

Using following code

assembly "basics"

public ref class CONSTS abstract sealed
{
public:
  static const int  SUCCESS     = 1;
  static const int  InProgress  = 101;
};

assembly "program"

enum class EnumReplyLLI
{
  Nothing = 0,
  SUCCESS = CONSTS::SUCCESS,      // C2057
  Busy    = CONSTS::InProgress,   // C2057
  ...
};

I get the error C2057: expected constant expression

How can I define a compile-time constant and use it in another assembly?
My code is almost identical to the accepted answer in this SO post, but this does not work.

Upvotes: 0

Views: 992

Answers (2)

Matteo Umili
Matteo Umili

Reputation: 4017

The real equivalent of C# const in C++/CLI is literal, so your CONSTS class should look like:

public ref class CONSTS abstract sealed
{
public:
    literal int  SUCCESS = 1;
    literal int  InProgress = 101;
};

Upvotes: 2

D Stanley
D Stanley

Reputation: 152654

Not sure why it's not working in C++, but here's the equivalent code in C#:

public static class CONSTS 
{
  public const int  SUCCESS     = 1;
  public const int  InProgress  = 101;
};

enum EnumReplyLLI
{
  Nothing = 0,
  SUCCESS = CONSTS.SUCCESS,     
  Busy    = CONSTS.InProgress,  
};

Upvotes: 1

Related Questions