以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Dot NET,C#,ASP,VB 』  (http://bbs.xml.org.cn/list.asp?boardid=43)
----  我写的一个关于使用C# Indexer的例子  (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=11748)


--  作者:admin
--  发布时间:11/9/2004 2:25:00 AM

--  我写的一个关于使用C# Indexer的例子


发信人: Jobs (温少), 信区: DotNET        
标  题: 我写的一个关于使用C# Indexer的例子
发信站: BBS 水木清华站 (Sat May 12 01:34:34 2001)


   Indexer是C#中新增加的,可能有些朋友会有些困惑,最近做的一些事情
经常使用Index,顺手写一个简单的例子给大家看看,如果有什么问题可以问!

这个例子包含三个类:DataRow、DataItem、DataItemCollection。
其中DataItemCollection中使用了Indexer。

using System;
using System.Collections;
public static void Main(string [] args)
{
        DataRow myDataRow = new DataRow();
        DataItem myDataItem = new DataItem("Text","Value");
        DataRow.DataItems.Add(myDataItem);
        Console.WriteLine(myDataRow.DataItems[0].Text);
}
        public class DataRow
        {
                public DataItemCollection DataItems;
                public DataRow()
                {
                        DataItems = new DataItemCollection();
                }
        }

    public class DataItem
    {
                private string _text;
                private string _value;

        public DataItem(string text, string value)
        {
                        _text=text;
                        _value=value;
        }

                public string Text
                {
                        get
                        {
                                return(_text);
                        }
                        set
                        {
                                _text=value;
                        }
                }

                public string Value
                {
                        get
                        {
                                return(_value);  
                        }
                        set
                        {
                                _value=value;
                        }
                }

        }

        public class DataItemCollection
        {
                private ArrayList _array = new ArrayList();
         
                public virtual int Count
                {
                        get
                        {
                                return(_array.Count);
                        }
                }

                public DataItem this[int index] //此处使用了Indexer
                {
                        get
                        {        
                                if(index>=0 && index<_array.Count)
                                {
                                        return((DataItem)_array[index]);
                                }
                                else
                                {
                                        throw new Exception("index overflow");
                                }
                        }

                        set
                        {
                                if(index>=0 && index<_array.Count)
                                {
                                        _array[index]=value;
                                }
                                else
                                {
                                        throw new Exception("index overflow");
                                }
                        }
                }

                public void Add(DataItem item)
                {
                        _array.Add(item);
                }

                public void Remove(DataItem item)
                {
                        _array.Remove(item);
                }

                public void RemovAt(int i)
                {
                        _array.RemoveAt(i);
                }
        }
--

※ 修改:·Jobs 於 May 12 01:34:55 修改本文·[FROM:    210.39.3.110]
※ 修改:·Jobs 於 May 12 01:41:57 修改本文·[FROM:    210.39.3.110]
※ 来源:·BBS 水木清华站 smth.org·[FROM: 210.39.3.110]
上一篇
返回上一页
回到目录
回到页首
下一篇



W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms