0
我想通過Genericx在我的程序中實現堆棧。我有一個文本框和按鈕來添加堆棧中的元素,一個下拉列表框和一個用於在下拉列表框中綁定總堆棧的按鈕。在C#中的泛型下拉列表框中的數據綁定問題#
我有通用類和代碼如下:
[Serializable]
public class myGenClass<T>
{
private T[] _elements;
private int _pointer;
public myGenClass(int size)
{
_elements = new T[size];
_pointer = 0;
}
public void Push(T item)
{
if (_pointer > _elements.Length - 1)
{
throw new Exception("Stack is full");
}
_elements[_pointer] = item;
_pointer++;
}
public T Pop()
{
_pointer--;
if (_pointer < 0)
{
throw new Exception("Stack is empty");
}
return _elements[_pointer];
}
public T[] myBind()
{
T[] showall = new T[_pointer];
Array.Copy(_elements,showall, _pointer);
T[] newarray = showall;
Array.Reverse(showall);
return showall;
}
}
和我的.cs頁面如下:
public partial class _Default : System.Web.UI.Page
{
myGenClass<int> mystack = new myGenClass<int>(25);
protected void Button1_Click(object sender, EventArgs e)
{
mystack.Push(Int32.Parse(TextBox1.Text));
//DropDownList1.Items.Add(mystack.Pop().ToString());
TextBox1.Text = string.Empty;
TextBox1.Focus();
}
protected void Button2_Click(object sender, EventArgs e)
{
//string[] db;
//db = Array.ConvertAll<int, string>(mystack.myBind(), Convert.ToString);
DropDownList1.DataSource = mystack.myBind();
DropDownList1.DataBind();
}
}
但是當我下拉列表框中的DataSource屬性綁定到泛型類型回報數組(即myBind()),它顯示爲空...請幫助..
是的我堅持泛型類的實例。 – user299827 2010-03-23 13:52:14