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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shanish
  • 1,964
  • 12
  • 35
  • 62
  • How are you writing to the database? If Entity Framework, then that doesn't support an enum/int conversion unfortunately. See [here](http://stackoverflow.com/q/2065612/121309). – Hans Kesting Jun 22 '12 at 15:16
  • am passing thru entity framework – shanish Jun 22 '12 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! – Alex Essilfie Jun 22 '12 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 – Tom Chantler Jun 22 '12 at 15:38
  • Does this answer your question? [How do I cast int to enum in C#?](https://stackoverflow.com/questions/29482/how-do-i-cast-int-to-enum-in-c) – AztecCodes Jun 25 '23 at 15:12

7 Answers7

5

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

MNGwinn
  • 2,394
  • 17
  • 18
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;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
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.

Eric Andres
  • 3,417
  • 2
  • 24
  • 40
  • 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) – shanish Jun 22 '12 at 15:19
  • If you're dealing with strings, you have to go about it another way. I'll look it up and edit my post. – Eric Andres Jun 22 '12 at 15:22
1

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

yourintdatabasefiled = (int)GENDER.yourproperty
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KF2
  • 9,887
  • 8
  • 44
  • 77
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...

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);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
S2S2
  • 8,322
  • 5
  • 37
  • 65
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CM90
  • 825
  • 1
  • 7
  • 9
  • You should example the reason `i = (int)Gender;` return 1. – Security Hound Jun 22 '12 at 15:58
  • 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! – CM90 Jun 22 '12 at 16:05