zarfishan zahid

zarfishan zahid

  • NA
  • 0
  • 17.4k

Convert CSharp enums to Java using CodePorting App

Jul 27 2012 2:35 AM

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 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.

 

C# code:

 

namespace CsPorter.Examples.Convert.Enums

{

    /// <summary>

    /// Enum summary.

    /// </summary>

    public enum Example0

    {

        /// <summary>

        /// The first.

        /// </summary>

        One,

        /// <summary>

        /// The second.

        /// </summary>

        Two,

        //Third.

        Three

    }

 

    internal enum Example1: byte

    {

        OneByte,

        TwoByte,

        ThreeByte

    }

 

    internal enum Example2{

        One,

        Two,

        Three

    }

 

    internal enum Example3 {One, Two, Three}

}

 

Ported Java code:

 

package CsPorter.Examples.Convert.Enums;

 

// **** THIS CODE IS AUTO PORTED FROM C# TO JAVA USING CODEPORTING.COM TECHNOLOGY ****

 

import com.codeporting.csharp2java.java.Enum;

 

/**

     * <p>

     * Enum summary.

     * </p>

     */

public /*enum*/ final class Example0 extends Enum

{

         private Example0(){}

    /// <summary>

    /// The first.

    /// </summary>

    public static final int ONE = 0;

    /// <summary>

    /// The second.

    /// </summary>

    public static final int TWO = 1;

    //Third.

    public static final int THREE = 2;

 

         static {

                 Enum.register(new Enum.SimpleEnum(Example0.class, Integer.class) {{

                 addConstant("ONE", ONE);

                 addConstant("TWO", TWO);

                 addConstant("THREE", THREE);

                 }});

         }

 

}

 

/*enum*/ final class Example1/*: byte*/ extends Enum

{

         private Example01(){}

    public static final byte ONE_BYTE = 0;

    public static final byte TWO_BYTE = 1;

    public static final byte THREE_BYTE = 2;

 

         static {

                 Enum.register(new Enum.SimpleEnum(Example01.class, Byte.class) {{

                 addConstant("ONE_BYTE", ONE_BYTE);

                 addConstant("TWO_BYTE", TWO_BYTE);

                 addConstant("THREE_BYTE", THREE_BYTE);

                 }});

         }

 

}

 

/*enum*/ final class Example2 extends Enum{

         private Example2(){}

    public static final int ONE = 0;

    public static final int TWO = 1;

    public static final int THREE = 2;

 

         static {

                 Enum.register(new Enum.SimpleEnum(Example2.class, Integer.class) {{

                 addConstant("ONE", ONE);

                 addConstant("TWO", TWO);

                 addConstant("THREE", THREE);

                 }});

         }

 

}

 

/*enum*/ final class Example3 extends Enum {

         private 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);

               }});

        }

 

}

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.