What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 8576 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > Automatically Convert C# Indexers to Java using CodePorting Engine ...
Zarfishan

Automatically Convert C# Indexers to Java using CodePorting Engine

 Code Snippet posted by: Zarfishan | Posted on: 6/20/2012 | Category: C# Codes | Views: 383 | Status: [Member] | Points: 40 | Alert Moderator   


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 the 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.
Found interesting? Add this to:


 Responses

T.Saravanan
Posted by: T.Saravanan | Posted on: 6/23/2012 | Level: Silver | Status: [Member] [MVP] | Points: 10 | Alert Moderator 

Kindly post your code inside the code tag.

Thanks,
T.Saravanan

>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/23/2013 7:17:14 PM