Diskuze: porovnání dvou objektů
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Zobrazeno 3 zpráv z 3.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Skus pouzit genericke rozhranie IComparable.
public class sp_GetDeviceDetail_Result : IComparable<sp_GetDeviceDetail_Result>
{
public int id { get; set; }
public string inventory_number { get; set; }
public string name { get; set; }
public string model { get; set; }
public string serial_number { get; set; }
public string product_number { get; set; }
public Nullable<double> price_purchase { get; set; }
public string currency { get; set; }
public string document_number { get; set; }
public Nullable<System.DateTime> date_purchase { get; set; }
public Nullable<System.DateTime> date_elimination { get; set; }
public string location { get; set; }
public string note { get; set; }
public int CompareTo(sp_GetDeviceDetail_Result other)
{
if (id != other.id || inventory_number != other.inventory_number)
{
return 1;
}
else
{
return 0;
}
}
}
ukazka ako pracuje :
static void Main(string[] args)
{
sp_GetDeviceDetail_Result result1 = new sp_GetDeviceDetail_Result(); // obj from db
sp_GetDeviceDetail_Result result2 = new sp_GetDeviceDetail_Result(); // obj from page
//result2.id = 25;
int changed = result1.CompareTo(result2);
if (changed == 1)
{
Console.WriteLine("something changed");
}
else
{
Console.WriteLine("object are equale");
}
Console.Read();
}
Alebo pouzi genericke rozhranie IEquatable. Priklad najdes tu
http://stackoverflow.com/questions/10454519/best-way-to-compare-two-complex-object
Zobrazeno 3 zpráv z 3.