4

I've built a wrapper over NumbericUpDown control. The wrapper is generic and can support int? and double?

I would like to write a method that will do the following.

public partial class NullableNumericUpDown<T> : UserControl where T : struct
{
  private NumbericUpDown numericUpDown;


  private T? Getvalue()
  {
    T? value = numericUpDown.Value as T?; // <-- this is null :) thus my question
    return value;
  }}

of course there is no cast between decimal and double? or int? so I need to use a certain way of converting. I would like to avoid switch or if expressions.

What would you do?

To clarify my question I've provided more code...

ArielBH
  • 1,991
  • 3
  • 22
  • 38
  • Your question isn't quite clear enough. Is the GetValue method part of the wrapper? Or is numericUpDown an instance of the wrapper? Maybe a little more code showing what you are trying to achieve will help. – Paul Batum Sep 21 '08 at 11:10
  • I asked a [related question](http://stackoverflow.com/questions/63694/creating-a-math-library-using-generics-in-c) last week. I think the answer could be effective in your case. – Sklivvz Sep 21 '08 at 11:01
  • I failed to see how it can help. I would like to use a converter logic somehow. – ArielBH Sep 21 '08 at 11:06
  • You would return something akin to a MathProvider – Sklivvz Sep 21 '08 at 11:17
  • I think that the question is how to create this "something akin" – aku Sep 22 '08 at 01:41

3 Answers3

5

It's not clear how you gonna use it. If you want double create GetDouble() method, for integers - GetInteger()

EDIT:

Ok, now I think I understand your use case

Try this:

using System;
using System.ComponentModel;

static Nullable<T> ConvertFromString<T>(string value) where T:struct
{
    TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
    if (converter != null && !string.IsNullOrEmpty(value))
    {
        try
        {
            return (T)converter.ConvertFrom(value);
        }
        catch (Exception e) // Unfortunately Converter throws general Exception
        {
            return null;
        }
    }

    return null;
}

...

double? @double = ConvertFromString<double>("1.23");
Console.WriteLine(@double); // prints 1.23

int? @int = ConvertFromString<int>("100");
Console.WriteLine(@int); // prints 100

long? @long = ConvertFromString<int>("1.1");
Console.WriteLine(@long.HasValue); // prints False
aku
  • 122,288
  • 32
  • 173
  • 203
0

Since this method will always return the result of

numericUpDown.Value

you have no cause for the value to be converted to anything other than Decimal. Are you trying to solve a problem you don't have?

lotsoffreetime
  • 1,100
  • 5
  • 11
0
public class FromDecimal<T> where T : struct, IConvertible
{
    public T GetFromDecimal(decimal Source)
    {
        T myValue = default(T);
        myValue = (T) Convert.ChangeType(Source, myValue.GetTypeCode());
        return myValue;
    }
}

public class FromDecimalTestClass
{
    public void TestMethod()
    {
        decimal a = 1.1m;
        var Inter = new FromDecimal<int>();
        int x = Inter.GetFromDecimal(a);
        int? y = Inter.GetFromDecimal(a);
        Console.WriteLine("{0} {1}", x, y);

        var Doubler = new FromDecimal<double>();
        double dx = Doubler.GetFromDecimal(a);
        double? dy = Doubler.GetFromDecimal(a);
        Console.WriteLine("{0} {1}", dx, dy);
    }
}

private T? Getvalue()
{
  T? value = null;
  if (this.HasValue)
    value = new FromDecimal<T>().GetFromDecimal(NumericUpDown);
  return value;
}
Amy B
  • 108,202
  • 21
  • 135
  • 185