1

I have an enum class called Gender, and I have values in this like:

public enum GENDER
{
    MALE = 1,
    FEMALE = 2,
    OTHERS = 3,
}

And in my business object I just created a property for this, like:

public GENDER Gender
{
  get
  { 
      return gender; 
  }
  set 
  {
      gender = value; 
  }
}

In my database it's in type integer. For this I tried to assign the Gender value in the add function by its name Gender. But it's showing an error. Is it possible to cast the enum to integer so that it'll get the value automatically.

How can I fix this?

5
  • How are you writing to the database? If Entity Framework, then that doesn't support an enum/int conversion unfortunately. See here. Commented Jun 22, 2012 at 15:16
  • am passing thru entity framework Commented Jun 22, 2012 at 15:23
  • 2
    @Dommer: In English, some things are neither male or female they are neutral. For instance a car is assigned the pronoun 'it'. Very unlike French where everything must be masculine or feminine. So there! Fundamentals of English Language for Programmers! Commented Jun 22, 2012 at 15:27
  • @Alex: Yes you're quite correct. Indeed in English things that can't be described as fauna are generally not assigned gender, although colloquially pretty much everything is assigned a gender at some point ;-p Commented Jun 22, 2012 at 15:38
  • Does this answer your question? How do I cast int to enum in C#? Commented Jun 25, 2023 at 15:12

7 Answers 7

5

(int)Gender will do it. To cast a specific value instead of the property, you'll do something like (int)GENDER.MALE;

Sign up to request clarification or add additional context in comments.

Comments

3

Enums by default "inherit" from int (they are stored in int by default unless you choose another integral type), so you can simply cast to and from int without any trouble:

int i = (int)GENDER.MALE;

GENDER g = (GENDER)2;

Note that implicit casting is not supported. Casting must be explicit.

Beware that the cast doesn't do any checks of the range, so you can cast an int that doesn't have a GENDER value into a GENDER variable without error:

GENDER g = (GENDER)26;

Comments

1

If (int)Gender is not enough to fix your cast issue, try to add int.Parse(var, int) in your code. It might be helpful...

Comments

1

You should be able to use a cast to convert between an integer and an enum value.

int n = (int)GENDER.MALE;

GENDER g = (GENDER)1;

If you're dealing with a string that you are reading from a database column (or any string for that matter) you can do the following:

GENDER g = (GENDER) Enum.Parse(typeof(GENDER), "MALE");

Note that Enum.Parse returns an object, so you have to cast it to your enum type first.

2 Comments

if i create a string property like, public string Name, I can simply give Name to pass the value right, in the same way is it possible to pass the enum value, coz am not sure the value is 1 or something else(in ur example its 1)
If you're dealing with strings, you have to go about it another way. I'll look it up and edit my post.
1

If you want to set the database value to gender property, do this:

yourintdatabasefiled = (int)GENDER.yourproperty

Comments

1

The following examples should suffice:

int val = (int)Gender.Male;
int val2 = (int)_gender;

There are also System.Enum methods to help you discover and manipulate enum values like:

Console.WriteLine("The values of the Gender Enum are: ");
foreach (int i in Enum.GetValues(typeof(Gender)))
{
    Console.WriteLine(i);
}

Comments

1

The previous answers is right on it. Gender is now a type, just like int, so you can cast accordingly - both ways, too:

(int)Gender = . . .
/* Or */
(Gender)int = . . .

so, for

int i = (int)GENDER.male;

i would equal 1.

And the same for

Gender = GENDER.male;
i = (int)Gender;

i would also be 1.

2 Comments

You should example the reason i = (int)Gender; return 1.
Ah, good catch. Shanish, in your code you said 'public enum GENDER {male = 1, ...};' so if you set your Gender variable to GENDER.male like I did in the example, then when you (int) cast Gender you will get a value of 1 because that is the int equivalent that you assigned to male when you first made the enum. If you need more just ask!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.