一个自定义Collection类的实现(C#) 第一篇工作日志

决心从今天起开始写工作日志。可以对工作的总结和对人生感悟。很多年后,也许会是一笔厚厚的财富。

今天用C#实现了一个Collection类。放着现成.net类库中的Array,ArrayList,HashTable等等集合类不用,为什么要重写一个呢。
不知道大家有没有遇到这种情况。项目开发过程中会遇到状态,性别,某某类型等情况,显示给用户看的是文本,而存储在数据库中的是值。比如性别,可能有 男对应1,女对应0。怎么在程序中完成这种对应呢。
   第一种采用系统参数的办法。专门为此建一个参数表。
   第二种在程序中对这些 文本--值 的类型进行定义。虽然可以使用.net类库中数组或其他集合定义,但是将来使用这些定义时不方便。比如根据值获得文本,从数据库取得0,如何判断它对应的文本值女。或者是向下拉控件中绑定这些值。都不是很方便。
   所以我定义了新的集合类TypeCollection。以下是代码。贴出来和大家切磋。

 

  1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
  2InBlock.gif        /// 文本-值  类型的类
  3ExpandedBlockEnd.gif        /// </summary>

  4 None.gif          public   class  Type
  5 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
  6InBlock.gif            //构造函数
  7InBlock.gif            public Type(string strName,string strValue)
  8ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
  9InBlock.gif                this._name=strName;
 10InBlock.gif                this._value=strValue;
 11ExpandedSubBlockEnd.gif            }

 12InBlock.gif
 13InBlock.gif            public Type()
 14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 15InBlock.gif
 16ExpandedSubBlockEnd.gif            }

 17InBlock.gif
 18InBlock.gif            private string _name="";
 19InBlock.gif            private string _value="";
 20InBlock.gif
 21ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 22InBlock.gif            /// 获取Type的文本
 23ExpandedSubBlockEnd.gif            /// </summary>

 24InBlock.gif            public string Name
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 26ExpandedSubBlockStart.gifContractedSubBlock.gif                getdot.gif{return this._name;}
 27ExpandedSubBlockEnd.gif            }

 28InBlock.gif
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 30InBlock.gif            /// 获取Type的值
 31ExpandedSubBlockEnd.gif            /// </summary>

 32InBlock.gif            public string Value
 33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 34ExpandedSubBlockStart.gifContractedSubBlock.gif                getdot.gif{return this._value;}
 35ExpandedSubBlockEnd.gif            }

 36ExpandedBlockEnd.gif        }

 37 None.gif
 38 ExpandedBlockStart.gifContractedBlock.gif         /**/ /// <summary>
 39InBlock.gif        /// 用来存放 文本-值  类型的类的集合
 40ExpandedBlockEnd.gif        /// </summary>

 41 None.gif          public   class  TypeCollection
 42 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 43InBlock.gif            private DataTable dt=null;
 44InBlock.gif            public const string NameField="Name";
 45InBlock.gif            public const string ValueField="Value";
 46InBlock.gif
 47InBlock.gif            public TypeCollection()
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 49InBlock.gif                dt=new DataTable();
 50InBlock.gif                dt.Columns.Add(NameField);
 51InBlock.gif                dt.Columns.Add(ValueField);
 52ExpandedSubBlockEnd.gif            }

 53InBlock.gif
 54InBlock.gif            public TypeCollection(string[,] listItem)
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 56InBlock.gif                dt=new DataTable();
 57InBlock.gif                dt.Columns.Add(NameField);
 58InBlock.gif                dt.Columns.Add(ValueField);
 59InBlock.gif                
 60InBlock.gif                if(listItem.Length>0)
 61ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 62InBlock.gif                    //获取一维个数
 63InBlock.gif                    int count=listItem.Length/listItem.Rank;
 64InBlock.gif                    for(int i=0;i<count;i++)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 66ExpandedSubBlockStart.gifContractedSubBlock.gif                        dt.Rows.Add(new object[]dot.gif{listItem[i,0],listItem[i,1]});
 67ExpandedSubBlockEnd.gif                    }

 68ExpandedSubBlockEnd.gif                }

 69ExpandedSubBlockEnd.gif            }

 70InBlock.gif
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 72InBlock.gif            /// 获取TypeCollection中item
 73InBlock.gif            /// </summary>
 74InBlock.gif            /// <param name="strValue">item的值</param>
 75ExpandedSubBlockEnd.gif            /// <returns></returns>

 76InBlock.gif            public Type GetItemByValue(string strValue)
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 78InBlock.gif                DataRow[] rows=dt.Select(string.Format("Value='{0}'",strValue.Trim()));
 79InBlock.gif                
 80InBlock.gif                if(rows!=null&&rows.Length>0)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 82InBlock.gif                    return new Type(rows[0].ToString(),rows[1].ToString());
 83ExpandedSubBlockEnd.gif                }

 84InBlock.gif                else
 85ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 86InBlock.gif                    return null;
 87ExpandedSubBlockEnd.gif                }

 88ExpandedSubBlockEnd.gif            }

 89InBlock.gif
 90ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 91InBlock.gif            /// 返回TypeCollection的DataTable
 92InBlock.gif            /// </summary>
 93ExpandedSubBlockEnd.gif            /// <returns></returns>

 94InBlock.gif            public DataTable GetTypeAsDataTable()
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 96InBlock.gif                return dt;
 97ExpandedSubBlockEnd.gif            }

 98InBlock.gif
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
100InBlock.gif            /// 绑定列表控件
101InBlock.gif            /// </summary>
102InBlock.gif            /// <param name="ddl">列表控件</param>
103InBlock.gif            /// <param name="defaultText">默认项Text</param>
104InBlock.gif            /// <param name="defaultValue">默认项value</param>
105ExpandedSubBlockEnd.gif            /// <param name="exitDefault">是否要默认项</param>

106InBlock.gif            private void BindDDlControl(ListControl objControl,string defaultText,string defaultValue,bool exitDefault)
107ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
108InBlock.gif                if(dt!=null&&dt.Rows.Count>0)
109ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
110InBlock.gif                    objControl.Items.Clear();
111InBlock.gif
112InBlock.gif                    //添加默认项
113InBlock.gif                    if(exitDefault)
114ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
115InBlock.gif                        objControl.Items.Add(new ListItem(defaultText,defaultValue));
116ExpandedSubBlockEnd.gif                    }

117InBlock.gif                    //绑定数据
118InBlock.gif                    foreach(DataRow row in dt.Rows)
119ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
120InBlock.gif                        objControl.Items.Add(new ListItem(row[0].ToString().Trim(),row[1].ToString().Trim()));
121ExpandedSubBlockEnd.gif                    }

122ExpandedSubBlockEnd.gif                }

123ExpandedSubBlockEnd.gif            }

124InBlock.gif
125ContractedSubBlock.gifExpandedSubBlockStart.gif            绑定下拉控件#region 绑定下拉控件
126ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
127InBlock.gif            /// 绑定下拉控件
128InBlock.gif            /// </summary>
129InBlock.gif            /// <param name="ddl">下拉控件</param>
130InBlock.gif            /// <param name="defaultText">默认项Text</param>
131ExpandedSubBlockEnd.gif            /// <param name="defaultValue">默认项value</param>

132InBlock.gif            public void BindDDlControl(DropDownList ddl,string defaultText,string defaultValue)
133ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
134InBlock.gif                this.BindDDlControl(ddl,defaultText,defaultValue,true);
135ExpandedSubBlockEnd.gif            }

136InBlock.gif
137ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
138InBlock.gif            /// 绑定下拉控件
139InBlock.gif            /// </summary>
140ExpandedSubBlockEnd.gif            /// <param name="ddl">下拉控件</param>

141InBlock.gif            public void BindDDlControl(DropDownList ddl)
142ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
143InBlock.gif                this.BindDDlControl(ddl,"","",false);
144ExpandedSubBlockEnd.gif            }

145ExpandedSubBlockEnd.gif            #endregion

146InBlock.gif
147ContractedSubBlock.gifExpandedSubBlockStart.gif            绑定多选框列表#region 绑定多选框列表
148InBlock.gif            public void BindChkControl(CheckBoxList chk)
149ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
150InBlock.gif                this.BindDDlControl(chk,"","",false);
151ExpandedSubBlockEnd.gif            }

152ExpandedSubBlockEnd.gif            #endregion

153ExpandedBlockEnd.gif        }

154 None.gif
155 ExpandedBlockStart.gifContractedBlock.gif         /**/ /// <summary>
156InBlock.gif        /// 性别
157ExpandedBlockEnd.gif        /// </summary>

158 ExpandedBlockStart.gif ContractedBlock.gif          public   static  TypeCollection tcOrderType = new  TypeCollection( new   string [,] dot.gif {dot.gif{"","1"},dot.gif{"","0"}} );

 

 这个集合类可以方便实现
1,文本--值类型的定义
2,根据值查找文本
3,向控件绑定更方便。
尤其是定义大量的类型时。

总结:1,可以通过自己对.net类库的灵活使用 ,实现更多的功能。
2,多态的应用。对于多选框和下拉框,都是ListControl控件。参数声明直接声明为基类型。
3, 对面向对象的深入理解。具有相同行为和属性的一组对象,可以抽象为类。定义统一的方法和属性。比如本例中的Type和TypeCollection类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值