Using a String as a Variable at Run Time

How to access a variable set at run time with a string?

INFO is a constant and, if it's declared in the global scope, you can retrieve it with:

Object.const_get "info".upcase 
Object.const_get param[:type].upcase

Is it possible to create variables at runtime in Java?

Is it possible to create variables at runtime in Java?

The simple answer is No.

Java is a static language and does not support the injection of new variable declarations into an existing compiled program. There are alternatives (in order of decreasing usefulness / increasing difficulty):

  • Represent your "variables" as name / value pairs in a Map. Or come up with some other design that doesn't require real dynamic variables.
  • Use a scripting language that runs on the JVM and is callable from Java.
  • Use some kind of templating mechanism to generate new source code containing the declarations, and compile and load it dynamically.
  • Use a byte code manipulation library (e.g. BCEL) to create class files on the fly and then dynamically load them.

The first approach is the best. Java is a static language, and works best if you don't fight it. If this is a problem for you, maybe you are using the wrong language.

The last two are difficult / complicated and have significant performance costs. They are almost certainly not going to help ...

How to change variable name in runtime

Put your buttons in a HashMap<String, Button>, and this way you could name your buttons and rename them as many times as you want. You cannot rename variables in runtime.

Here is an example:

// this will keep the variables this way: [ "btn01" -> buttonObject1, "btn02" -> buttonObject2 ...]
HashMap<String, Button> buttonHashMap = new HashMap<>();

for(int i = 0; i < 10; i++)
{
final int buttonInd = i;
String buttonName = "btn0" + i;
Button buttonObject = new Button(Integer.toString(i));

buttonHashMap.put(buttonName, buttonObject);

// The .get(buttonName) gets the specific Button that you need!
buttonHashMap.get(buttonName).setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
System.out.println("Button pressed " + ((Button) e.getSource()).getText());
System.out.println("button clicked " + ((Control)e.getSource()).getId());
lastClickedIndex = buttonInd;
}
});
}

Convert string to variable name or variable type

No, this is not possible. This sort of functionality is common in scripting languages like Ruby and Python, but C++ works very differently from those. In C++ we try to do as much of the program's work as we can at compile time. Sometimes we can do things at runtime, and even then good C++ programmers will find a way to do the work as early as compile time.

If you know you're going to create a variable then create it right away:

int count;

What you might not know ahead of time is the variable's value, so you can defer that for runtime:

std::cin >> count;

If you know you're going to need a collection of variables but not precisely how many of them then create a map or a vector:

std::vector<int> counts;

Remember that the name of a variable is nothing but a name — a way for you to refer to the variable later. In C++ it is not possible nor useful to postpone assigning the name of the variable at runtime. All that would do is make your code more complicated and your program slower.



Related Topics



Leave a reply



Submit