tags:

views:

685

answers:

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...

+2  A: 

I asked a related question last week.

I think the answer could be effective in your case.

Sklivvz
I failed to see how it can help.I would like to use a converter logic somehow.
ArielBH
You would return something akin to a MathProvider<T>
Sklivvz
I think that the question is how to create this "something akin"
aku
+4  A: 

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
Well, than it is not generic...
ArielBH
Why do you need genericы if all you need is double and int ?
aku
because people might use it with other int or double like types...
ArielBH
Aku, it's cool but unfortunately DoubleConveter does not support decimal :|
ArielBH
I don't understand what are you talking about. Why DoubleConverter? This code works decimal? @decimal = ConvertFromString<decimal>("99999999999999999999999");
aku
and this too: ConvertFromString<decimal>("1.1")
aku
A: 

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?

A: 
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;
}
David B