2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2014-07-31 15:04:09 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
namespace TBF.Boxes
|
|
|
|
|
|
{
|
|
|
|
|
|
public class IntBox : IBox
|
|
|
|
|
|
{
|
|
|
|
|
|
/// Box value
|
|
|
|
|
|
private int val;
|
|
|
|
|
|
public int Val
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (valid ? val : 0); }
|
|
|
|
|
|
set { val = value; valid = true; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Box name
|
|
|
|
|
|
private string name;
|
|
|
|
|
|
public string Name
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return name; }
|
2014-07-31 20:17:38 +00:00
|
|
|
|
set { name = value; }
|
|
|
|
|
|
}
|
2014-07-31 15:04:09 +00:00
|
|
|
|
|
|
|
|
|
|
/// Validity flag: true = the box value is valid
|
|
|
|
|
|
private bool valid = false;
|
|
|
|
|
|
public bool Valid
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return valid; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> Parameterless constructor </summary>
|
2014-07-31 20:17:38 +00:00
|
|
|
|
public IntBox()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2014-07-31 15:04:09 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary> Constructor that initializes the value </summary>
|
2014-07-31 20:17:38 +00:00
|
|
|
|
public IntBox(int val)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Val = val;
|
|
|
|
|
|
}
|
2014-07-31 15:04:09 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clear the box value and the valid flag
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
|
{
|
|
|
|
|
|
val = 0;
|
|
|
|
|
|
valid = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-31 20:17:38 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// 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 float LimitLo = int.MinValue;
|
|
|
|
|
|
public float LimitHi = int.MaxValue;
|
2014-07-31 15:04:09 +00:00
|
|
|
|
|
2014-07-31 20:17:38 +00:00
|
|
|
|
/// <summary>
|
2014-07-31 15:04:09 +00:00
|
|
|
|
/// Converts the box value to a string representation
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!valid)
|
|
|
|
|
|
{
|
2014-07-31 20:17:38 +00:00
|
|
|
|
return FormatInvalid;
|
2014-07-31 15:04:09 +00:00
|
|
|
|
}
|
2014-07-31 20:17:38 +00:00
|
|
|
|
else if (Format == null)
|
2014-07-31 15:04:09 +00:00
|
|
|
|
{
|
2014-07-31 20:17:38 +00:00
|
|
|
|
return string.Format(FormatEx, Val);
|
2014-07-31 15:04:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2014-07-31 20:17:38 +00:00
|
|
|
|
return string.Format(FormatEx, Val.ToString(Format));
|
2014-07-31 15:04:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Validates 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)
|
|
|
|
|
|
{
|
|
|
|
|
|
int dummy;
|
2014-07-31 20:17:38 +00:00
|
|
|
|
return int.TryParse(strVal, out dummy) && (dummy >= LimitLo) && (dummy <= LimitHi);
|
2014-07-31 15:04:09 +00:00
|
|
|
|
}
|
2014-07-31 20:17:38 +00:00
|
|
|
|
|
2014-07-31 15:04:09 +00:00
|
|
|
|
/// <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 = int.Parse(strVal);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch { };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IntBox Clone()
|
|
|
|
|
|
{
|
2014-07-31 20:17:38 +00:00
|
|
|
|
IntBox newFloatBox = new IntBox();
|
|
|
|
|
|
newFloatBox.name = name;
|
|
|
|
|
|
newFloatBox.val = val;
|
|
|
|
|
|
newFloatBox.valid = valid;
|
|
|
|
|
|
newFloatBox.Format = Format;
|
|
|
|
|
|
newFloatBox.FormatInvalid = FormatInvalid;
|
|
|
|
|
|
newFloatBox.FormatEx = FormatEx;
|
|
|
|
|
|
return newFloatBox;
|
|
|
|
|
|
}
|
2014-07-31 15:04:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|