473,395 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Java to C#. Please help.

Hello all,

I'm trying to convert a Java Genetic Programming program to C#.
I don't know how to convert the following Java code:

Object choice = functionSet.getSelectedItem(choice)).value();
Class cls = ((ProgramChoice) choice;
function = (Function) cls.newInstance();

The object "choice" shows that a Subtraction class (i.e. "sub") was selected,
and therefore, "Function" should be set to the "Substraction" class.

"Function" is an abstract class, and the "Substraction" classs is:

internal class Subtraction : Function {
internal Subtraction() {
arg = new GeneticProgram[2];
}
override internal string Name {
get {
return "sub";
}
}
override internal double eval(double x) {
return arg[0].eval(x) - arg[1].eval(x);
}
}

Here is ProgramChoice:

class ProgramChoice {
private object cls;
private string text;
public ProgramChoice(string text_, object cls_) {
cls = cls_;
text = text_;
}
public override string ToString() {
return text;
}
public object Value {
get {
return cls;
}
}
}

Any help will be greatly appreciated.
Charles
Sep 1 '08 #1
4 1157

"Charles" wrote:
Hello all,

I'm trying to convert a Java Genetic Programming program to C#.
I don't know how to convert the following Java code:

Object choice = functionSet.getSelectedItem(choice)).value();
Class cls = ((ProgramChoice) choice;
function = (Function) cls.newInstance();

The object "choice" shows that a Subtraction class (i.e. "sub") was selected,
and therefore, "Function" should be set to the "Substraction" class.
The additional code could pretty much be compiled as C# without
modifications, but the above three lines would not, especially the first line
which doesn't make any sense. The second line doesn't make much more sense,
but it looks like you are trying to get a Type object and creating an
instance of this type in the third line. In that case you would do something
like

Type cls = Type.GetType("WinTest.Subtraction");
Function function = (Function)Activator.CreateInstance(cls, true);

WinTest was added due to my sample putting it under a WinTest namespace.
The overloaded CreateInstance specifying true as the last parameter was due
to Subtraction having a non public (internal) constructor.

function will be an instance of Subtraction.

If I have misunderstood, please clarify. You can also check the C# for Java
developers page

http://msdn.microsoft.com/en-us/vs2005/aa700844.aspx

---
Happy Coding!
Morten Wennevik [C# MVP]
Sep 1 '08 #2
Charles wrote:
Hello all,

I'm trying to convert a Java Genetic Programming program to C#.
I don't know how to convert the following Java code:

Object choice = functionSet.getSelectedItem(choice)).value();
Class cls = ((ProgramChoice) choice;
function = (Function) cls.newInstance();
This isn't valid Java. Aside from a couple of parentheses missing,
there's no way you could assign (ProgramChoice)choice to a variable of
type Class. Now, if you mean this:

Class class = ((ProgramChoice)choice).Value;

then I can see it working.

Regardless, the analog of Object.getClass() in .NET is
Object.GetType(), and instead of Class.newInstance(), you can use
Activator.CreateInstance(Type).

Here is ProgramChoice:

class ProgramChoice {
private object cls;
private string text;
public ProgramChoice(string text_, object cls_) {
cls = cls_;
text = text_;
}
public override string ToString() {
return text;
}
public object Value {
get {
return cls;
}
}
}
If "cls" is always a Type object (was Class in Java), then why not
declare it as such, rather than plain Object?
Sep 1 '08 #3
Hello Pavel,

Sorry if I got my previous post confused.
Here is the Java code:

Class cls = ((ProgramChoice)functionSet.getSelectedItem(choice )).value();
function = (Function)cls.newInstance();

where:

class ProgramChoice {
private Class cls;
private String text;
ProgramChoice(String text, Class cls) {
this.cls = cls;
this.text = text;
}
public String toString() { return text; }
Class value() { return cls; }
}

This code runs under Java "1.6.0_10-ea"

This is the code I'm having trouble converting to C#

Charles
Sep 1 '08 #4
On Sun, 31 Aug 2008 23:38:00 -0700, Charles
<Ch*****@discussions.microsoft.comwrote:
Hello Pavel,

Sorry if I got my previous post confused.
Here is the Java code:

Class cls =
((ProgramChoice)functionSet.getSelectedItem(choice )).value();
function = (Function)cls.newInstance();
Not that reflection is usually a great way to design an application in
Java _or_ .NET, but...

I think the classes you're looking for are System.Type and
System.Activator.

Pete
Sep 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
13
by: Andrew Bell | last post by:
I'm doing a uni course in the UK and one of my semesters is about programming. This is just going to be compilied and executed with no menu just using command promt (javac classfile.class) I am...
2
by: ramasubramanian.rahul | last post by:
hi i am trying to call some java APIs from c . i use the standatd JNI calls to load the JVM from a c program and call all java functions by using a pointer to the jvm which was returned by the JNI...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
5
by: KiranJyothi | last post by:
Can Anybody please help me in installing Java. I installed JCreatorI in c:/program files/java and once I am trying to compile my java programs, I am getting this thing : Error : Invalid path,...
3
by: Dameon99 | last post by:
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the...
1
by: onlinegear | last post by:
HI i am writing this for college i know i have loads of combo boxes with nothing in the i havent got that far yet. but every time i run this is comes up with this erro run: Exception in thread...
3
by: kr151080 | last post by:
I need some help with a Measurement Program I Wrote.....I have this code as the main workings of my program: import java.util.*; public class Measurement{ { static Scanner console = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.