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

  • SEARCH
  • Home
  • 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 564211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:44:34+00:00 2026-05-13T12:44:34+00:00

We have legacy character codes that we want to store as numbers in a

  • 0

We have legacy character codes that we want to store as numbers in a new system. To increase readibility and general understanding in the code for devs making the migration, I want to do Enums like this…

Public Enum Status As Short
    Open = AscW("O")
    Closed = AscW("C")
    Pending = AscW("P")
    EnRoute = AscW("E")
End Enum

With this setup, the code will be readable (imagine If Record.Status = Status.Open), and yet the values will be stored in the database as small numbers so it will be efficient. However… I am a VB.NET guy, but everybody wants to code in C#, so I need this sort of structure in C#.

After Googling, I discovered the the general .NET equivalent of AscW is Convert.ToInt32("C"). When I try to use that statement in an enum, I get the compiler error “Constant Expression Required”.

How can I do this in C#? Is there a better way?

  • 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-05-13T12:44:34+00:00Added an answer on May 13, 2026 at 12:44 pm

    A method call is not a constant expression. Try this:

    public enum Status { 
       Open = 'O',
       Closed = 'C',
       Pending = 'P',
       EnRoute = 'E'
    }
    

    The reason AscW works in VB is that it’s an internal thing that VB compiler understands and evaluates at compile time and is considered a constant expression by the compiler. Even in VB, Convert.ToInt32 will not work.

    To quote the Visual Basic specification:

    11.2 Constant Expressions

    A constant expression is an expression whose value can be fully evaluated at compile time. […] The following constructs are permitted in constant expressions:

    […]

    • The following run-time functions:

      • Microsoft.VisualBasic.Strings.ChrW
      • Microsoft.VisualBasic.Strings.Chr, if the constant value is between 0 and 128
      • Microsoft.VisualBasic.Strings.AscW, if the constant string is not empty
      • Microsoft.VisualBasic.Strings.Asc, if the constant string is not empty
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have legacy C++ code that I wrote to generate uniform random numbers and
I have legacy DB that store dates that means no-date as 9999-21-31, The column
We have a legacy system that requires a format which is not html but
I have a legacy system that produces XML with timestamps similar to ISO 8601,
I have some data files from a legacy system that I would like to
I have legacy C++ code that changes a process DACL and am trying to
I have legacy application code written for a real-time operating system (RTOS). Most of
I have legacy code that I'm trying to obsolete that I cannot change. Its
I have legacy code that I am slowly moving to C# MVC. How would
We have a legacy database that uses strings as primary keys. I want to

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.