public void DropAddValue(DropDownList thisDrop, string drpTable, string drpColumn){ }
该函数是public class DRPItemValue中的一个函数,我想把DropDownList thisDrop用泛型方式重写,该泛型主要包括DropDownList和ListBox。
请问:该该如何定义class DRPItemValue??
谢谢各位!!
该函数是public class DRPItemValue中的一个函数,我想把DropDownList thisDrop用泛型方式重写,该泛型主要包括DropDownList和ListBox。
请问:该该如何定义class DRPItemValue??
谢谢各位!!
public class DRPItemValue<T>
{ public void DropAddValue(T collectControl, string drpTable, string drpColumn)
{
//:TODO
}
}
//第二种方法
public class DRPItemValue1
{ public void DropAddValue<T>(T collectControl, string drpTable, string drpColumn)
{
//:TODO
}
}
//两种方法的实例化
DRPItemValue<DropDownList> d = new DRPItemValue<DropDownList>();
d.DropAddValue(1, "table1", "column");
DRPItemValue1 d1 = new DRPItemValue1();
d1.DropAddValue<DropDownList>(1, "22", "33");
{ public void DropAddValue(T collectControl, string drpTable, string drpColumn)
{
//:TODO
}
}
//第二种方法
public class DRPItemValue1
{ public void DropAddValue<T>(T collectControl, string drpTable, string drpColumn)
{
//:TODO
}
}至于这两种方法,肯定选第二种。
并且,可以这样写:DropAddValue( dropDownList, "table", "column" );
谢谢各位!!
DropAddValue( ListControl collectControl, string drpTable, string drpColumn)在你这个例子里,根本用不着泛型……
已经照你的方法作了,谢谢!!
如果某个类或者方法需要处理泛型类型的东西(如IList<T>),并且对于方法而言,有两个或两个以上的参数或返回值与泛型类型有关,对于类而言,有两个或两个以上的方法与泛型类型有关,就可以用泛型。例如,我们假设写一个方法,把一个List<T>里面指定序号的元素从列表中删除并构建一个新的列表,这样的方法就可以用泛型:List<T> RemoveAt<T>( IList<T> list, int index )
{
List<T> _list = new List<T>(); for ( int i = 0; i < list.Count; i++ )
if ( i != index )
_list.Add( list[i] );
return _list;
}
当然,泛型的适用场景还有很多,这是一个典型的非容器类泛型。