Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, 24 May 2013

Convert values to Enum types using Generic Methods.

Alright, so I am back again on System.Enum

I faced this problem when converting a generic type T from short to Enum with base int.

For Example:


 enum TestEnum
    {
        TestMemberA = 1,

        TestMemberB = 3
    }


The following conversion just works:


           short theValue = 1;
           TestEnum directConverted = (TestEnum)theValue;

But, when using the following it fails at "return (T)input;


 static void Main(string[] args)
        {
            short theValue = 1;
            TestEnum returnedEnum = GetValueWithError<TestEnum>(theValue);
       
        }

        public static T GetValueWithError<T>(object input)
        {
            if (input == null || Convert.IsDBNull(input))
                return default(T);
           
            return (T)input;
        }



So, to fix this issue I used the following code:



    static void Main(string[] args)
        {
            short theValue = 1;
            string stringValue = GetValue<string>(theValue);
            int intValue = GetValue<int>(theValue);
            TestEnum returnedEnum = GetValue<TestEnum>(theValue);
         
        }
        public static T GetValue<T>(object input)
        {
            if (input == null || Convert.IsDBNull(input))
                return default(T);

            Type type = typeof(T);
            if (type.IsEnum)
            {

                Type underlyingType = type.GetEnumUnderlyingType();
                if (underlyingType == input.GetType())
                    return (T)input;

                return (T)Convert.ChangeType(input, underlyingType);
            }
            return (T)Convert.ChangeType(input, type);
        }


I hope this is good enough for a programmer to understand. Sorry for the bad formatting again!.

Regards
Kajal






Sunday, 22 May 2011

About this Blog

First of all, hello to everyone who is viewing this page. This is my first blog post and I have no experience in content writing. I apologies if you find anything missing or any mistake, please do let me know and I will correct it.


I thought and planned to write something interesting. Something, that will attract the interest of developers who are willing to read about new technologies. As this blog name suggests, we will discuss about .NET and the new features which are added by Microsoft.


I thank Microsoft for providing such a great platform to work on. I am working on .NET since its 1.0 Beta 1 release. In every next release I found lot of improvements which requires discussion and knowledge sharing. I would like to share that I was a Java guy and have worked in Core Java, Advance Java, J2EE, J2ME technology for around 4 years. I started working in .NET in parallel with Java. I liked and switched to .NET computing. Every new release of .NET  Framework for any platform makes me happy as it opens many new dimensions for developers.


I would like to start my discussion with few good features like Generics, LINQ, PLINQ, TPL and would like to focus on rest of the language features. I will mostly talk in C#. I welcome your comments/suggestions.


Regards
Kajal