Re: [問題] 請問可否用迴圈跑出變數名稱?

看板C_Sharp作者 (不放過自己)時間17年前 (2008/06/04 11:48), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串2/2 (看更多)
如果你是寫網頁 那麼你可以在迴圈內利用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
文章代碼(AID): #18HX1_S1 (C_Sharp)
文章代碼(AID): #18HX1_S1 (C_Sharp)