Re: [問題] 請問可否用迴圈跑出變數名稱?
如果你是寫網頁
那麼你可以在迴圈內利用Page的FindControl Method去尋找目標的TextBox
e.g.
foreach( string temp in words )
{
TextBox textBox = (TextBox)this.Page.FindControl( temp + "_buy" );
textBox.Text = "4";
}
如果你不是用來處裡ASP.NET Web Controls, 那麼命名那麼多變數要處理還蠻雜的,
我會選擇利用Dictionary,
e.g.
Dictionary<string, string> variables = new Dictionary<string, string>();
foreach( string temp in words )
{
variables[ temp + "_buy" ] = "4";
}
※ 引述《Futurend (歸人)》之銘言:
: 例如我有一個陣列為 string[] words = {"A", "B", "C", "D", "E"};
: 在網頁也有許多的textbox用這個當開頭命名
: 例如:A_use, B_use, C_use, D_use, E_use
: A_buy, B_buy, C_buy, D_buy, E_buy,
: 要怎麼用foreach來做指定如
: foreach(string temp in words)
: {
: temp + "_buy" = "4"
: }
: 之類的效果呢?
: 或著有更好的方法
: 感謝了
--
對於已經無法擁有的
唯一能做的是
不要忘記
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.125.251.225
→
06/04 11:56, , 1F
06/04 11:56, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):