-1

I am currently working on a project where I create a custom calculator that saves your own formulas.

There is numeric up-down element which allows the user to set a certain amount of variables in the calculator. Once the variables they are using are listed, they type in the problem and it displays the result in an output textbox.

The way I want this to work is that there will be a variable for each textbox. I want the name of the variable to be dynamically changed to the text in the textbox.

In the problem the user inputs it is set to a string which is solved by the computer.

For example

var unnamedVarForTextbox1;

//pseudo code
unnamedVarForTextbox1.name = textbox1.Text; //let's say var1 is l for length (user input)
unnamedVarForTextbox2.name = textbox2.Text; //let's say var2 is w for width (user input)
unnamedVarForTextbox3.name = textbox3.Text; //let's say var3 is h for height (user input)

string userProblem = problem.Text;
//problem.Text is l*w*h (so volume)

//Displays solved problem in output textbox
output.text = userProblem;

How would I do this? If there is another way to achieve my goal, or if you need any photos please let me know, and thank you in advance.

Here are some mildly related websites: swift / changing name of variable in loop and Is it possible to dynamically name variables based off user input?

1
  • unnamedVarForTextbox1.name This shouldn't compile. There is a property called Name, though. Not sure if it will help as you can set it freely and thus it is not guaranteed to be unique. - The usual workaround is a Dictionary. Commented Dec 22, 2020 at 9:36

1 Answer 1

1

Firstly, you can't do this; variables and control names need to be known at compile time. You can however use a dictionary or similar where you can create keys and values at compile time or runtime and query for them at runtime.

Complete contrived example:

private Dictionary<string,string _store = new Dictionary<string,string();

...


_store.Add(textbox1.Text,someValue);


...

if(_store.TryGetValue(textbox1.Text,out var someValue)
   Debug.WriteLine(someValue);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.