这个实例中定义了两个属性,看不太明白是怎么回事儿,以前没用过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
提交
取消