Skip to main content
added 118 characters in body
Source Link
Peter Csala
  • 9.9k
  • 1
  • 15
  • 33

Why don't you define an abstract class with the following two methods:

  • One which can convert an object to string
  • Another which can convert an object to double
public abstract class FromObjectConverter
{
    protected double ToDouble(object value)
        => double.TryParse(value as string, out var x) ? x : double.NaN;

    protected string ToString(object value)
        => value is double x ? x.ToString("f3") : string.Empty;
}

With this class in our hand the two concrete converter classes are just simple wrappers

public sealed class StringToDoubleConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);
}

public sealed class DoubleToStringConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);
}

Or if you want to avoid inheritance then define the FromObjectConverter class and its methods as static.

Why don't you define an abstract class with the following two methods:

  • One which can convert an object to string
  • Another which can convert an object to double
public abstract class FromObjectConverter
{
    protected double ToDouble(object value)
        => double.TryParse(value as string, out var x) ? x : double.NaN;

    protected string ToString(object value)
        => value is double x ? x.ToString("f3") : string.Empty;
}

With this class in our hand the two concrete converter classes are just simple wrappers

public sealed class StringToDoubleConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);
}

public sealed class DoubleToStringConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);
}

Why don't you define an abstract class with the following two methods:

  • One which can convert an object to string
  • Another which can convert an object to double
public abstract class FromObjectConverter
{
    protected double ToDouble(object value)
        => double.TryParse(value as string, out var x) ? x : double.NaN;

    protected string ToString(object value)
        => value is double x ? x.ToString("f3") : string.Empty;
}

With this class in our hand the two concrete converter classes are just simple wrappers

public sealed class StringToDoubleConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);
}

public sealed class DoubleToStringConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);
}

Or if you want to avoid inheritance then define the FromObjectConverter class and its methods as static.

added 6 characters in body
Source Link
Peter Csala
  • 9.9k
  • 1
  • 15
  • 33

Why don't you define an abstract class which defineswith the following two methods:

  • One which can convert an object to string
  • Another which can convert an object to double
public abstract class FromObjectConverter
{
    protected double ToDouble(object value)
        => double.TryParse(value as string, out var x) ? x : double.NaN;

    protected string ToString(object value)
        => value is double x ? x.ToString("f3") : string.Empty;
}

With this class in our hand the two concrete converter classes are just simple wrappers

public sealed class StringToDoubleConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);
}

public sealed class DoubleToStringConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);
}

Why don't you define an abstract class which defines two methods

  • One which can convert an object to string
  • Another which can convert an object to double
public abstract class FromObjectConverter
{
    protected double ToDouble(object value)
        => double.TryParse(value as string, out var x) ? x : double.NaN;

    protected string ToString(object value)
        => value is double x ? x.ToString("f3") : string.Empty;
}

With this class in our hand the two concrete converter classes are just simple wrappers

public sealed class StringToDoubleConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);
}

public sealed class DoubleToStringConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);
}

Why don't you define an abstract class with the following two methods:

  • One which can convert an object to string
  • Another which can convert an object to double
public abstract class FromObjectConverter
{
    protected double ToDouble(object value)
        => double.TryParse(value as string, out var x) ? x : double.NaN;

    protected string ToString(object value)
        => value is double x ? x.ToString("f3") : string.Empty;
}

With this class in our hand the two concrete converter classes are just simple wrappers

public sealed class StringToDoubleConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);
}

public sealed class DoubleToStringConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);
}
Source Link
Peter Csala
  • 9.9k
  • 1
  • 15
  • 33

Why don't you define an abstract class which defines two methods

  • One which can convert an object to string
  • Another which can convert an object to double
public abstract class FromObjectConverter
{
    protected double ToDouble(object value)
        => double.TryParse(value as string, out var x) ? x : double.NaN;

    protected string ToString(object value)
        => value is double x ? x.ToString("f3") : string.Empty;
}

With this class in our hand the two concrete converter classes are just simple wrappers

public sealed class StringToDoubleConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);
}

public sealed class DoubleToStringConverter : FromObjectConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => ToString(value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => ToDouble(value);
}