Forums Register Login

Calling variable or methods with Strings?

+Pie Number of slices to send: Send
Halo everyone

I'm new here and I'm wondering how to call variables or methods with Strings in Java?

Below is what I normally do in ActionScript3(flash) using this["String"];

var cButtons:Vector.<Button> = this["_" + name + "Buttons"];

so I can easily iterate or change the name variable to access any other variable.
And using this["myMethod" + num](); to call a bunch of methods

But it doesn't seem to work in java
How do you do it in java?

Thanks


+Pie Number of slices to send: Send
That probably isn't something you want to be doing in Java. Usually there are better ways than the literal copy of the ActionScript code. Java tends more to have classes which do things than tricky syntax which does things.

For example if you have a mapping from Strings to Buttons, then you would simply create a Map<String, Button> object and use its built-in methods.

And I can't imagine any language where you would have methods named myMethod1, myMethod2, and myMethod3 and want to call them sequentially. That's probably more ActionScript providing syntax options which lead you down the bad-design path.

I would suggest going through the Java tutorials for beginners. That way you will see what Java programs look like, and you won't end up speaking Java with a heavy ActionScript accent.
+Pie Number of slices to send: Send

Never worked in actionscript, but based on what you described, actionscript objects seems very similar to javascript objects -- in that it can be used like an associative array. It may be better to not do that in java, and instead, actually have the Java object have an associative array. In java, these types of arrays are implemented as Maps.... such as the java.util.HashMap class.

Henry
+Pie Number of slices to send: Send
I'm using the newest version of actionscript and its oop as well, with packages, inheritances etc.
I know how to use hashmaps, lists, and stuff already, actionscript have dictionary class similar to this too, but it seems like an extra step to take

I'm not really calling methods1, methods2, methods 3 etc, What I'm wanting to do is to make the code expandable and otherwise I will just have a massive big list of switch case...and if else

say I have a variable called name, another called nameTextField, another called nameBar, another nameWhatever
then I can just iterate through an array ["name", "password", "address", .....]

for(int i=0; i<array.length; i++){
this[array[i] + "TextField"];
this[array[i] + "Bar"];
this[array[i] + "Whatever"];
}

each of them are the actual reference to the variable, so I can do something like this[array[i] + "TextField"].subString(); and I can easily add stuff to the array to expand it


what I'm doing is a lot more complicated than this actually, its a massuser program, involves gettings lots of data from the database, and needs to be expandable
involves both actionscript and java

want a quick and lazy way to do this for the java part , wouldn't using HashMaps make it more complicated, I normally only use HashMaps for objects
is there not a way to get a variable e.g. int _type when I just have a string with the name "type" ?

thanks
+Pie Number of slices to send: Send
 

Adam Yaure wrote:
want a quick and lazy way to do this for the java part , wouldn't using HashMaps make it more complicated, I normally only use HashMaps for objects
is there not a way to get a variable e.g. int _type when I just have a string with the name "type" ?



If you don't want to use a hashmap, and want to use the variable names directly, then you can use the reflection library.... but be warned. A couple of hours with the reflection library, and hashmaps will definitely not be "more complicated".

Henry
+Pie Number of slices to send: Send
 

Adam Yaure wrote:I'm not really calling methods1, methods2, methods 3 etc, What I'm wanting to do is to make the code expandable and otherwise I will just have a massive big list of switch case...and if else



Well, again, you can replace a massive switch-case or if-else in Java by a Map whose keys are whatever it is in your design and whose values are Runnable objects.

But the bottom line is that Java is not a scripting language so it's not a good idea to try to pretend that it is.
+Pie Number of slices to send: Send
lol reflection is insane

guess I'll need to put them into maps
thanks


btw this forum is sooo active @@
+Pie Number of slices to send: Send
Flash actionscripts use javascript.

Scripting languages like javascript are executed 'on the fly' meaning you can have an 'eval' function that can treat a string thats dynamically
generated on the fly as a valid command statement.

Thus, on the go, you can create functions, arrays, etc. directly through strings and even allow method calls to be done in this manner.

While java, is a compiled language, you cannot do functionality like adding new properties/functions to existing objects, you can only utilise
the methods that are already declared in code.

Though this may seem like a drawback to you, it actually amounts to cleaner coding.
I do not want to arise the age old debate between scripters and language coders ...

personally I dislike the use of prototypes, but thats just me
+Pie Number of slices to send: Send
 

salvin francis wrote:Flash actionscripts use javascript.

Scripting languages like javascript are executed 'on the fly' meaning you can have an 'eval' function that can treat a string thats dynamically
generated on the fly as a valid command statement.

Thus, on the go, you can create functions, arrays, etc. directly through strings and even allow method calls to be done in this manner.

While java, is a compiled language, you cannot do functionality like adding new properties/functions to existing objects, you can only utilise
the methods that are already declared in code.

Though this may seem like a drawback to you, it actually amounts to cleaner coding.
I do not want to arise the age old debate between scripters and language coders ...

personally I dislike the use of prototypes, but thats just me



eval() is gone now,,I use strings to call variables and methods, I can't create them with strings on the fly...

=3= you should check out the newest version of ActionScript 3, you'll be surprised how much it has changed since the last version (I hate prototypes too and old actionscript)
http://www.infoq.com/articles/actionscript-java

it is real oop and compiled (apart from constructor and $init is interpreted), it has classes, inheritance, interfaces, namespace, events ...etc tho lacks concurrency...
not prototypes!

I still do declare everything private, after I've declared a global variable _something doesn't it makes sense I should be able to call it with "_some" + "thing", I like dynamic class tho =]
I learnt java first actually, really hated swing then, so went on to actionscript3, which can do things much faster(for me)

Anyway, they are for different things anyway, one for server and one for client side.
Guess I can live with indexing, got to learn more about concurrency now
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1742 times.
Similar Threads
Good practice or Bad practice(tm) ?
static variable?!
Calling object methods using reference to variables
Java Reflection
Method calling
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:29:40.