Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

i want C# equivalent Java code

Thread view: 
Bala - 05 Jan 2007 10:31 GMT
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
Andrew Thompson - 05 Jan 2007 10:50 GMT
...
> is there any way to convert this code to java.

(Looks over code)  Probably - assuming I read it correctly.
(I don't code C#).
....
> please provide me  a solution

Please see a help desk.

Andrew T.
Gordon Beaton - 05 Jan 2007 10:56 GMT
> i have the following code in C# ..
>
> is there any way to convert this code to java.

No, it's impossible.

> please provide me a solution

Please send cash.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Ike - 05 Jan 2007 15:46 GMT
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 have the following code in C# ..
>
[quoted text clipped - 35 lines]
>
> please provide me  a solution
Arne Vajhøj - 06 Jan 2007 01:41 GMT
> 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
[quoted text clipped - 3 lines]
> 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
Ian Wilson - 05 Jan 2007 15:51 GMT
> 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;
[quoted text clipped - 7 lines]
>   {
>    return data[index];

what happens if index < 0 or index > 5?

>   }
>   set
>   {
>    data[index] = value;

what happens if index < 0 or index > 5?

>   }
>  }
> }

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

> class MyClient
> {
[quoted text clipped - 6 lines]
>   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?
Arne Vajhøj - 06 Jan 2007 01:43 GMT
>> 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.

Not of you follow Java coding convention.

>>  public string this [int index]
>>  {
[quoted text clipped - 3 lines]
>
> 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.

>>   }
>>   set
>>   {
>>    data[index] = value;
>
> what happens if index < 0 or index > 5?

Same.

>>   }
>>  }
>> }

Arne
Ian Wilson - 08 Jan 2007 10:27 GMT
>>> i have the following code in C# ..
>>>
[quoted text clipped - 3 lines]
>
> 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]
>>>  {
[quoted text clipped - 6 lines]
> 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".
Arne Vajhøj - 09 Jan 2007 02:04 GMT
>>>> i have the following code in C# ..
>>>>
[quoted text clipped - 8 lines]
> 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]
>>>>  {
[quoted text clipped - 11 lines]
> 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
Ian Wilson - 09 Jan 2007 10:18 GMT
>>>>> i have the following code in C# ..
>>>>> is there any way to convert this code to java.
[quoted text clipped - 9 lines]
>
> 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]);
 }
}
------------------------------------------------------------------------
Arne Vajhøj - 11 Jan 2007 02:33 GMT
> 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
Tor Iver Wilhelmsen - 07 Jan 2007 11:55 GMT
> 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.).


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.