Convert CSharp enums to Java using CodePorting App

Discussion in 'Programming' started by zarfishsn, Jul 27, 2012.

  1. #1
    With the help of CodePorting C#2Java you can fix small changes by editing your code in source code editor. CodePorting provides built-in source code viewer with syntax highlighter which enables you to read and access your code from anywhere, everywhere. With the help of this feature you can fix your code by using your iPad or smart phone while you are travelling or away from your machine.

    In C#, enum keyword is used to declare enumeration which consists of different types of name constants. This list is called enumerator list. Each enumeration element contains underlying type, default is int. This underlying type is used to decide how much storage should be allocated to each enumerator.

    Let me give you an example to show you how Codeporting engine converts C# enum to java.
     
    [B]C# code:[/B]
     
    namespace CsPorter.Examples.Convert.Enums
    {
        [I]/// <summary>[/I]
        [I]/// Enum summary.[/I]
        [I]/// </summary>[/I]
        [B]public[/B] [B]enum[/B] Example0
        {
            [I]/// <summary>[/I]
            [I]/// The first.[/I]
            [I]/// </summary>[/I]
            One,
            [I]/// <summary>[/I]
            [I]/// The second.[/I]
            [I]/// </summary>[/I]
            Two,
            [I]//Third.[/I]
            Three
        }
     
        internal [B]enum[/B] Example1: [B]byte[/B]
        {
            OneByte,
            TwoByte,
            ThreeByte
        }
     
        internal [B]enum[/B] Example2{
            One,
            Two,
            Three
        }
     
        internal [B]enum[/B] Example3 {One, Two, Three}
    }
     
    [B]Ported Java code:[/B]
     
    [B]package[/B] CsPorter.Examples.Convert.Enums;
     
    [I]// **** THIS CODE IS AUTO PORTED FROM C# TO JAVA USING CODEPORTING.COM TECHNOLOGY ****[/I]
     
    [B]import[/B] com.codeporting.csharp2java.java.Enum;
     
    [B][I]/**[/I][/B]
    [B][I]     * <p>[/I][/B]
    [B][I]     * Enum summary.[/I][/B]
    [B][I]     * </p>[/I][/B]
    [B][I]     */[/I][/B]
    [B]public[/B] [I]/*enum*/[/I] [B]final[/B] [B]class[/B] Example0 [B]extends[/B] [B]Enum[/B]
    {
             [B]private[/B] Example0(){}
        [I]/// <summary>[/I]
        [I]/// The first.[/I]
        [I]/// </summary>[/I]
        [B]public[/B] [B]static[/B] [B]final[/B] [B]int[/B] ONE = 0;
        [I]/// <summary>[/I]
        [I]/// The second.[/I]
        [I]/// </summary>[/I]
        [B]public[/B] [B]static[/B] [B]final[/B] [B]int[/B] TWO = 1;
        [I]//Third.[/I]
        [B]public[/B] [B]static[/B] [B]final[/B] [B]int[/B] THREE = 2;
     
             [B]static[/B] {
                     [B]Enum[/B].register([B]new[/B] [B]Enum[/B].SimpleEnum(Example0.[B]class[/B], Integer.[B]class[/B]) {{
                     addConstant("ONE", ONE);
                     addConstant("TWO", TWO);
                     addConstant("THREE", THREE);
                     }});
             }
     
    }
     
    [I]/*enum*/[/I] [B]final[/B] [B]class[/B] Example1[I]/*: byte*/[/I] [B]extends[/B] [B]Enum[/B]
    {
             [B]private[/B] Example01(){}
        [B]public[/B] [B]static[/B] [B]final[/B] [B]byte[/B] ONE_BYTE = 0;
        [B]public[/B] [B]static[/B] [B]final[/B] [B]byte[/B] TWO_BYTE = 1;
        [B]public[/B] [B]static[/B] [B]final[/B] [B]byte[/B] THREE_BYTE = 2;
     
             [B]static[/B] {
                     [B]Enum[/B].register([B]new[/B] [B]Enum[/B].SimpleEnum(Example01.[B]class[/B], Byte.[B]class[/B]) {{
                     addConstant("ONE_BYTE", ONE_BYTE);
                     addConstant("TWO_BYTE", TWO_BYTE);
                     addConstant("THREE_BYTE", THREE_BYTE);
                     }});
             }
     
    }
     
    [I]/*enum*/[/I] [B]final[/B] [B]class[/B] Example2 [B]extends[/B] [B]Enum[/B]{
             [B]private[/B] Example2(){}
        [B]public[/B] [B]static[/B] [B]final[/B] [B]int[/B] ONE = 0;
        [B]public[/B] [B]static[/B] [B]final[/B] [B]int[/B] TWO = 1;
        [B]public[/B] [B]static[/B] [B]final[/B] [B]int[/B] THREE = 2;
     
             [B]static[/B] {
                     [B]Enum[/B].register([B]new[/B] [B]Enum[/B].SimpleEnum(Example2.[B]class[/B], Integer.[B]class[/B]) {{
                     addConstant("ONE", ONE);
                     addConstant("TWO", TWO);
                     addConstant("THREE", THREE);
                     }});
             }
     
    }
     
    [I]/*enum*/[/I] [B]final[/B] [B]class[/B] Example3 [B]extends[/B] [B]Enum[/B] {
             [B]private[/B] Example3(){}
    public static final int ONE = 0;
    public static final int TWO = 1; public static final int THREE = 2;
     
            static {
                   Enum.register(new Enum.SimpleEnum(Example3.class, Integer.class) {{
                   addConstant("ONE", ONE);
                   addConstant("TWO", TWO);
                   addConstant("THREE", THREE);
                   }});
            }
     
    }
    Code (markup):
    It is clear from the above example that Codeporting C#2Java engine uses final classes to replace enum. Although java language has enums but Codeporting converts enum to integer constant because enum appeared in java v1.5 only and before java v5 the acceptable way to translate enum is by using classes with integer constant.
     
    zarfishsn, Jul 27, 2012 IP