zarfishan zahid

zarfishan zahid

  • NA
  • 0
  • 18.6k

Automatically Convert C# Indexers to Java using CodePorting Engine

Jun 20 2012 4:57 AM

Codeporting has become a commonly used web based service by developers to convert their C# code to java which gives them the benefit of platform independence. C#2Java Engine accurately translates C# constructs to java.

Let me prove my point by taking the example of C# Indexers. For starters, Indexers creates classes or struct instances same way as of arrays but its not necessary that indexers are indexed by integer values, rather it depends on developers how he defines them.

Following examples shows how CodePorting C#2Java Engine translates C# Indexers to Java.

C# Code:

namespace CsPorter.Examples.Convert.Indexers

{

    public class Example14

    {

        void Method()

        {

            //int indexer

            int i = this[1];

            this[2] = i;

 

            //enum, but int indexer in java

            i = this[Example14Enum.Two];

            this[Example14Enum.Two] = 1;

        }

 

        //default java name: get/set

        internal int this[int index]

        {

            get { return index; }

            set { int i = value; }

        }

 

        //java name should be changed to getByExample13Enum/setByExample13Enum

        internal int this[Example14Enum idx]

        {

            get { return (int)idx; }

            set { Example14Enum e = (Example14Enum)value; }

        }

    }

 

    internal class Example141

    {

        void Method(Example14 Example)

        {

            //int indexer

            int i = Example[1];

            Example[2] = i;

 

            //enum, but int indexer in java

            i = Example[Example14Enum.Two];

            Example[Example14Enum.Two] = 1;

        }

    }

 

    internal enum Example14Enum

    {

        One, Two, Three

    }

}

Java Code:

package CsPorter.Tests.Convert.Indexers;

 

// ********* THIS FILE IS AUTO PORTED FORM C# USING CODEPORTING.COM *********

 

import com.codeporting.csharp2java.java.Enum;

 

public class Test14

{

    private void method()

    {

        //int indexer

        int i = this.get(1);

        this.set(2, i);

 

        //enum, but int indexer in java

        i = this.getByExample14Enum(Example14Enum.TWO);

        this.setByExample14Enum(Example14Enum.TWO, 1);

    }

 

    //default java name: get/set

    int get(int index) { return index; }

    void set(int index, int value) { int i = value; }

 

    //java name should be changed to getByExample13Enum/setByExample13Enum

    int getByExample14Enum(/*Example14Enum*/int idx) { return (int)idx; }

    void setByExample14Enum(/*Example14Enum*/int idx, int value) { /*Example14Enum*/int e = (/*Example14Enum*/int)value; }

}

 

class Example141

{

    private void method(Example14 Example)

    {

        //int indexer

        int i = Example.get(1);

        Example.set(2, i);

 

        //enum, but int indexer in java

        i = Example.getByExample14Enum(Example14Enum.TWO);

        Example.setByExample14Enum(Example14Enum.TWO, 1);

    }

}

 

/*enum*/ final class Example14Enum extends Enum

{

         private Example14Enum(){}

    public static final int ONE = 0; public static final int TWO = 1; public static final int THREE = 2;

 

         static {

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

                 addConstant("ONE", ONE);

                 addConstant("TWO", TWO);

                 addConstant("THREE", THREE);

                 }});

         }

 

}

 

It is clear from the above example that CodePorting C#2java accurately converts C# indexers to compile-able java code.