Revised the return statement to correctly handle nested string formatting by ensuring the format string is constructed properly. This resolves potential formatting issues with dynamic values.
123 lines
3.0 KiB
C#
123 lines
3.0 KiB
C#
///
|
|
/// Copyright (c) 2013-2015 Sensus Slovensko a.s.
|
|
///
|
|
namespace TBF.Boxes
|
|
{
|
|
public class DoubleBox : IBox
|
|
{
|
|
/// Box value
|
|
private double val;
|
|
public double Val
|
|
{
|
|
get { return (valid ? val : 0); }
|
|
set { val = value; valid = true; }
|
|
}
|
|
|
|
/// Box name
|
|
string name;
|
|
public string Name
|
|
{
|
|
get { return name; }
|
|
set { name = value; }
|
|
}
|
|
|
|
/// Validity flag: true = the box value is valid
|
|
private bool valid = false;
|
|
public bool Valid
|
|
{
|
|
get { return valid; }
|
|
}
|
|
|
|
/// <summary> Parameterless constructor </summary>
|
|
public DoubleBox()
|
|
{
|
|
}
|
|
|
|
/// <summary> Constructor that initializes the value </summary>
|
|
public DoubleBox(double val)
|
|
{
|
|
this.Val = val;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear the box value and the valid flag
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
val = 0;
|
|
valid = false;
|
|
}
|
|
|
|
///
|
|
/// Specifies a conversion to a string.
|
|
/// In the most complex case the conversion is:
|
|
/// string.Format(FormatEx, val.ToString(Format))
|
|
/// If the value is invalid, the conversion result is 'FormatInvalid'.
|
|
///
|
|
public string Format = null;
|
|
public string FormatEx = "{0}";
|
|
public string FormatInvalid = "---";
|
|
public double Factor = 1.0;
|
|
public double LimitLo = double.MinValue;
|
|
public double LimitHi = double.MaxValue;
|
|
|
|
/// <summary>
|
|
/// Converts the box value to a string representation
|
|
/// </summary>
|
|
public override string ToString()
|
|
{
|
|
if (!valid)
|
|
{
|
|
return FormatInvalid;
|
|
}
|
|
else if (Format == null)
|
|
{
|
|
return string.Format(FormatEx, Val * Factor);
|
|
}
|
|
else
|
|
{
|
|
return string.Format(string.Format("{{0:{0}}}", Format), Val * Factor);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate the string representation of the box value
|
|
/// </summary>
|
|
/// <param name="strVal">String representation of the parameter</param>
|
|
/// <returns>true = string is OK, false = string is wrong (see 'message')</returns>
|
|
public bool ValidateParam(string strVal)
|
|
{
|
|
float dummy;
|
|
return Utils.TryParseSFloat(strVal, out dummy) &&
|
|
((dummy / Factor) >= LimitLo) &&
|
|
((dummy / Factor) <= LimitHi);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update the parameter from a string
|
|
/// </summary>
|
|
/// <param name="strVal">String representation of the box value</param>
|
|
public void UpdateParam(string strVal)
|
|
{
|
|
try
|
|
{
|
|
Val = (double)Utils.ParseSFloat(strVal) / Factor;
|
|
}
|
|
catch { };
|
|
}
|
|
|
|
public DoubleBox Clone()
|
|
{
|
|
DoubleBox newDoubleBox = new DoubleBox();
|
|
newDoubleBox.name = name;
|
|
newDoubleBox.val = val;
|
|
newDoubleBox.valid = valid;
|
|
newDoubleBox.Format = Format;
|
|
newDoubleBox.FormatInvalid = FormatInvalid;
|
|
newDoubleBox.FormatEx = FormatEx;
|
|
newDoubleBox.Factor = Factor;
|
|
return newDoubleBox;
|
|
}
|
|
}
|
|
}
|