我想知道有没有办法在不使用switch或if语句的情况下将用户的输入作为命令运行?
例如,如果我有一个包含10 x 10图片框的表单,并希望将其大小更改为100更好。更好的是,有没有办法使用代码中定义的string
:
string newString = "";
然后更改所述字符串的名称:newString = "newButton" + count;
此代码的使用方式如下:
for (int count = 0; count < records) // Records being the count of records to be presented.
{
newString = "newButton" + count;
Button newString.ToString() = new Button();
} //Uses the newString to give ID name to the new buttons.
与此类似的是使用java代码使用<tag>
和out_print
命令为servlet创建表。
答案 0 :(得分:1)
好像你正在寻找一个Dictionary<T,T>
。你可以像这样实现你的循环思想:
var buttons = new Dictionary<string,Button>();
for (int count = 0; count < records)
{
newString = "newButton" + count;
var newButton = new Button();
buttons.Add(newString, newButton);
}