Converting Strings to Ints

Question:

You've got a TextField in your visual design area and you want to pass an integer that's typed into it to a method that takes an int value.  What do you do???

The hard way...

You could define a script method that calls Integer.parseInt(valueFromTextField).   You could use that script method to make the connection for the int parameter you need.

But that's overkill...

The easy way... cheat a little...

First, let me state that what I'm about to tell you is considered "bad style".  However, that only really applies to when you're coding this stuff by hand.  That said...

It is possible to call static methods via a reference to some instance of a class.  For example:

Integer someInteger = new Integer();

int crud = someInteger.parseInt("1234");         

To make matters even weirder, you can call the method even if you don't have an instance of the class!  For example:

Integer someInteger = null;

int crud = someInteger.parseInt("1234")         

This will not result in a NullPointerException!  Pretty neat, eh?  More like pretty sneaky...

Yeah, yeah, get on with it!

So how can this help us?  Suppose we have a TextField field1 and want to pass the integer value of that field to a method doSomething(int x) (suppose it's a method in some bean in the design area)  when button1 is pressed:

And you've now converted a String to an int!

But what about the "et al"?

You can do similar processing for most of the primitive types, again by dropping a variable of the Wrapper class (Double, Float...) and connecting to the appropriate conversion method.