當cmb事件觸發時, 根據cmb.SelectedValue做為HashTable的Key
並取出對應Key的Value值...
而Value為一個類別(cls_marking)...
C#:
[cls_marking.cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExportSys
{
class cls_marking
{
private bool _disposed = false;
private String _mk1;
private String _mk2;
private String _mk3;
private String _mk4;
private String _mk5;
private String _mk6;
private String _mk7;
private String _mk8;
private String _mk9;
//建構子
//public cls_marking(String g,String b);
public cls_marking(String mk1, String mk2,
String mk3, String mk4,
String mk5, String mk6,
String mk7, String mk8, String mk9)
{
this._mk1 = mk1;
this._mk2 = mk2;
this._mk3 = mk3;
this._mk4 = mk4;
this._mk5 = mk5;
this._mk6 = mk6;
this._mk7 = mk7;
this._mk8 = mk8;
this._mk9 = mk9;
this._disposed = false;
}
public override string ToString()
{
return _mk1 + " " + _mk2 + " " + _mk3 + " " + _mk4 + " " + _mk5 + " " + _mk6 + " " + _mk7 + " " + _mk8 + " " + _mk9;
}
public string mk1
{
get { return _mk1; }
set { _mk1 = value; }
}
public string mk2
{
get { return _mk2; }
set { _mk2 = value; }
}
public string mk3
{
get { return _mk3; }
set { _mk3 = value; }
}
public string mk4
{
get { return _mk4; }
set { _mk4 = value; }
}
public string mk5
{
get { return _mk5; }
set { _mk5 = value; }
}
public string mk6
{
get { return _mk6; }
set { _mk6 = value; }
}
public string mk7
{
get { return _mk7; }
set { _mk7 = value; }
}
public string mk8
{
get { return _mk8; }
set { _mk8 = value; }
}
public string mk9
{
get { return _mk9; }
set { _mk9 = value; }
}
public void Dispose()
{
Dispose(true);
// Use SupressFinalize in case a subclass
// of this type implements a finalizer.
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
// If you need thread safety, use a lock around these
// operations, as well as in your methods that use the resource.
if (!_disposed)
{
if (disposing)
{
if (_mk1 != null)
{
_mk1 = null;
_mk2 = null;
_mk3 = null;
_mk4 = null;
_mk5 = null;
_mk6 = null;
_mk7 = null;
_mk8 = null;
_mk9 = null;
}
Console.WriteLine("Object disposed.");
}
// Indicate that the instance has been disposed.
_mk1 = null;
_mk2 = null;
_mk3 = null;
_mk4 = null;
_mk5 = null;
_mk6 = null;
_mk7 = null;
_mk8 = null;
_mk9 = null;
_disposed = true;
}
}
}
}
[ExpOrder.cs]
private Hashtable htb_mark = new Hashtable();
private void cmdBing()
{
cmb_印字編號.DisplayMember = "Value";
cmb_印字編號.ValueMember = "Key";
ArrayList al = new ArrayList();
using (IDataReader reader = db.ExecuteReader(db.GetSqlStringCommand("SELECT MARK代碼 + '(' + 中文名稱 + ')' as 中文名稱,MARK代碼,MARK1,MARK2,MARK3,MARK4,MARK5,MARK6,MARK7,MARK8,MARK9 FROM [tbl資料表];")))
{
if (htb_mark.Count > 0) htb_mark.Clear();
while (reader.Read())
{
al.Add(new DictionaryEntry(reader.GetValue(1), reader.GetValue(0)));
cls_marking mark = new cls_marking(reader["MARK1"].ToString(), reader["MARK2"].ToString(),
reader["MARK3"].ToString(), reader["MARK4"].ToString(), reader["MARK5"].ToString(),
reader["MARK6"].ToString(), reader["MARK7"].ToString(), reader["MARK8"].ToString(),
reader["MARK9"].ToString());
htb_mark.Add(reader["MARK代碼"].ToString(), mark);
}
cmb_印字編號.DataSource = al;
}
}
private void cmb_印字編號_SelectedIndexChanged(object sender, EventArgs e)
{
this.txt_印字1.Text = ((cls_marking)htb_mark[cmb_印字編號.SelectedValue]).mk1.ToString();
this.txt_印字2.Text = ((cls_marking)htb_mark[cmb_印字編號.SelectedValue]).mk2.ToString();
this.txt_印字3.Text = ((cls_marking)htb_mark[cmb_印字編號.SelectedValue]).mk3.ToString();
this.txt_印字4.Text = ((cls_marking)htb_mark[cmb_印字編號.SelectedValue]).mk4.ToString();
this.txt_印字5.Text = ((cls_marking)htb_mark[cmb_印字編號.SelectedValue]).mk5.ToString();
this.txt_印字6.Text = ((cls_marking)htb_mark[cmb_印字編號.SelectedValue]).mk6.ToString();
this.txt_印字7.Text = ((cls_marking)htb_mark[cmb_印字編號.SelectedValue]).mk7.ToString();
this.txt_印字8.Text = ((cls_marking)htb_mark[cmb_印字編號.SelectedValue]).mk8.ToString();
this.txt_印字9.Text = ((cls_marking)htb_mark[cmb_印字編號.SelectedValue]).mk9.ToString();
}
private void btn_Test(object sender, EventArgs e)
{
//法一
foreach(DictionaryEntry item in htb_mark)
{
MessageBox.Show(item.Key + " = " + ((cls_marking)item.Value).mk1);
}
//法二
foreach(cls_marking item in htb_mark.Values)
{
MessageBox.Show(item.mk1.ToString());
}
//法三
//variable to hold the list of songs
string list = string.Empty;
//create an instance of the IDictionaryEnumerator Interface
IDictionaryEnumerator enumerator;
//make sure our Hashtable holds items
if (htb_mark.Count > 0)
{
//let the user know what we're doing
MessageBox.Show("The following songs are in your list:");
//now set out IDictionaryEnumerator value
enumerator = htb_mark.GetEnumerator();
//now use the MoveNext Method to iterate through our list
while (enumerator.MoveNext())
{
//keep adding song names until we reach the end
list += enumerator.Value.ToString() + "\r\n";
}
//now show the song names
MessageBox.Show(list);
}
}
缺點:
C# - 替代 Hashtable 沒有泛型的方案
雖然Hashtable在某些地方很好用,但在某些情況下,反而成了礙手礙腳的。
Hashtable裡面的物件都是存成object,倘若要不斷的使用物件,就要不斷的轉型…
一來折損不少效能,二來程式碼看起來就很不舒服
其實,解決方法有很多種,只是找不到有詳盡介紹 C# 所有Collections的好文章。
最根本的做法就是直接去實作Collections使用的介面,但是又整個很麻煩的感覺。
一直在想,都有List<>的泛型了,為啥沒有對應Hashtable的泛型咧~
今天終於給我咕到一個好用的Collections可以取代之!
參考:
DictionaryEntry 結構
C#集合--Dictionary
Hashtable 類別
C#中HashTable的用法
C#中如何操作HashTable类呢?
Hashtable中key, value键值对均为object类型,所以Hashtable可以支持任何类型的keyvalue键值对.
C#集合之Hashtable
C# 應用雜湊表(Hashtable)
什麼是 Dictionaries
Using The C# Hashtable Collection
C# Hashtable
C#: Storing Instance of Objects in (Hashtable)
How to Serialize a Dictionary or Hashtable in C#
使用泛型類別解決 ComboBox 的自訂資料繫結問題
集合與泛型對應
c#中Dictionary、ArrayList、Hashtable和数组 Array 的区别
Array和ArrayList的异同点
沒有留言:
張貼留言