473,769 Members | 7,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.get SelectedItem(ch oice)).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 "Substracti on" class.

"Function" is an abstract class, and the "Substracti on" 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(s tring 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 1172

"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.get SelectedItem(ch oice)).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 "Substracti on" 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("W inTest.Subtract ion");
Function function = (Function)Activ ator.CreateInst ance(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.get SelectedItem(ch oice)).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.newInstan ce(), you can use
Activator.Creat eInstance(Type) .

Here is ProgramChoice:

class ProgramChoice {
private object cls;
private string text;
public ProgramChoice(s tring 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.ge tSelectedItem(c hoice)).value() ;
function = (Function)cls.n ewInstance();

where:

class ProgramChoice {
private Class cls;
private String text;
ProgramChoice(S tring 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*****@discus sions.microsoft .comwrote:
Hello Pavel,

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

Class cls =
((ProgramChoice )functionSet.ge tSelectedItem(c hoice)).value() ;
function = (Function)cls.n ewInstance();
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.Activato r.

Pete
Sep 1 '08 #5

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

Similar topics

11
9269
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 C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
1
9651
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
13
4278
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 not that good at programming but what I need to know is in java how would i do this.................. menu appears, user selects menu option (1,2,3,4,5,6) a new menu appears after selecting option 1 asking the user to select another option...
2
2828
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 call the source code is given below and also the errors .. Plz help in resolving these. This is a code being (slightly modified ) which was downloaded from SUN website :
0
7115
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 the due date 10% of the grade is taken off. I think I'm coming down with the flu and my brain is just not processing this assignment. Here is the assignment: Modify the Inventory program by adding a button to the GUI that allows the user to move...
5
1435
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, \bin\javac.exe -classpath "C:\Program Files\Java\jdk1.5.0_11\bin" -d "C:\Program Files\Java\jdk1.5.0_11\bin" "C:\Program Files\Java\jdk1.5.0_11\bin\Car.java" Process Completed.
3
2405
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 class: (one I added all the asteriss to. // If we are at the end of input then NoSuchElement; // If there is still input left then InputMismatch private void throwFor() { skipped = false; if ((sourceClosed) &&...
1
6757
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 "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1041) at java.awt.Container.add(Container.java:365) at orderingsystem.OrderingSystem.<init>(OrderingSystem.java:261) at...
3
2611
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 Scanner(System.in) public static void main(String args); double feet, inches, feetTotal, inchTotal, centTotal; boolean done;
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.