這個實例中定義了兩個屬性,看不太明白是怎么回事兒,以前沒用過foreach循環,請詳解,謝謝……using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;public partial class Diary : System.Web.UI.Page{public int ArrayIndex{get {return Convert.ToInt32(ViewState["ArrayIndex"]);//從ViewState中讀取ArrayIndex值}set{ViewState["ArrayIndex"]=value;}}public string[] Juarnal{get{return (string[])ViewState["Juarnal"];}set{ViewState["Juarnal"] = value;}}protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){ViewState["ArrayIndex"] = 0;ViewState["Juarnal"] = new string[31];}}protected void Button1_Click(object sender, EventArgs e){this.Label1Error.Text = "";string Str = this.TextBoxDiary.Text;if (Str != ""){if (ArrayIndex < Juarnal.Length){Juarnal[ArrayIndex] = Str;//賦值ArrayIndex = ArrayIndex + 1;this.Label1Error.Text = "成功";}else{this.Label1Error.Text = "不能超過31天";}}else{this.Label1Error.Text = "你還沒有寫日記";}}protected void Button2_Click(object sender, EventArgs e){this.Label1Error.Text = "";this.LabelShow.Text = "";foreach (string j in Juarnal){if (j != null){this.LabelShow.Text += j + "<br/>";}}}}主要是get 和set搞不明白ViewState["ArrayIndex"]=value;value在這里的意義是什么,看了好多例子,全是這一個參數值
2 回答

ABOUTYOU
TA貢獻1812條經驗 獲得超5個贊
這是迭代的應用,為什么能用迭代,對于繼承了IEnumerable接口的對象 譬如數組,集合,泛型類都可以使用迭代
foreach (string j in Juarnal)
表示 迭代字符串數組Juarnal的項,j是字符串

慕妹3146593
TA貢獻1820條經驗 獲得超9個贊
foreach底層也是采用的迭代器實現的。主要是用來遍歷。例子List<String>
list=new
ArrayList<>(String);list.add("aa");.....//添加值略然后遍歷for(String
temp:list){System.out.println(temp);}這樣就可以把list里面的值全輸出出來
添加回答
舉報
0/150
提交
取消