Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences Between Enums in C# and VB.NET

Tags:

c#

enums

vb.net

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?

like image 300
Josh Stodola Avatar asked Jan 14 '10 16:01

Josh Stodola


People also ask

Which is better #define or enum?

The use of an enumeration constant (enum) has many advantages over using the traditional symbolic constant style of #define. These advantages include a lower maintenance requirement, improved program readability, and better debugging capability.

What is the difference between enum and #define in C?

One of the differences between the two is that #define is a pre-processor directive while enum is part of the actual C language. #define statements are processed by the compiler before the first line of C code is even looked at!

What are enums in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

What is the difference between enum and struct in C?

Enum is to define a collection of options available. A struct can contain both data variables and methods. Enum can only contain data types. A struct supports a private but not protected access specifier.


2 Answers

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
like image 106
mmx Avatar answered Sep 30 '22 17:09

mmx


Try this:

public enum Status
{
    Open    = 'O',
    Closed  = 'C',
    Pending = 'P',
    EnRoute = 'E'
}
like image 32
Rubens Farias Avatar answered Sep 30 '22 16:09

Rubens Farias