0

I have the following class:

public class CompareField<T>
{
    public CompareField() 
    {
        Status = FieldStatus.NoChange;
    }
    public CompareField(T value)
    {
        Value = value;
        Status = FieldStatus.NoChange;
    }

    public T Value { get; set; }
    public FieldStatus Status { get; set; }

    public bool Equals(T objectValue)
    {
        if (!Equals(Value, default(T)) || !Equals(objectValue, default(T))) return false;
        return Value.Equals(objectValue);
    }
}

public enum FieldStatus
{
    NoChange = 0,
    HasChanges = 1
}

So my model would contain a bunch of properties, that all use the CompareField type (like CompareField<'string> FirstName {get;set;}...

Now I want to compare two models to eachother and set the FieldStatus of each field, based on the fact that the fields are equal or different.

I've tried using the solution from this post, but it only touches on the comparison part...I can't actually set any properties using that method.

Also, what should I do if T is more complex than just a simple type? Let's say T is a List of classes, what should I use then?

I'm unsure how to implement this (and just using reflection would probably be too slow)...

Any tips would be helpful :)

EDIT So an example would be


ModelA

Firstname : Bob

Lastname : Test


ModelB

Firstname : Bobby

Lastname : Test


So I'd like "something" to compare ModelA to ModelB and get the following result


ModelA

Firstname : Bob - HasChanges

Lastname : Test - NoChanges

so by hand it would be something like

if (!modelA.FirstName.Value.Equals(modelB.FirstName.Value)) 
    modelA.FirstName.Status = FieldStatus.HasChanges
Community
  • 1
  • 1
Team-JoKi
  • 1,766
  • 3
  • 15
  • 23
  • 1
    what property is it you want to set? If it weren't for the generics and reflection, how would you code it by hand, so we can see what you mean? Also: `EqualityComparer.Default.Equals(...))` would be a **much** better choice than `Equals(x,y)`, especially if `T` could be a value-type (`int` etc). Comparing lists is **much** more complicated, as you have problems like: same logical values in a different order; item 2 and 3 are removed; how to match that 1==1,2==4,3==5,etc - frankly, that is *really* hard to get right – Marc Gravell Jul 15 '14 at 07:07
  • 1
    for comparing list [Enumerable.SequenceEqual](http://msdn.microsoft.com/ru-ru/library/bb348567(v=vs.110).aspx) – Grundy Jul 15 '14 at 07:32
  • @Grundy Thanks, that's going to come in handy! – Team-JoKi Jul 15 '14 at 07:48
  • @MarcGravell I added an example – Team-JoKi Jul 15 '14 at 08:39
  • @Team-JoKi you can implement [IEquatable](http://msdn.microsoft.com/ru-ru/library/ms131187(v=vs.110).aspx) interface for your custom classes and simple call `Equals` – Grundy Jul 16 '14 at 10:50

0 Answers0