I have a 'Invoice' WinForm C# where it contains the usual textboxes and an unbound datagridview. I used a 'Invoice' class object to store the form fields' values. The datagridview rows are stored to a List < SubInvoice> properties in the 'Invoice'.
public static bool CompareObjects(object original, object altered)
{
Type o = original.GetType();
Type a = altered.GetType();
foreach (PropertyInfo p in o.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
Console.WriteLine("Original: {0} = {1}", p.Name, p.GetValue(original, null));
Console.WriteLine("Altered: {0} = {1}", p.Name, p.GetValue(altered, null));
if (p.GetValue(original, null).ToString() != p.GetValue(altered, null).ToString())
{
//Console.WriteLine("Not Equal");
return false;
}
}
return true;
}
I am using a global class with a method that uses Reflection to loop through the original 'invoice' object and compared it with the altered 'invoice'. If there is any difference, the user will be notified to save the invoice.
The above method works fine with all the properties of 'Invoice' class but I do not know how to check for the List in which it stores the value of each datagridrow into a 'SubInvoice' class object that stored it as an object in the list.
Can someone please give me some help here? I have also checked for similar thread in stackoverflow and other forums but in vain.
Update: I need to create a global generic method that will check for all type of classes. It can be 'Invoice', 'Customer'. The purpose is to track any changes made to the form at any particular instance and prompt the user to save.
oandaare different types, unlessahappens to have all the same properties thatohas. It will also not work as you intend if any of the properties are classes or structs, unless they happen to have overloaded ToString() to return a value that can be used as an equality comparison. Finally it will fail to detect a possible difference ifahas properties thatodoes not have.oandato be the same type (still does not solve the issue of non-primitive properties in that generalized code.