一个自定义数据类型,教你使用索引器

来源:互联网 发布:js base64加密 编辑:程序博客网 时间:2024/05/22 10:29

在逻辑层到数据层的传递时。存储过程参数过多的时候。用datatable对象来传递有太消耗资源了。不如自己写个类型的数据来做。于是参考其他一些资料就改写了一个。用他做函数的返回类型。还有传递参数的时候的栽体。

using System;

namespace ParentClassNameSpace
{
 /// <summary>
 /// 自定义数据类型 500sea修改 发出去,
 /// </summary>
 public class MyArray
 {

  protected object[] _items;

  protected int _count = 0;

  /// <summary>
  /// 构造数组初始长度
  /// </summary>
  /// <param name="initialSize">初始长度</param>
  public MyArray(int initialSize)
  {
   _items = new object[initialSize];
  }

  /// <summary>
  /// 字符串索引器
  /// </summary>
  public object this[string key]
  {
   get{ return KeyToObject(key);}
   set{ AddToArray(key, value); }
  }

  /// <summary>
  /// 长度属性
  /// </summary>
  public int Length
  {
   get { return _count; }
  }

  /// <summary>
  /// 添加数组对象函数
  /// </summary>
  protected void AddToArray(string key, object item)
  {
   if(KeyExists(key))
   {
    for(int n = 0; n < _count; ++n)
    {
     KeyItemPair pair = (KeyItemPair)_items[n];
     if(string.Compare(key,pair.key,false)==0)
      _items[n] = new KeyItemPair(key, item);
    }
   }
   else
   {
    if(_count == _items.Length)
    {
     IncreaseCapacity();
    }
    _items[_count] = new KeyItemPair(key, item);
    _count++;
   }
  }

  /// <summary>
  /// 检测是否有重复key健值函数
  /// </summary>
  protected bool KeyExists(string key)
  {
   for(int n = 0; n < _count; ++n)
   {
    KeyItemPair pair = (KeyItemPair)_items[n];
    if(string.Compare(key,pair.key,false)==0)
     return true;
   }
   return false;
  }
  
  /// <summary>
  /// 索引器用获取对象函数
  /// </summary>
  /// <param name="key"></param>
  /// <returns></returns>
  protected object KeyToObject(string key)
  {
   for(int n = 0; n < _count; ++n)
   {
    KeyItemPair pair = (KeyItemPair)_items[n];
    if(string.Compare(key,pair.key,false)==0)
     return pair.item;
   }
   return null;
  }

  /// <summary>
  /// 数组自动加长函数
  /// </summary>
  protected void IncreaseCapacity()
  {
   int size = _items.Length + 20;
   object [] oldArray = _items;
   _items = new object[size];
   oldArray.CopyTo(_items, 0);
  }

  /// <summary>
  /// 结构体,自定义
  /// </summary>
  protected struct KeyItemPair
  {
   public KeyItemPair(string k, object obj)
   {
    key = k;
    item = obj;
   }
   public object item;
   public string key;
  }

  #region 数字索引器
        
  /// <summary>
  /// 数字索引器
  /// </summary>
  public object this[int key]
  {
   get{ return KeyToObject(key);}
   set{ AddToArray(key, value); }
  }

  /// <summary>
  /// 数字索引器用获取对象函数
  /// </summary>
  /// <param name="key"></param>
  /// <returns></returns>
  protected object KeyToObject(int key)
  {
   if(key>=0 && key <_count)
   {
    KeyItemPair pair = (KeyItemPair)_items[key];
    return pair.item;
   }
   else
    return null;
  }

  /// <summary>
  /// 添加数组对象函数
  /// </summary>
  protected void AddToArray(int key, object item)
  {
   if(KeyExists(key))
   {
    KeyItemPair pair = (KeyItemPair)_items[key];
    _items[key] = new KeyItemPair(key.ToString(), item);
   }
   else
   {
    if(_count == _items.Length)
    {
     IncreaseCapacity();
    }
    _items[_count] = new KeyItemPair(key.ToString(), item);
    _count++;
   }
  }

  /// <summary>
  /// 检测是否有重复key健值函数
  /// </summary>
  protected bool KeyExists(int key)
  {
   if( key>=0 && key < _count )
    return true;
   else
    return false;
  }
  
  #endregion

  #region 使用方法

  /*
   * 使用方法,保留
   *
   MyArray foodFavorites =new MyArray(1);
   foodFavorites["Ali"] = "Plain Cheeseburger";
   foodFavorites["Mackenzie"] = "Macaroni and Cheese";
   foodFavorites["Mickey"] = "Risotto with Wild Mushrooms";
   foodFavorites["Rene"] = "Escargots";

   this.Response.Write(foodFavorites["Ali"]);
   this.Response.Write("<br>");
   this.Response.Write(foodFavorites["Mackenzie"]);
   this.Response.Write("<br>");
   this.Response.Write(foodFavorites["Mickey"]);
   this.Response.Write("<br>");
   this.Response.Write(foodFavorites["Rene"]);

   MyArray a1 =new MyArray(1);
   a1[0] = "Plain Cheeseburger";
   a1[1] = "Macaroni and Cheese";
   a1[2] = "Risotto with Wild Mushrooms";
   a1[3] = "Escargots";

   this.Response.Write("<br>------------------------<br>");
   this.Response.Write(a1[0]);
   this.Response.Write("<br>");
   this.Response.Write(a1["1"]); //this.Response.Write(a1[1]);//这样转换等价
   this.Response.Write("<br>");
   this.Response.Write(a1[2]);
   this.Response.Write("<br>");
   this.Response.Write(a1[3]);
  *
  *
  */

  #endregion

 }//end class

}