Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# enum typo correction

Tags:

c#

enums

I am sure everyone made a typo somewhere during the development and realized it after it is released. Well, I am experiencing the same issue except this problem live in the Enum options.

I have a enum definition like below in C#

    public enum SlideShowZoomEffect
    {
        NoEffect,
        ZoomIn,
        ZoonOut
    }

Recently, after release of the product, we realize that there's a typo for ZoonOut (it should be ZoomOut). We want to change this but it is save into database as a string (so it is saved as ZoonOut) and program will reconstruct the enum by calling enum.parse(...) which allow us to use it in our program as an enum.

The problem here is that once we changed the ZoonOut to ZoomOut, is there any ways I can have backward compatible so that both (string)ZoonOut and (string)ZoomOut will be parsed as (enum)ZoomOut?

Any suggestion will be great!

Thank you


UPDATE (Solution to this issue):

With answer accepted below, here's my testing code to show the effect on this accepted solution for future reference purposes. Thank you all

class Program
{
    static void Main(string[] args)
    {
        //Testing input as (string)ZoomOut
        Console.WriteLine("Testing #1: Input String = 'ZoomOut'");
        TestCase("ZoomOut");
        Console.WriteLine("\n\n\n");


        //Testing input as (string)ZoonOut
        Console.WriteLine("Testing #2: Input String = 'ZoonOut'");
        TestCase("ZoonOut");
        Console.WriteLine("\n\n\n");


        //Additional testing to ensure the TestCase function is working, we should see something else detected here
        Console.WriteLine("Testing #3: Input String = 'ZoomIn'");
        TestCase("ZoomIn");
    }


    static void TestCase(string inputString)
    {
        SlideShowZoomEffect enumInput = (SlideShowZoomEffect)Enum.Parse(typeof(SlideShowZoomEffect), inputString);

        Console.WriteLine("enumInput.tostring() = " + enumInput.ToString());


        Console.WriteLine("\n===> using case SlideShowZoomEffect.ZoonOut:");
        switch (enumInput)
        {
            case SlideShowZoomEffect.ZoonOut:
                Console.WriteLine("      ===> ZoomOut detected");
                break;

            default:
                Console.WriteLine("      ===> Something else detected");
                break;
        }



        Console.WriteLine("\n===> using case SlideShowZoomEffect.ZoomOut:");
        switch (enumInput)
        {
            case SlideShowZoomEffect.ZoomOut:
                Console.WriteLine("      ===> ZoomOut detected");
                break;

            default:
                Console.WriteLine("      ===> Something else detected");
                break;
        }
    }


    public enum SlideShowZoomEffect
    {
        NoEffect,
        ZoomIn,
        ZoomOut,
        ZoonOut = ZoomOut
    }
}

Here's the output from console

Testing #1: Input String = 'ZoomOut'
enumInput.tostring() = ZoomOut

===> using case SlideShowZoomEffect.ZoonOut:
      ===> ZoomOut detected

===> using case SlideShowZoomEffect.ZoomOut:
      ===> ZoomOut detected




Testing #2: Input String = 'ZoonOut'
enumInput.tostring() = ZoomOut

===> using case SlideShowZoomEffect.ZoonOut:
      ===> ZoomOut detected

===> using case SlideShowZoomEffect.ZoomOut:
      ===> ZoomOut detected




Testing #3: Input String = 'ZoomIn'
enumInput.tostring() = ZoomIn

===> using case SlideShowZoomEffect.ZoonOut:
      ===> Something else detected

===> using case SlideShowZoomEffect.ZoomOut:
      ===> Something else detected
Press any key to continue . . .
like image 540
WorldWind Avatar asked Dec 05 '22 21:12

WorldWind


2 Answers

Alternative answer for Alioza's approach:

update TableX set MyEnumField = 'ZoomOut' where MyEnumField = 'ZoonOut'

Run it when you patch your product ;)

like image 175
Bas Avatar answered Dec 08 '22 09:12

Bas


This should work:

  public enum SlideShowZoomEffect
    {
        NoEffect,
        ZoomIn,
        ZoomOut,
        ZoonOut = ZoomOut

    }

Edit: your safest bet would be to use @Bas' suggestion.

like image 28
Alioza Avatar answered Dec 08 '22 11:12

Alioza