2014-05-20 33 views
-3

我有这个类我怎么能比较2个对象与IComparable的

{ 

public class Conexiune:ICloneable,IComparable<Conexiune> 
{ 
    [XmlElement("Name")] 
    private string Nume; 


    private string Server; 

    private string User; 

    private string Parola; 

    private string NumeBD; 

    public string NUME 
    { 
     get { return this.Nume; } 
     set { this.Nume = value; } 
    } 

    public string SERVER 
    { 
     get { return this.Server; } 
     set { this.Server = value; } 
    } 

    public string USER 
    { 
     get { return this.User; } 
     set { this.User = value; } 
    } 

    public string PAROLA 
    { 
     get { return this.Parola; } 
     set { this.Parola = value; } 
    } 

    public string NUMEBD 
    { 
     get { return this.NumeBD; } 
     set { this.NumeBD = value; } 
    } 

    public override string ToString() 
    { 
     return Nume; 
    } 
    public Conexiune() 
    { 

    } 
    public Conexiune(string numec, string serverc, string userc, string parolac, string numebdc) 
    { 
     this.Nume = numec; 
     this.Server = serverc; 
     this.User = userc; 
     this.Parola = parolac; 
     this.NumeBD = numebdc; 
    } 

    public object CloneP() 
    { 
     Conexiune copieP = new Conexiune(this.NUME, this.SERVER, this.USER, this.PAROLA, this.NumeBD); 
     return copieP; 
    } 
    public object Clone() 
    { 
     Conexiune copie = new Conexiune(); 
     return copie; 
    } 

    //Here i want to compare 
    public int CompareTo(Conexiune other) 
    { 
     int result = 0; 
     return result; 
    } 
} 
} 

我想比较2 Conexiune(连接),但我不知道由什么来比较。比较,如果你想排序后上市的目标越高,必须返回一个int

+1

如果你不知道,我们应该怎么知道?你一定有一些想法,为什么你想比较他们和什么被认为是平等的?此外,你的“克隆”方法并不真正克隆,这是用意吗? – Silvermind

+0

我想通过NumeBD(数据库名称)比较它们, –

回答

0

返回负数正面的,如果你希望它低,0,如果对象是相等的

,我们不知道你是什么所需的顺序

1

在这种情况下,只需使用字符串比较的NumeBD领域:

public int CompareTo(Conexiune other) 
{ 
    return String.Compare(NUMEBD, other.NUMEBD, StringComparison.Ordinal); 
} 

您可能需要修改这个,如果NumeBD可以null到大概订购吧opriately