locked
How to extend existing enum (or what to do when I can't cast?)

    Question

  • Hi,

    I'm an experienced C/C++/Delphi programmer but new to C#. I'm building a panel control that supports gradient fill background color. I want to expose a property of type LinearGradientMode but additionally, I would like to include an additional "None" value. I created my own SKLinearGradientMode enum including the original values plus "None" but I obviously can't pass that to LinearGradientBrush and I can't typecast it a LinearGradientMode. Is there a way to achieve this in C# without resorting to a switch/if statement to map from one to the other?

    Wednesday, February 14, 2007 8:34 PM

Answers

  • Why do you say you can't typecast it?  This code works fine for me:

     

    enum ABC { A, B, C};

    enum DEF { A, B, C, D, E, F, G};

    DEF d = (DEF)ABC.B;

    ABC b = (ABC) DEF.B;

     

    Friday, February 16, 2007 4:29 PM
  • Hi, Scott

    Yes, you can easily define a enumeration extending existing enum. When you extend the LinearGradientMode enum (which has value 0 to 3), it's ok.

    However, if you put your extended enum value say 4 (which does not exist in original one) to a specific method (the constructor of LinearGradientBrush), it wont work any way. Because, the method only design for original enum type values from 0 to 3 as its argument but not the extended enum.

    If you want it to work, you must override or rewrite that method to process the extended value.

    If you've further problems pls feel free to let us know. Thank you.

     

    Saturday, February 24, 2007 8:52 AM

All replies

  • Hi,

      if I understand your issue you want to be able to convert between different enums,  the following code will do that for you:

     

     using System;

    using System.Collections.Generic;

    using System.Text;

     

    namespace ConsoleApplication6

    {

        class Program

        {

            enum EnumA

            {

                A = 0,

                B = 1,

                C = 2

            }

     

            enum EnumB

            {

                X = 0,

                Y = 1,

                Z = 2

            }

     

            static void Main(string[] args)

            {

                //a - will be equal to EnumA.B

                EnumA a = (EnumA)Enum.Parse(typeof(EnumA), ((int)EnumB.Y).ToString());

            }

        }

    }

     You can replace the EnumB.Y with any value you want from the originating enum.

    Mark.

    Thursday, February 15, 2007 2:40 AM
  • EnumA a = (EnumA)Enum.Parse(typeof(EnumA), ((int)EnumB.Y).ToString());

    That could be done much simpler/fast by just:

    EnumA a = (EnumA) EnumB.Y;

    Friday, February 16, 2007 4:25 PM
  • Why do you say you can't typecast it?  This code works fine for me:

     

    enum ABC { A, B, C};

    enum DEF { A, B, C, D, E, F, G};

    DEF d = (DEF)ABC.B;

    ABC b = (ABC) DEF.B;

     

    Friday, February 16, 2007 4:29 PM
  • Hi, Scott

    Yes, you can easily define a enumeration extending existing enum. When you extend the LinearGradientMode enum (which has value 0 to 3), it's ok.

    However, if you put your extended enum value say 4 (which does not exist in original one) to a specific method (the constructor of LinearGradientBrush), it wont work any way. Because, the method only design for original enum type values from 0 to 3 as its argument but not the extended enum.

    If you want it to work, you must override or rewrite that method to process the extended value.

    If you've further problems pls feel free to let us know. Thank you.

     

    Saturday, February 24, 2007 8:52 AM