Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum with default typecast? is that possible?

is it possible to make a default typecast for an enum?

I use enum for a lot, such as states and I want to compare enums directly to LINQ fields, but I have to typecast all the time.

like image 869
BerggreenDK Avatar asked Dec 13 '25 04:12

BerggreenDK


2 Answers

You should be able to have properties in your LINQ objects that have an enum type. This way you do not have to cast.

So just change your properties to have the correct enum type and you don't have to worry about casts any longer. You can do this in the LINQtoSQL designer. Just right-click on a property, select 'Properties' and set the appropriate Type in the Visual Studio Properties window.

like image 147
Ronald Wildenberg Avatar answered Dec 14 '25 16:12

Ronald Wildenberg


The answer was MUCH more simple!!!

A good friend of mine told me this is very simple! have a look at this sample!

public enum State:byte
{
    EmailNotValidated = 0x00,
    EmailValidated = 0x10,
    Admin_AcceptPending = 0x40,
    Active = 0x80,
    Admin_BlockedAccount = 0xff
}

Pay attention to the :BYTE part after the name of the Enum... there is the trick I was looking for! But thanks to everyone trying for me!

like image 27
BerggreenDK Avatar answered Dec 14 '25 16:12

BerggreenDK