//Return true if value in list. //Throws ArgumentException if value cant be converted to list type. public bool InList(string value) { if (_ranges == null || RangeListType == FieldDataType.UNKNOWN) { throw new InvalidOperationException("RangeList hasn't been initialized."); } if (FieldTypeUtil.CheckType(value, RangeListType) == false) { throw new ArgumentException(string.Format("Value can't be converted to type {0}.", RangeListType)); } bool found = false; foreach (Range r in _ranges) { if (r.InRange(value) == true) { found = true; break; } } return(found); }
//Perform a type specific check //Return true if value is in range public bool InRange(string value) { if (FieldTypeUtil.Compare(value, MinVal, _list.RangeListType) >= 0 && FieldTypeUtil.Compare(value, MaxVal, _list.RangeListType) <= 0) { return(true); } else { return(false); } }
/* * Verify that any user supplied value in dataEntryFields matches its * specified fielddatatype. All fields must specify a fieldDataType. * * Only validate if user supplies a value. * * Put any errors in notifications. * * Return FALSE if any validation errors occur. * */ protected bool ValidateFieldType2(DataFieldControl dfc, ArrayList notifications) { if (dfc.FieldTextBoxText.Trim() != string.Empty && FieldTypeUtil.CheckType(dfc.FieldTextBoxText.Trim(), dfc.FieldDataType) == false) { dfc.AddError(string.Format("{0} is not a valid {1} value.", Shorten(dfc.FieldLabelText), dfc.FieldDataType)); notifications.Add(string.Format("{0} is not a valid {1} value.", Shorten(dfc.FieldLabelText), dfc.FieldDataType)); return(false); } else { return(true); } }
/* * Given a string that's an item in the list * first check to see if its an single item x * or a range x thru y. * * Throw an ArgumentException if items can't * be converted to itemType */ public Range(string item, RangeList list) { _list = list; string[] sArr = item.Trim().Split(new string[] { "thru" }, StringSplitOptions.RemoveEmptyEntries); if (sArr.Length == 1) { //single item. Check that it can be converted to list type and set min/max val if (FieldTypeUtil.CheckType(sArr[0].Trim(), list.RangeListType) == false) { throw new ArgumentException(string.Format("Item not valid. Can't convert {0} to {1}", sArr[0], list.RangeListType)); } else { MinVal = sArr[0]; MaxVal = sArr[0]; } } else if (sArr.Length == 2) { //range if (FieldTypeUtil.CheckType(sArr[0].Trim(), list.RangeListType) == false) { throw new ArgumentException(string.Format("Item not valid. Can't convert {0} to {1}", sArr[0], list.RangeListType)); } else if (FieldTypeUtil.CheckType(sArr[1].Trim(), list.RangeListType) == false) { throw new ArgumentException(string.Format("Item not valid. Can't convert {0} to {1}", sArr[1], list.RangeListType)); } else if (FieldTypeUtil.Compare(sArr[0], sArr[1], list.RangeListType) > 0) { throw new ArgumentException("Item not valid. In a range x thru y, x must be less than or equal to y."); } else { MinVal = sArr[0]; MaxVal = sArr[1]; } } else { //must have 1 or 2 items throw new ArgumentException("Item not valid."); } }
//Do a type specific comparison: //Return 0 if equal -1 if val1 < val2 1 if val1 > val2 //Throws an ArgumentException if val1 or val2 can't be converted //to FieldDataType fdt public static int Compare(string val1, string val2, FieldDataType fdt) { //check types if (FieldTypeUtil.CheckType(val1, fdt) == false || FieldTypeUtil.CheckType(val2, fdt) == false) { throw new ArgumentException(string.Format("Values must be convertable to type {0}.", fdt)); } int result = 0; switch (fdt) { case FieldDataType.DATE: DateTime dtVal1 = DateTime.Parse(val1); DateTime dtVal2 = DateTime.Parse(val2); result = dtVal1.CompareTo(dtVal2); break; case FieldDataType.FLOAT: float fVal1 = float.Parse(val1); float fVal2 = float.Parse(val2); result = fVal1.CompareTo(fVal2); break; case FieldDataType.INT: int iVal1 = int.Parse(val1); int iVal2 = int.Parse(val2); result = iVal1.CompareTo(iVal2); break; case FieldDataType.TEXT: result = val1.CompareTo(val2); break; default: throw new Exception("FieldDataType invalid."); } return(result); }