一个自定义数据类型,教你使用索引器
来源:互联网 发布: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
}
- 一个自定义数据类型,教你使用索引器
- 使用 dynamic_templates自定义动态索引
- 一个使用c++在lua中创建自定义数据类型的简易方法
- 一个使用c++在lua中创建自定义数据类型的简易方法
- 自定义索引器
- 手把手教你做一个自定义表格标签
- 手把手教你做一个自定义表格标签
- 手把手教你做一个自定义表格标签
- 手把手教你写一个完整的自定义View
- 手把手教你写一个完整的自定义View
- 手把手教你写一个完整的自定义View
- 使用VideoView自定义一个播放器控件
- SqlServer2000中用户自定义数据类型的使用
- COM组件中使用用户自定义数据类型
- SqlServer2000中用户自定义数据类型的使用
- 如何在Qt中使用自定义数据类型
- 如何在Qt中使用自定义数据类型
- COM组件中使用用户自定义数据类型
- 很菜很基础的html问题
- 翻译1-5的数字的英文
- oracle中子句执行顺序
- 针对选择语句select case的使用!
- 2005年度BBS人气最高妹妹照片排行榜
- 一个自定义数据类型,教你使用索引器
- 我今天就要离开我的这个公司了 不知道什么时候才能更新blog
- 惨痛啊
- 300路上的女孩
- JSP中的注释
- Checked还是Unchecked
- 卖菠萝的女人
- 2005年最感人的帖子!!!
- 重大危险源辨识(收集)