如何解决动态设置变量的名称为文本框的名称
我目前正在从事一个项目,我将在其中创建一个自定义计算器来保存您自己的公式。
有数字上下元素,允许用户在计算器中设置一定数量的变量。列出他们使用的变量后,他们输入问题并将结果显示在输出文本框中。
我希望它工作的方式是每个文本框都有一个变量。我希望变量的名称动态更改为文本框中的文本。
在用户输入的问题中,它被设置为一个由计算机解决的字符串。
例如
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;
我该怎么做?如果有其他方法可以实现我的目标,或者如果您需要任何照片,请告诉我,并提前感谢您。
以下是一些稍微相关的网站: swift / changing name of variable in loop 和 Is it possible to dynamically name variables based off user input?
解决方法
首先,你不能这样做;在编译时需要知道变量和控件名称。但是,您可以使用字典或类似的东西,您可以在编译时或运行时创建键和值,并在运行时查询它们。
完整的人为例子:
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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。