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 . . .
Alternative answer for Alioza's approach:
update TableX set MyEnumField = 'ZoomOut' where MyEnumField = 'ZoonOut'
Run it when you patch your product ;)
This should work:
public enum SlideShowZoomEffect
{
NoEffect,
ZoomIn,
ZoomOut,
ZoonOut = ZoomOut
}
Edit: your safest bet would be to use @Bas' suggestion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With