KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Synopsis: I have a need to take two generic C# objects, and if they are numerical, compare them using either less than or greater than comparisons. Problem : I can't figure out how to have my class implement IComparable as described in this post: Having to implement a generic less than and greater than operation . If that's not even the proper path to take, then I need to know that as well. Background: I have implemented the RequiredIf ValidationAttribute found at A more complex custom validator but needed the > and < options in addition to an equals comparison. Code (taken from A more complex custom validator , one-third down the page): private bool IsRequired(object actualPropertyValue) { switch (Comparison) { case Comparison.IsLessThan: case Comparison.IsLessThanOrEqualTo: case Comparison.IsGreaterThan: case Comparison.IsGreaterThanOrEqualTo: if (!Value.IsNumber()) { throw new Exception("The selected comparison option is only applicable to numeric values"); } break; } switch (Comparison) { case Comparison.IsNotEqualTo: return actualPropertyValue == null || !actualPropertyValue.Equals(Value); case Comparison.IsEqualTo: return actualPropertyValue != null && actualPropertyValue.Equals(Value); case Comparison.IsGreaterThan: // THIS LINE FAILS BECAUSE actualPropertyValue DOESN'T IMPLEMENT IComparable return actualPropertyValue != null && (actualPropertyValue.CompareTo(Value) > 0); /
Tags (comma-separated)
Save Edits
Cancel