CSharp中索引器的简单使用
2014-01-01 09:33
1001 查看
索引器(indexer)是一种特殊的类方法 ,允许使用一个看起来像获取数组元素一样的方法来访问类的内部数据 。下面使用BitList类来演示 indexer的简单用法 。
在BitList类中 ,索引器返回 number 域 第 i 个比特位(bit)的值 。
设计一个简单的窗体 :
TextBox txtNumber;//用来输入number
NumbericUpDown numUD;//获取index
ListBox lsBits;//显示number 域 第 index 个比特位的值
运行效果:
在BitList类中 ,索引器返回 number 域 第 i 个比特位(bit)的值 。
TextBox txtNumber;//用来输入number
NumbericUpDown numUD;//获取index
ListBox lsBits;//显示number 域 第 index 个比特位的值
public partial class FrmBitList : Form { public FrmBitList() { InitializeComponent(); } private void numUD_ValueChanged(object sender, EventArgs e) { if (this.txtNumber.Text.Length < 1) { return; } //从调节钮控件中获取索引值 int index =(int)this.numUD.Value; if (index == -1) { return; } //通过BitList的索引器获取bit值 int bit = BitList.GetInstance()[index]; lsBits.Items.Add(bit.ToString()); } private void txtNumber_KeyPress(object sender, KeyPressEventArgs e) { //设置KeyPress事件已经处理过 e.Handled = true; //只能输入数字 和 BackSpace if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '\b') { e.Handled = false; } } private void txtNumber_TextChanged(object sender, EventArgs e) { //更新BitList中的属性Number if (txtNumber.Text.Length < 1) { BitList.GetInstance().Number = 0; } else { BitList.GetInstance().Number = Convert.ToInt32(txtNumber .Text); } this.numUD.Value = -1;//复位 this.lsBits.Items.Clear();//清空 } }
运行效果:
相关文章推荐
- DataGrid在VS2005 CSharp中的简单使用
- C#中索引器简单使用
- JSP内置对象:Request和Response的简单介绍及使用
- PostgreSQL 安装和简单使用第1/2页
- JSTL标签使用简单列举
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
- Linux libusb 安装及简单使用
- 安卓中SQLite的最简单使用(—)
- java中CyclicBarrier简单入门使用
- 简单使用Eclipse编写第一个Java程序
- dispatch_soruce_t的简单使用事例
- android EventBus 3.0 简单使用
- Toolbar的简单使用
- checkedListBox控件的使用简单示例
- 初学JavaScript_03(ExtJs Grid的简单使用)
- 如何使用TCP/IP与服务器进行通信-一个简单的聊天程序
- Python 使用单链表实现简单的稀疏矩阵
- vim编辑器简单使用(3)
- java环境搭建系列:JDK从下载安装到简单使用
- ListView的简单使用