Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:15:12+00:00 2026-06-07T00:15:12+00:00

I have a const int that is computed at compile time in my Managed

  • 0

I have a const int that is computed at compile time in my Managed C++ DLL. I need to use this value in an attribute within the C# program that calls it. Initially I had created a static method that returns the const int value but C# doesn’t see this as a compile time const. I also tried declaring it as a const int within the DLL namespace

// C++
namespace MyNameSpace {
    const int AttributeConstValue = 15 + sizeof(int);
 . . .
}

Attempts to access MyNameSpace.AttributeConstValue from C# returns “does not exist in namespace MyNameSpace”

Is there a way to pass a const to C# and have it see it as a const expression?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:15:14+00:00Added an answer on June 7, 2026 at 12:15 am

    You must use the C++/CLI literal keyword to declare public constants that are visible to other managed compilers. And it must appear inside a ref class. Like this:

    namespace Example {
        public ref class Constants {
        public:
            literal int AttributeConstValue = 15 + sizeof(int);
        };
    }
    

    Sample C# usage:

    [MyAttribute(Example.Constants.AttributeConstValue)]
    // etc..
    

    Beware that this is fairly dangerous. The literal value gets compiled into the C# assembly’s metadata without a reference to your C++/CLI assembly. So if you make a change to this declaration but don’t recompile the C# project then you’ll have a nasty mismatch. But as long as you need to use it in an attribute declaration then there’s no fix for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code doesn't compile: const int x = 123; const int y = x;
I have a constructor prototype that looks like: template <typename type_position> window( const int
I have this code (simplified version): const int& function( const int& param ) {
In Form1.cs i have public const int n = 30; public TabPage[] tp =
I have these strings: int name const int level const char * const format
i have the following code below: const char* timeformat = %Y-%m-%d %H:%M:%S; const int
I have a function as below extern C int FuncTrace(const char *fmt, ...) {
I have the following piece of code compiling under gcc: int parseMsg(const char *msg_to_parse,
I have a simple C function which I declare as: int strLen(const char* str_)
Say I have something along the lines of: int main() { const char* someString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.