zarfishan zahid

zarfishan zahid

  • NA
  • 0
  • 18.6k

Automatically convert CSharp Out and Ref Keyword to Java using CodePorting Engine

Jul 24 2012 10:49 PM

In my Previous Blog I have told you that CodePorting C#2Java Application uses a powerful and absolute parsing engine. It can parse and converts thousands of lines of code from C# to Java in just few seconds. Parsing engine is fully compatible to International standards like ECMA 334 and 335.It supports syntax and significs of latest version for Microsoft C# language and can easily translate it to Java.

Following are some features of CodePorting Parsing Engine:

·         Provide most accurate conversion

·         Preserves code formatting

·         Support customized library mapping

·         Its extensible library mapping allows you to add maps for third party libraries

·         It has already mapped thousands of .Net libraries and converted to java and count is still going on

·         Requires no installation just sign-in and start converting C# code to Java

·         It converts your code instantly, just paste your code and it will be converted in seconds

·        Fastest conversion, converts mega projects with million of lines of code in few minutes.

·         Support projects/solutions from all versions of Visual Studio(2002, 2003, 2005, 2008, 2010)

·         Automatically handle partial classes and partial methods

·         Input code formatting does not matter, can convert badly formatted code easily

·         Automatically renames members to Java style

·         Supports default arguments and automatic properties

·         Supports complex language constructs like Out, Ref, multidimensional arrays, enums, lifted nullable types

 

In C# out keyword is used while passing some arguments by reference.  Out is somewhat similar to ref keyword.  The only difference is that ref requires variable that needed to be passed. Both out and ref are not available in Java. Lets learn how C#2Java engine handles them while converting code to Java.

The following code represents the example:

C# Code:

 

namespace C#2Java.Examples.Convert.LanguageConstructs.OutRef

{

    public class Example26

    {

        //single-line statement

        void Method()

        {

            int i;

            do

                MethodRef(out i);

            while (true);

        }

 

        //workaround of above that can be autoported

        void Method2()

        {

            int i;

            bool ret;

            do

            {

                MethodRef(out i);

                ret = MethodRef(out i);

            }

            while (ret);

        }

 

        bool MethodRef(out int outI)

        {

            outI = 42;

            return true;

        }

    }

}

Generated Java Code:

 

package C#2Java.Examples.Convert.LanguageConstructs.OutRef;

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

public class Example26

{

    //single-line statement

    private void method()

    {

        int i = 0;

        do

                    {

            int[] referenceToI = { i };

            methodRef(/*out*/ referenceToI);

            i = referenceToI[0];

                    }

        while (true);

    }

    //workaround of above that can be autoported

    private void method2()

    {

        int i = 0;

        boolean ret;

        do

        {

            int[] referenceToI = { i };

            methodRef(/*out*/ referenceToI);

            i = referenceToI[0];

            referenceToI[0] = i;

            ret = methodRef(/*out*/ referenceToI);

            i = referenceToI[0];

        }

        while (ret);

    }

 

    private boolean methodRef(/*out*/ int[] outI)

    {

        outI[0] = 42;

        return true;

    }

}

It is clear from the above example that CodePorting Engine use java arrays where values are sent by reference. This perfectly imitates the functionality of the out and ref keywords.