Hashtable 在程序中控制重复项

在一个索引器类中集合类中,为控制add方法,中不加入重复项(ID)。使用了Hashtable。
private  Hashtable iDHash=new Hashtable();

ContractedBlock.gif ExpandedBlockStart.gif /***添加item***/ #region/***添加item***/
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 添加item类
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <param name="newItem">新Item类</param>

InBlock.gifpublic void Add(Item newItem)     
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{  
InBlock.gif
int id=newItem.B.ID;       //(B为newItem 的一个属性类)
InBlock.gif
if(this.iDHash.Contains(id))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.iDHash.Add(id,"");
InBlock.gifList.Add(newItem);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif
#endregion

ContractedBlock.gif ExpandedBlockStart.gif   /***移除Item***/ #region/***移除Item***/
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 移除Item类
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <param name="oldItem">待移除的Item类</param>

InBlock.gif  public void Remove(Item oldItem)//移除类对象重载函数 
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif{  
InBlock.gif   
if(oldItem!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
int id=oldItem.B.ID;
InBlock.gif    
this.iDHash.Remove(id);
InBlock.gif    List.Remove(oldItem); 
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif  
#endregion

ContractedBlock.gif ExpandedBlockStart.gif   /***移除Item***/ #region/***移除Item***/
InBlock.gif  
//  /// <summary>
InBlock.gif  
//  /// 移除Item类
InBlock.gif  
//  /// </summary>
InBlock.gif  
//  /// <param name="index">待移除的index</param>
InBlock.gif
  public void Remove(int index)              //移除类对象重载函数 
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif
InBlock.gif   
// 如果Item不存在,显示提示 messagebox 
InBlock.gif
  
InBlock.gif   
if (index > Count - 1 || index < 0)
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Item oldItem
=(Item)this[index]; 
InBlock.gif    
int id=oldItem.B.ID;
InBlock.gif    
this.iDHash.Remove(id);
InBlock.gif
InBlock.gif     List.RemoveAt(index); 
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif  
#endregion

 

转载于:https://www.cnblogs.com/flashicp/archive/2007/04/16/714953.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值