private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.Add();
Response.Write (this.alFieldItems.Count);
}
protected System.Collections.ArrayList alFieldItems = new System.Collections.ArrayList(10); // 10对值 public void AddFieldItem(string _fieldName,object _fieldValue)  // object,因为值类型不确定!
{
for(int i=0;i<alFieldItems.Count;i++)
{
if(((DbKeyItem)this.alFieldItems[i]).fieldName == _fieldName)
{
throw new ArgumentException("The field name has existed!");
}
this.alFieldItems.Add(new DbKeyItem(_fieldName,_fieldValue));
Response.Write (this.alFieldItems.Count);
}
}
public void Add()
{
this.AddFieldItem("userName","xlingfeng");
} public class DbKeyItem
{
/// <summary>
///  字段名称
/// </summary>
public string fieldName;
/// <summary>
///  字段值
/// </summary>
public string fieldValue;
/// <summary>
///  类的构造函数
///  <para name="_fieldName">字段名称。</para>
///  <para name="_fieldValue">字段值。</para>
/// </summary>
public DbKeyItem(string _fieldName,object _fieldValue)
{
this.fieldName = _fieldName;
this.fieldValue = _fieldValue.ToString();
}
}----------预期实现效果
 将 字段和字段值放入 数组中待以后处理 为何 Count的值认为 0 呢??

解决方案 »

  1.   

    补充:
    Response.Write (this.alFieldItems.Count);  // 输出结果为0
      

  2.   

    你把this.alFieldItems.Add(new DbKeyItem(_fieldName,_fieldValue));放在了for循环里当然不会添加值进去,因为永远都进不去这个循环
      

  3.   


    public void AddFieldItem(string _fieldName,object _fieldValue)  // object,因为值类型不确定!
    {
    bool isFieldExisted = false;
    for(int i=0;i<alFieldItems.Count;i++)
    {
    if(((DbKeyItem)this.alFieldItems[i]).fieldName == _fieldName)
    {
    throw new ArgumentException("The field name has existed!");
    isFieldExisted = true;
    break;
    }
    }

    if(isFieldExisted == false)
    {
    this.alFieldItems.Add(new DbKeyItem(_fieldName,_fieldValue));
    }

    Response.Write (this.alFieldItems.Count);
    }
      

  4.   

    判断字段是否存在要等遍历完alFieldItems之后才会知道,如果不存在再往alFieldItems中添加新项。而不是在判断alFieldItems中一个项就往里面添加。