V
V
Vladimir Korotenko2020-06-06 10:28:35
Android
Vladimir Korotenko, 2020-06-06 10:28:35

Converting dependent values ​​what can be simplified?

I am writing an area converter. Below is the model code, it confuses code repetitions for each field, what can be corrected?
In the view, the name of the active input field is set, this is from looping. In general, it could be recalculated by the Completed end event or by the ReturnCommand command, but this also does not get rid of duplicates.
Can you suggest any ideas?

private class Model : BaseViewModel
        {
            private const double InitialMs = 1000;
            
            public Model()
            {
                Title = Resource.ConverterSquareTitle;
                ActiveButton = nameof(MsValue);
                MsValue = InitialMs;
            }
            #region Propertyes
            /*
            Квадратный метр (км2)       SquareMsLabel       MsValue
            Квадратный километр (км2)   SquareKmsLabel      KmValue
            Ар «соток» (а)              SquareArLabel       ArValue
            Гектар (га)                 SquareGaLabel       GaValue
            Акр                         SquareAcrLabel      AcrValue
            Квадратная миля             SquareMileLabel     MileValue
            Квадратный ярд (yd2)        SquareYardLabel     YardValue
            Квадратный фут (ft2)        SquareFtLabel       FtValue
             */

            double Ms2Km() => MsValue / 1000000;
            double Ms2Acr() => MsValue / 4046.86;
            double Ms2Ga() => MsValue / 10000;
            double Ms2Ar() => MsValue / 100;
            double Km2Mile() => KmValue / 2.58999;
            double Ms2Yards() => MsValue * 1.19599;
            double Ms2Ft() => MsValue * 10.7639;
            #region MsValue
            private double _ms = InitialMs;

            public double MsValue
            {
                get => _ms;
                set
                {
                    SetProperty(ref _ms, value);
                    if (ActiveButton != nameof(MsValue)) return;
                    KmValue = Ms2Km();
                    ArValue = Ms2Ar();
                    GaValue = Ms2Ga();
                    AcrValue = Ms2Acr();
                    MileValue = Km2Mile();
                    YardValue = Ms2Yards();
                    FtValue = Ms2Ft();
                }
            }

            #endregion
          /// Ниже  куча абсолютно аналогичного кода

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-06-08
@firedragon

What happened in the end. The model took another, so that it would take up less space, the principle is the same.
The main thing is to bring all values ​​to the base and the universal Calculate method that calculates the value.

private class Model : BaseViewModel
        {
            private const double KlConst = 273.15;
            private const double InitialKelvin = KlConst + 20;
            public Model()
            {
                Title = Resource.ConverterTemperatureTitle;
                ActiveButton = nameof(KValue);
                KValue = InitialKelvin;
            }

            #region Props


            private double K2C() => KValue - KlConst;
            private double C2K() => CValue + KlConst;
            private double K2F() => (KValue - KlConst) * (9D / 5) + 32;

            private double F2K() => (FValue - 32) * 5d / 9 + KlConst;
            private double K2Re() => (4d / 5) * (KValue - KlConst);
            private double Re2K() => (5d / 4) * ReValue + KlConst;
            private void Calculate()
            {
                if (ActiveButton != nameof(CValue)) CValue = K2C();
                if (ActiveButton != nameof(FValue)) FValue = K2F();
                if (ActiveButton != nameof(ReValue)) ReValue = K2Re();
            }


            #region CValue
            private double _cValue;

            public double CValue
            {
                get => _cValue;
                set
                {
                    SetProperty(ref _cValue, value);
                    if (ActiveButton != nameof(CValue)) return;
                    KValue = C2K();
                    Calculate();
                }
            }

            #endregion
            #region FValue
            private double _fValue;

            public double FValue
            {
                get => _fValue;
                set
                {
                    SetProperty(ref _fValue, value);
                    if (ActiveButton != nameof(FValue)) return;
                    KValue = F2K();
                    Calculate();
                }
            }

            #endregion
            #region ReValue
            private double _reValue;

            public double ReValue
            {
                get => _reValue;
                set
                {
                    SetProperty(ref _reValue, value);
                    if (ActiveButton != nameof(ReValue)) return;
                    KValue = Re2K();
                    Calculate();
                }
            }

            #endregion
            #region KValue

            private double _kValue;

            public double KValue
            {
                get => _kValue;
                set
                {
                    SetProperty(ref _kValue, value);
                    if (ActiveButton != nameof(KValue)) return;
                    Calculate();
                }
            }

            #endregion







            #endregion
            #region ActiveButton
            private string _activeButton = "";

            public string ActiveButton
            {
                get => _activeButton;
                set => SetProperty(ref _activeButton, value);
            }
            #endregion

        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question