Skip to content
Fix Code Error

Get int value from enum in C#

March 13, 2021 by Code Error
Posted By: Anonymous

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this.

public enum Question
{
    Role = 2,
    ProjectFunding = 3,
    TotalEmployee = 4,
    NumberOfServers = 5,
    TopBusinessConcern = 6
}

In the Questions class I have a get(int foo) function that returns a Questions object for that foo. Is there an easy way to get the integer value off the enum so I can do something like this Questions.Get(Question.Role)?

Solution

Just cast the enum, e.g.

int something = (int) Question.Role;

The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int.

However, as cecilphillip points out, enums can have different underlying types.
If an enum is declared as a uint, long, or ulong, it should be cast to the type of the enum; e.g. for

enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649};

you should use

long something = (long)StarsInMilkyWay.Wolf424B;
Answered By: Anonymous

Related Articles

  • Vue.JS & Spring Boot - Redirect to homepage on 404
  • Checking if a variable is an integer in PHP
  • How to print binary tree diagram?
  • No function matches the given name and argument types
  • HQL Responding with…
  • How to filter a RecyclerView with a SearchView
  • JSON API response and ember model names
  • Compiler error: "class, interface, or enum expected"
  • org.hibernate.MappingException: Could not determine…
  • Callback functions in C++
  • Enum Naming Convention - Plural
  • MySQL user DB does not have password columns -…
  • Webpack build error
  • MySQL server has gone away - in exactly 60 seconds
  • What is the origin of foo and bar?
  • Calculate time difference in minutes in SQL Server
  • TypeScript metadata reflection references other…
  • I want to create a SQLite database like in the…
  • How to use module.exports in Next.js
  • Aurelia, navigate is not updating viewport
  • Difference between
  • Fastest way to list all primes below N
  • Is there a naming convention for MySQL?
  • Is this request generated by EF Core buggy or is it my code?
  • Javax.net.ssl.SSLHandshakeException:…
  • Usage of __slots__?
  • How can I condense this Python Tkinter GUI input code?
  • Conversion failed when converting the varchar value…
  • Remix error The transaction has been reverted to the…
  • Java Class.cast() vs. cast operator
  • Nested class: Calling child class properties in parent class
  • Smart way to truncate long strings
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • enum to string in modern C++11 / C++14 / C++17 and…
  • Why is enum class preferred over plain enum?
  • dynamic_cast and static_cast in C++
  • How do I make it so my bot doesn't send "role given"…
  • REST URI convention - Singular or plural name of…
  • Which is more efficient, a for-each loop, or an iterator?
  • How to make Ember data stop changing my endpoints to…
  • Explanation on Integer.MAX_VALUE and…
  • When should static_cast, dynamic_cast, const_cast…
  • Automapper Aftermap alternative for Nested Mapping
  • What is a NullReferenceException, and how do I fix it?
  • How to create ranges and synonym constants in C#?
  • Conveniently map between enum and int / String
  • PHP - Indirect modification of overloaded property
  • JavaScript - Truncate innerHTML string after N…
  • Knight's tour Problem - storing the valid moves then…
  • vue v-for of typescript enum
  • Convert Json String to C# Object List
  • How do you handle pluralisation in Ember?
  • Cast from VARCHAR to INT - MySQL
  • What's the advantage of a Java enum versus a class…
  • How to fix Hibernate LazyInitializationException:…
  • How can I create a Promise in TypeScript from a union type
  • Error: the entity type requires a primary key
  • python change value of a dictionary based on majority
  • How can I represent an 'Enum' in Python?
  • What is the purpose of the "role" attribute in HTML?
  • How to cast an Object to an int
  • pandas: best way to select all columns whose names…
  • Regular cast vs. static_cast vs. dynamic_cast
  • Minimal webpack setup for Aurelia without easy-webpack
  • Item position in RecyclerView only changing when…
  • Get the value from select form in EmberJS and ember-data
  • Map enum in JPA with fixed values?
  • Change type of varchar field to integer: "cannot be…
  • Table Naming Dilemma: Singular vs. Plural Names
  • Vue.js: Using propsData in a jasmine test
  • How to register a custom callback after Ember.RSVP.hash
  • How to iterate through a JSON response with nested…
  • Equals implementation with override Equals,…
  • Why cat does not work with parameter -0 in xargs?
  • What is and how to fix…
  • How to implement the factory method pattern in C++ correctly
  • Post request with many-to-many relation
  • Why is Ember throwing "Uncaught Error: Assertion…
  • SQL Transaction Error: The current transaction…
  • Signed to unsigned conversion in C - is it always safe?
  • How do I return the response from an asynchronous call?
  • How can I iterate over an enum?
  • What is a typedef enum in Objective-C?
  • Playing HTML5 video on fullscreen in android webview
  • How do Mockito matchers work?
  • What is the "proper" way to cast Hibernate…
  • Getting value from appsettings.json in .net core
  • Validation failed for one or more entities. See…
  • EL access a map value by Integer key
  • I need to extract working hour breaks out of a Time…
  • Class vs. static method in JavaScript
  • Java using enum with switch statement
  • How can I manually compile a svelte component down…
  • jQuery Mobile: document ready vs. page events
  • Creating an instance of class
  • What json structure should the ember-data "findMany"…
  • Generate sequence of dates for given frequency as…
  • Uno Platform WASM SignalR deserialization issues
  • Pyparsing: Parse Dictionary-Like Structure into an…
  • Why postman indicated 404 not found

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Creating multiline strings in JavaScript

Next Post:

Transitions on the CSS display property

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error