Prev: Are there methods with parameters of interface type in the standardlibrary?
Next: How to collapse C# XML doc comments like in VB.NET
From: Craig Lister on 1 Jul 2010 04:38 I have a class with a property, called Value, which contains different types of data. That is, it might hold a Float, an Int, a String... The property value depends on another property which defines they type. I also have an 'OriginalValue' object. That never changes. It contains the origibal value when the class loaded. I was declaring the property object... and with that, I could assign any value, and it works fine... myProperty = "Hello", myProperty = 1.1 etc etc... that all worked. I can set and get. BUT... I can't compare... That is, I can't say bool Modified = (Value != OriginalValue). That always returns true, because what I THINK it's saying is... is Value not exactly the same object as OriginalValue. Which, it never will be, as they are separate objects, but have different values. It's not comparing the value... How should I be doing this? /// <summary> /// The value of the parameter. This is updated when the user edits it. /// </summary> private object _value; public object Value { get { return _value; } set { _value = value; } } /// <summary> /// The original value as it was when read in from the firmware. /// </summary> private object _originalValue; public object OriginalValue { get { return _originalValue; } set { _originalValue = value; } }
From: Peter Duniho on 1 Jul 2010 13:07
Craig Lister wrote: > I have a class with a property, called Value, which contains different > types of data. That is, it might hold a Float, an Int, a String... The > property value depends on another property which defines they type. > > I also have an 'OriginalValue' object. That never changes. It contains > the origibal value when the class loaded. > > I was declaring the property object... and with that, I could assign > any value, and it works fine... myProperty = "Hello", myProperty = 1.1 > etc etc... that all worked. I can set and get. > > BUT... I can't compare... > > That is, I can't say bool Modified = (Value != OriginalValue). That > always returns true, because what I THINK it's saying is... is Value > not exactly the same object as OriginalValue. Which, it never will be, > as they are separate objects, but have different values. It's not > comparing the value... > > How should I be doing this? Hard to say without more context. I mean, you didn't even post a complete class example, never mind offer any information about the nature of these "generic" values (an ironic choice of terms, given there's nothing in the code example that actually uses C# generics :) ). But it seems to me that you've got a few different options: class A { private object _value; private object _originalValue; private bool _fModified; public A(object value) { _value = _originalValue = value; } public object Value { get { return _value; } set { _fModified = true; _value = value; } } public bool Modified { get { return _fModified; } } public object OriginalValue { get { return _originalValue; } } } or� class A { private object _value; private object _originalValue; private bool _fModified; public A(object value) { _value = _originalValue = value; } public object Value { get { return _value; } set { if (Comparer.Default.Compare(_value, value) != 0) { _fModified = true; _value = value; } } } public bool Modified { get { return _fModified; } } public object OriginalValue { get { return _originalValue; } } } or (actually using generics)� class A<T> where T : IEquatable<T> { private T _value; private T _originalValue; private bool _fModified; public A(T value) { _value = _originalValue = value; } public T Value { get { return _value; } set { if (_value.Equals(value) != 0) { _fModified = true; _value = value; } } } public bool Modified { get { return _fModified; } } public T OriginalValue { get { return _originalValue; } } } Notes: � Note that in none of the examples is it possible to modify OriginalValue after the object is created. Seems to me that it would be very poor design to allow that. � In the generic approach, you could use the IComparable<T> interface instead. Either should work fine, but of course if you're storing values having types that don't implement either interface, then neither will work. Of course, if you're doing that, it's not like you'll have any reliable way of comparing the two values anyway. � These aren't your only options. They just seem like the most natural to use. Hope that helps. Pete |