Carlos
Carlos

Reputation: 616

Use the type double as an argument in C#

Consider the following extension to a WPF TextBox:

public static void Parse<T>(this TextBox textBox, Func<string, T> parser)
{
    try
    {
        T value = parser(textBox.Text);
        textBox.Text = value.ToString();
    }
    catch
    {
        textBox.Text = default(T).ToString();
    }
}

That can be used like so:

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    ((TextBox)sender).Parse(double.Parse);
}

My question is: can the extension method be updated/modified so that I can call ((TextBox)sender).Parse(double) instead of ((TextBox)sender).Parse(double.Parse)?


EDIT

The extension method is meant to be very easily used to make sure that the user set, for example, a double or int value inside a TextBox. The idea is to use the method like so:

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    ((TextBox)sender).Parse(double); // make sure the input is a double
    //or
    ((TextBox)sender).Parse(int); // make sure the input is a int
}

What goes inside the extension method doesn't really matter, as long as it works, but I want a minimalist code.

Perhaps a better name for the method would be something like ParseTo(), or better yet, Validate(), as said in the comments.

Upvotes: 0

Views: 385

Answers (2)

BionicCode
BionicCode

Reputation: 28968

You can use the generic parameter type directly:

public static void Validate<TNumeric>(this TextBox textBox) where TNumeric : struct
{
  switch (typeof(TNumeric))
  {
    case Type doubleType when doubleType == typeof(double):
      if (!double.TryParse(textBox.Text, out _))
      {
        textBox.Text = default(TNumeric).ToString();
      } break;
    case Type int32Type when int32Type == typeof(int):
      if (!int.TryParse(textBox.Text, out _))
      {
        textBox.Text = default(TNumeric).ToString();
      } break;
    default: 
      throw new ArgumentException($"Validation of type {typeof(TNumeric)} not supported");
  }
}

Example

numericTextBox.Validate<double>();

Alternatively use specialized methods:

public static void ParseDouble(this TextBox textBox)
{
  if (!double.TryParse(textBox.Text, out _))
  {
    textBox.Text = default(double).ToString();
  }
}

public static void ParseInt32(this TextBox textBox)
{
  if (!int.TryParse(textBox.Text, out _))
  {
    textBox.Text = default(int).ToString();
  }
}

Upvotes: 1

nalnpir
nalnpir

Reputation: 1197

Have a look at this

public static string Parse<T>(string val)
{
    try
    {
        var result = (T)Convert.ChangeType(val, typeof(T));
        return result.ToString();
    }
    catch(Exception ex)
    {
        return default(T).ToString();
    }           
}

public static void Main()
{
    Console.WriteLine(Result<int>("2"));
    Console.WriteLine(Result<double>("22f"));
}

the first result will be 2, since its actually an integer, but the second value is a float, and im trying to parse it as a double, the value will be 0. If it fails it will give you the default value of T.

Upvotes: 2

Related Questions