public class sort<T>
{
private string m_colName;//<T>排序属性名
public sort(string colName)
{
m_colName = colName;
}
//用来给List<T>排序
public static int CompareCols(T x, T y)
{
if (x == null)
{
if (y == null)
{
return 0;
}
else
{
return -1;
}
}
else
{
if (y == null)
{
return 1;
}
else
{
return x.m_colName.CompareTo(y.m_colName);//这句话出了问题错误 17 “T”并不包含“m_colName”的定义 }
}
}
}有什么好的方法吗??
{
private string m_colName;//<T>排序属性名
public sort(string colName)
{
m_colName = colName;
}
//用来给List<T>排序
public static int CompareCols(T x, T y)
{
if (x == null)
{
if (y == null)
{
return 0;
}
else
{
return -1;
}
}
else
{
if (y == null)
{
return 1;
}
else
{
return x.m_colName.CompareTo(y.m_colName);//这句话出了问题错误 17 “T”并不包含“m_colName”的定义 }
}
}
}有什么好的方法吗??
解决方案 »
- SQLite数据库中删除数据后,文件大小没有变化
- ——————————删除mdb文件问题,进来就有分————————[问题点数:100分,结帖
- XML序列化与反序列化
- DataGridView前幾列數據如果有改動則改變后兩列的數據。
- 哪位大侠能指出下面的正则表达式应该怎么写才是正确的?
- XmlDocument等对象如何销毁?
- 如何保存不同字符串产生的哈稀码是唯一的
- 一个在winform用户控件中进行对webservice安全验证的问题,紧急!!!!
- Win Form 的TreeView 展开节点问题:
- .Net 前后台交互问题
- vs2008中文版什么时候发布
- c#读取硬盘物理序列号 在线等待........
{
private T m_colName;//<T>排序属性名
public sort(T colName)
{
m_colName = colName;
}
public class sort<T>
{
private T m_colName;//<T>排序属性名
public sort(T colName)
{
m_colName = colName;
}
-----------------------------------------------------
正解
string col_name { get; set;}
}
class myc<T> where T:ia
{
void youmethod( T x )
{
x.col_name = ...
}
}
return x.m_colName.CompareTo(y.m_colName);//这句话出了问题错误 17 “T”并不包含“m_colName”的定义
这里T的类型未知,所以出错。排序的关键在于比较,所以要求被排序的类要实现IComparable接口如:class Racer:IComparable
{
private string _name; public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age; public int Age
{
get { return _age; }
set { _age = value; }
}
public Racer(string name,int age)
{
this._name = name;
this._age = age;
} #region IComparable 成员 public int CompareTo(object obj)
{
Racer temp = (Racer)obj; return this._name.CompareTo(temp._name);
} #endregion
}
排序时用:
List<Racer> racers = new List<Racer>(); racers.Add(new Racer("aaaa",12));
racers.Add(new Racer("ddddd",15));
racers.Add(new Racer("bbb",3)); racers.Sort(); dataGridView1.DataSource = racers;http://bbs.gogolike.com/showtopic-476.aspx
interface ia {
string col_name { get; set;}
}
col_name是确定的吗,我要实现的是动态的
{
m_colName = colName;
}
根据传入的属性名对List<T>排序
public interface IMyClass
{
m_colName
{
get;
set;
}
}public class sort<T>
{
public static int CompareCols(T x, T y)
where T :IMyClass
{
// 你的方法
}
}// 可传入的类
public class MyClass : IMyClass
{
private string m_colname;
public string m_colName
{
get {return m_colname;}
set {m_colname=value;}
}
}
MyClass是动态的,排序属性也是动态的
我要根据MyClass不同属性排序,按照你这种做法,我只能根据m_colName对MyClass进行排序了
属性名称是通过构造函数public sort(string colName)传入的
问题是return x.m_colName.CompareTo(y.m_colName);//这句话出了问题错误17“T”并不包含“m_colName”的定义
private void InitTreeView()
{
for (int i=0;i<GetBigType ().Rows .Count ;i ++)
{
DataTable dt=GetBigType();
TreeNode node = new TreeNode();
node.Expanded = false;//设置节点是否展开,取反
//look this code run
node.Text =dt.rows[i]["bigname"].ToString ();
node.Value = dt.Rows[i]["TradeBigTypeid"].ToString();
this.MyTreeView.Nodes.Add(node);
}
}这是一级绑定到节点里.
//一级节点
public DataTable GetBigType()
{
using(SqlConnection con=new SqlConnection (MCommon.ConnectString ))
{
con.Open ();
string sql="select * from TradeBigType where status=1";
SqlCommand cmd=new SqlCommand (sql,con);
SqlDataAdapter ada=new SqlDataAdapter (cmd);
DataTable dt=new DataTable ();
ada.Fill (dt);
return dt;
}
}
//二级节点 public DataTable GetMidType(string bigname)
{
using (SqlConnection con = new SqlConnection(MCommon.ConnectString))
{
con.Open();
string sql = "select * from trademidtype where bigtradeid=(select tradebigtypeid from tradebigtype where bigname='" + bigname + "')";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ada.Fill(dt);
return dt;
}
} //三级节点
public DataTable GetSmallType(string bigname,string midname)
{
using (SqlConnection con = new SqlConnection(MCommon.ConnectString))
{
con.Open();
string sql = "select * from TradeSmallType where bigid=(select tradebigtypeid from tradebigtype where bigname='" + bigname + "') and midid=(select trademidtypeid from trademidtype where midname='" + midname + "')";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ada.Fill(dt);
return dt;
}
}这是我用到的得到数据的方法 都要传参数的,但是我不知道怎么把他们加到树里面的二级三级里去了各位路过的高手,能不能就这我这几个方法把二级 三级绑到树里去,小女子在这里先谢谢各位了
List<T>.Sort()没有public static int CompareCols(sort<T> x, sort<T> y)这个重载的方法
public class sort<T>
{
private string m_colName;//<T>排序属性名
public sort(string colName)
{
m_colName = colName;
}
//用来给List<T>排序
public static int CompareCols(sort<T> x, sort<T> y)这个重载的方法{
if (x == null)
{
if (y == null)
{
return 0;
}
else
{
return -1;
}
}
else
{
if (y == null)
{
return 1;
}
else
{
return x.m_colName.CompareTo(y.m_colName);//这句话出了问题错误17“T”并不包含“m_colName”的定义}
}
}
}
public interface IPropertyComparable{
IComparable GetComparableProperty(string propertyName);
}
根据给定的属性名返回可以比较的值,可以不用反射。
MyList.Sort(sort<Model.CHChance>("Name").CompareCols);//根据属性名Name给MyList排序
能不能详细一点?给点代码说明
public class sort<T>为什么不是继承IComparer<T>的呢?
public class sort<T>:IComparer<T>?然后就应该是实现接口
public int Compare(T x, T y)才对吧?然后你的m_colName是谁的私有变量呢?T的?还是sort类的?要是T的,为什么你定义在sort类下面呢?
要是sort的,x.m_colName又怎么理解呢?
{
public string m_colName;
}public class sort<T> where T : Test
{
.......
}
{
private string m_colName;//<T>排序属性名
public sort(string colName)
{
m_colName = colName;
}
//用来给List<T>排序
public static int CompareCols(T x, T y)
{
if (x == null)
{
if (y == null)
{
return 0;
}
else
{
return -1;
}
}
else
{
if (y == null)
{
return 1;
}
else
{
return x.GetComparableProperty("m_colName").CompareTo(y.GetComparableProperty("m_colName")); }
}
}
}public class CHChance:IPropertyComparable{
public IComparable GetComparableProperty(string propertyName){
switch(propertyName){
case "m_colName": return m_colName;
case "m_colName2":
...;
default: return (IComparable) this.GetType().GetProperty(propertyName).GetValue(this,null);
} private string m_col;
public string m_colName{
get{return m_col;}
set{ m_col = value;} }
}
修改GetComparableProperty方法,灵活性不是太好,有没有更好的idea
多谢大家!!!~~~
多谢 danjiewu(阿丹)强力支援