views:

294

answers:

4

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.

+1  A: 

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.

Ronald Wildenberg
No they are often tinyint as I do not have more than 5 to 20 different values.
BerggreenDK
In that case you should just change their type to the enum type they actually are. LINQtoSQL will handle this correctly.
Ronald Wildenberg
pardon for my studpid question, but where do I change their type?
BerggreenDK
I added some more info to my answer.
Ronald Wildenberg
A: 

Have you tried extension methods?

public enum MyEnum
{
    First = 1,
    Second = 2,
    Third = 3
}

public static class Utility
{
    public static string Description(this Enum e)
    {
        Type t = e.GetType();
        DescriptionAttribute[] desc =
            (DescriptionAttribute[])(t.GetField(e.ToString())
            .GetCustomAttributes(typeof(DescriptionAttribute), false));
        return desc.Length > 0 ? desc[0].Description : e.ToString();
    }
    public static byte ToByte(this Enum ai)
    {
        object o=Enum.ToObject(ai.GetType(), ai);
        return Convert.ToByte(o);
    }
}


class Program
{
    static void Main(string[] args)
    {
        MyEnum me = MyEnum.Third;

        Console.WriteLine("Value: {0}\r\nType: {1}"
        ,me.ToByte(),me.ToByte().GetType().ToString());

        Console.ReadLine();
    }
}

It outputs:

Value: 3

Type: System.Byte

TheVillageIdiot
No I haven't yet, but that seems like a lot of code just to typecast a variable? is this hitting performance in any way or is the best general way to do the trick? I was hoping for a "simple" (byte) replacement, something about syntax in the enum or something? Not possible I guess...
BerggreenDK
I've added the example code this is just few lines, 4 actually ;)
TheVillageIdiot
I'm confused... how is that easier than casting? It certainly is a lot less efficient, since it introduces a box/unbox...
Marc Gravell
Why would you take this approach when for enums it is as simple as giving your properties the correct enum type?
Ronald Wildenberg
+1  A: 

LINQ-to-SQL will usually handle direct integer maps and exact string (name) maps (note: case sensitive). Meaning: write your enum somewhere, and in the designer set the property type as the fully-qualified enum name: Some.Namespace.MyEnum. It should usually work.

For non-trivial mappings (for example where the column is a varchar with mixed-case values, or things like "In Progress" [note the space]), you will have to leave the storage property as int/varchar (etc) and map it manually. I usually do this by marking it as private and naming it FooStorage, and adding a mapping property in a partial class:

partial class MyType {
    public MyEnum Foo {
        get {... mapping logic reading from FooStorage...}
        set {... mapping logic, updating FooStorage...}
    }
}

The only problem is that LINQ queries will only work against the storage property (not the bespoke property).

Marc Gravell
My orignal problem is that I am used to have loads of enums, but when I do like this:oTabel.State = someEnum.SomeState;I have to write it:oTabel.State = (byte)someEnum.SomeState;Everytime... cant I make the "SomeEnum" into default (BYTE)-cast?
BerggreenDK
Why wouldn't you just tell the system to treat State as a SomeEnum via the designer? But no: you can't add an implicit conversion operator to an enum (except for 0, which always has an implicit conversion).
Marc Gravell
A: 

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!

Looking forward to post my next question.

BerggreenDK
Thanks for your time, all of you!
BerggreenDK