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 128563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:39:20+00:00 2026-05-11T05:39:20+00:00

I got an Int16 value, from the database, and need to convert this to

  • 0

I got an Int16 value, from the database, and need to convert this to an enum type. This is unfortunately done in a layer of the code that knows very little about the objects except for what it can gather through reflection.

As such, it ends up calling Convert.ChangeType which fails with an invalid cast exception.

I found what I consider a smelly workaround, like this:

String name = Enum.GetName(destinationType, value); Object enumValue = Enum.Parse(destinationType, name, false); 

Is there a better way, so that I don’t have to move through this String operation?

Here’s a short, but complete, program that can be used if anyone need to experiment:

using System;  public class MyClass {     public enum DummyEnum     {         Value0,         Value1     }      public static void Main()     {         Int16 value = 1;         Type destinationType = typeof(DummyEnum);          String name = Enum.GetName(destinationType, value);         Object enumValue = Enum.Parse(destinationType, name, false);          Console.WriteLine('' + value + ' = ' + enumValue);     } } 
  • 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. 2026-05-11T05:39:20+00:00Added an answer on May 11, 2026 at 5:39 am

    Enum.ToObject(.... is what you’re looking for!

    C#

    StringComparison enumValue = (StringComparison)Enum.ToObject(typeof(StringComparison), 5); 

    VB.NET

    Dim enumValue As StringComparison = CType([Enum].ToObject(GetType(StringComparison), 5), StringComparison) 

    If you do a lot of Enum converting try using the following class it will save you alot of code.

    public class Enum<EnumType> where EnumType : struct, IConvertible {      /// <summary>     /// Retrieves an array of the values of the constants in a specified enumeration.     /// </summary>     /// <returns></returns>     /// <remarks></remarks>     public static EnumType[] GetValues()     {         return (EnumType[])Enum.GetValues(typeof(EnumType));     }      /// <summary>     /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.     /// </summary>     /// <param name='name'></param>     /// <returns></returns>     /// <remarks></remarks>     public static EnumType Parse(string name)     {         return (EnumType)Enum.Parse(typeof(EnumType), name);     }      /// <summary>     /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.     /// </summary>     /// <param name='name'></param>     /// <param name='ignoreCase'></param>     /// <returns></returns>     /// <remarks></remarks>     public static EnumType Parse(string name, bool ignoreCase)     {         return (EnumType)Enum.Parse(typeof(EnumType), name, ignoreCase);     }      /// <summary>     /// Converts the specified object with an integer value to an enumeration member.     /// </summary>     /// <param name='value'></param>     /// <returns></returns>     /// <remarks></remarks>     public static EnumType ToObject(object value)     {         return (EnumType)Enum.ToObject(typeof(EnumType), value);     } } 

    Now instead of writing (StringComparison)Enum.ToObject(typeof(StringComparison), 5); you can simply write Enum<StringComparison>.ToObject(5);.

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

Sidebar

Related Questions

Has anyone got EclipseLink MOXy (I'm using eclipselink 2.1.0) to work with Java 5?

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.