i want C# equivalent Java code

B

Bala

i have the following code in C# ..

is there any way to convert this code to java.

using System;
using System.Collections;

class MyClass
{
private string []data = new string[5];
public string this [int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
}


class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc[0] = "Rajesh";
mc[1] = "A3-126";
mc[2] = "Snehadara";
mc[3] = "Irla";
mc[4] = "Mumbai";

Console.WriteLine("{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}
}

please provide me a solution
 
I

Ike

There is a thing called Anakrino, that converts Java (in .NET) to C# or
VB.NET, etc. In other words, it converts the .NET bytecode (though, like
everything else, they, MS, don;t call it 'bytecode') to a target .NET
language.

I think if you can convert the Java code you have, to Java in .NET, you
could use Anakrino to convert it to c3 or VB.Net, etc. -Ike
 
I

Ian Wilson

Bala said:
i have the following code in C# ..

is there any way to convert this code to java.

Yes, I just did it. It took 7 lines of Java.
using System;
using System.Collections;

class MyClass
{
private string []data = new string[5];
public string this [int index]
{
get
{
return data[index];

what happens if index said:
}
set
{
data[index] = value;

what happens if index said:

What purpose is served by MyClass that couldn't be better achieved by
using String[] in place of MyClass in MyClient below?
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc[0] = "Rajesh";
mc[1] = "A3-126";
mc[2] = "Snehadara";
mc[3] = "Irla";
mc[4] = "Mumbai";

That's an ugly way to initialise an array!
Console.WriteLine("{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}
}

please provide me a solution

Is this homework?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Ike said:
There is a thing called Anakrino, that converts Java (in .NET) to C# or
VB.NET, etc. In other words, it converts the .NET bytecode (though, like
everything else, they, MS, don;t call it 'bytecode') to a target .NET
language.

I think if you can convert the Java code you have, to Java in .NET, you
could use Anakrino to convert it to c3 or VB.Net, etc. -Ike

It is C# code he has ...

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

T

Tor Iver Wilhelmsen

Bala said:
is there any way to convert this code to java.

Not directly: Java does not have C# indexers or the necessary operator
overloading of [].

But why are you reimplementing a simple list anyway?

For the printout, use System.out.printf("%s %s %s %s %s", etc.).
 
I

Ian Wilson

Arne said:
Not of you follow Java coding convention.

I guess you mean <http://java.sun.com/docs/codeconv/>?

It does as far as I know, the seven lines excludes comments and blank
lines though.
public string this [int index]
{
get
{
return data[index];


what happens if index < 0 or index > 5?


What is supposed to happen if a C# indexer is called
with an index out of range.

I've no idea, since I don't know C#, which is why I asked.

If the OP relies on C# doing something clever that Java doesn't, my 7
lines would not represent a complete "conversion".
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Ian said:
I guess you mean <http://java.sun.com/docs/codeconv/>?

It does as far as I know, the seven lines excludes comments and blank
lines though.

Not counting end brackets and the main program either I assume.
public string this [int index]
{
get
{
return data[index];

what happens if index < 0 or index > 5?

What is supposed to happen if a C# indexer is called
with an index out of range.

I've no idea, since I don't know C#, which is why I asked.

If the OP relies on C# doing something clever that Java doesn't, my 7
lines would not represent a complete "conversion".

An exception. C# and Java will behave reasonable similar.

C# gives a System.IndexOutOfRangeException

Java gives a java.lang.ArrayIndexOutOfBoundsException

Arne
 
I

Ian Wilson

Arne said:
Not counting end brackets and the main program either I assume.
You assume wrongly, seven lines comprise a complete working example that
runs in Eclipse :)

Well, since you seem so interested, I'll post it here, then you can have
the satisfaction of telling me where I am going wrong :).

I didn't post it earlier in order to avoid discouraging newbies from
thinking for themselves a little.

Any conversion of C# to Java could be done at many levels, statement by
statement would be one. Producing the same output would be another.
Somewhere in between there are a range of conversions which, to varying
degrees, employ Java idioms or features in place of C# idioms or features.

Recall, in my original post I said ...

"What purpose is served by MyClass that couldn't be better achieved by
using String[] in place of MyClass in MyClient below?"

Well I chose a level of conversion which assumed that there was no real
need for MyClass in order to produce the same output. Maybe MyClass is
unnecessary in C# also but was included for ulterior reasons (e.g. to
demonstrate some aspect of C# programming) in which case my conversion
could be regarded as "incorrect". The OP doesn't provide enough
information to decide.

So, excluding blank lines and comments, here are the 7 lines I came up
with ...
-----------------------------------------------------------------------
class MyClient {
public static void main(String[] args) {
String[] mc = {"Rajesh","A3-126","Snehadara","Irla","Mumbai"};
System.out.printf("%s,%s,%s,%s,%s,%s",
"{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}
}
-----------------------------------------------------------------------

There you are, elementary stuff, nothing clever or surprising. A bit
shorter than the OP's C# original even accounting for the different
conventions for opening brackets.

Note that I did not, and am not, claiming that an equivalent program
couldn't be written in seven or fewer lines in C#. I merely noted that
my conversion produced 7 lines of Java.

-----------------------------------------------------------------------
using System;
using System.Collections;

class MyClass
{
private string []data = new string[5];
public string this [int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
}


class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc[0] = "Rajesh";
mc[1] = "A3-126";
mc[2] = "Snehadara";
mc[3] = "Irla";
mc[4] = "Mumbai";

Console.WriteLine("{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}
}
------------------------------------------------------------------------
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Ian said:
You assume wrongly, seven lines comprise a complete working example that
runs in Eclipse :)

Well, since you seem so interested, I'll post it here, then you can have
the satisfaction of telling me where I am going wrong :).
Well I chose a level of conversion which assumed that there was no real
need for MyClass in order to produce the same output. Maybe MyClass is
unnecessary in C# also but was included for ulterior reasons (e.g. to
demonstrate some aspect of C# programming) in which case my conversion
could be regarded as "incorrect". The OP doesn't provide enough
information to decide.

OK

:)

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top