Note that there are some explanatory texts on larger screens.

plurals
  1. POStruggling to come up with a generic C# method that compares different types of numeric objects
    primarykey
    data
    text
    <p>I have an Infragistics UltraNumericEditor (version 5.3, quite old) control on a form.</p> <p>If I set the .Value field to something less than .MinValue or something more than .MaxValue then I get a System.Exception thrown with the following message: </p> <blockquote> <p>The 'Value' property cannot be set to a value that is outside the range determined by the 'MinValue' and 'MaxValue' properties</p> </blockquote> <p>The signatures of the relevant fields on UltraNumericEditor are as follows:</p> <pre><code>public object MinValue { get; set; } public object MaxValue { get; set; } public object Value { get; set; } </code></pre> <p>This has potential to occur many hundreds of times through our codebase, so rather than check MinValue and MaxValue vs the value we're trying to set <em>every time</em>, I thought I'd subclass the control and put the check there:</p> <pre><code>public class OurNumericEditor : Infragistics.Win.UltraWinEditors.UltraNumericEditor { public object Value { get { return base.Value; } set { // make sure what we're setting isn't outside the min or max // if it is, set value to the min or max instead double min = (double)base.MinValue; double max = (double)base.MaxValue; double attempted = (double)value; if (attempted &gt; max) base.Value = max; else if (attempted &lt; min) base.Value = min; else base.Value = value; } } } </code></pre> <p>Clearly this works fine when the type of value, MinValue and MaxValue can be casted to doubles, but I would expect an InvalidCastException when that's not possible.</p> <p>Now I may just be having a blonde moment here but I think it should be possible to write a method that makes use of generics to do the comparison, but I'm struggling to visualise what that might look like.</p> <p>Any ideas or input at all?</p> <p>Thanks<br> Tom</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload