Java to C#. Please help.

C

Charles

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
 
M

Morten Wennevik [C# MVP]

Charles said:
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
 
P

Pavel Minaev

Charles said:
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?
 
C

Charles

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
 

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

Top