/// <summary> /// Returns a submitted object coalescing the null instance to a Empty string. /// </summary> /// <param name="o">An object instance.</param> /// <param name="trim">What kind of trim is required in the result.</param> /// <returns>The string value.</returns> public static string ToSafeString(this object o, StringTrim trim) { var s = o.ToSafeString(); switch (trim) { case StringTrim.Trim: { s = s.Trim(); break; } case StringTrim.TrimEnd: { s = s.TrimEnd(); break; } case StringTrim.TrimStart: { s = s.TrimStart(); break; } } return(s); }
/// <summary> /// Cleans a <see cref="String"/> and optionally allows <see cref="String.Empty"/>. /// </summary> /// <param name="value">The value to clean.</param> /// <param name="trim">The <see cref="StringTrim"/> (defaults to <see cref="DefaultStringTrim"/>).</param> /// <param name="transform">The <see cref="StringTransform"/> (defaults to <see cref="DefaultStringTransform"/>).</param> /// <returns>The cleaned value.</returns> public static string?Clean(string?value, StringTrim trim = DefaultStringTrim, StringTransform transform = DefaultStringTransform) { // Handle a null string. if (value == null) { if (transform == StringTransform.NullToEmpty) { return(string.Empty); } else { return(value); } } var tmp = trim switch { StringTrim.Both => value.Trim(), StringTrim.Start => value.TrimStart(), StringTrim.End => value.TrimEnd(), _ => value, }; // Transform the string. return(transform switch { StringTransform.EmptyToNull => (tmp.Length == 0) ? null : tmp, StringTransform.NullToEmpty => tmp ?? string.Empty, _ => tmp, });
private void QueryCustom() { string billNO = txtBillNO.Text.Trim(); string billState = ConvertUtil.ToString(listBillState.EditValue); BindQueryResult(3, billNO, StringTrim.DeleteTrim(billState), dateEditFrom.DateTime.Date, dateEditTo.DateTime.AddDays(1).Date); ClosePopup(); }
/// <summary> /// Cleans a <see cref="String"/> and optionally allows <see cref="String.Empty"/>. /// </summary> /// <param name="value">The value to clean.</param> /// <param name="trim">The <see cref="StringTrim"/> (defaults to <see cref="DefaultStringTrim"/>).</param> /// <param name="transform">The <see cref="StringTransform"/> (defaults to <see cref="DefaultStringTransform"/>).</param> /// <returns>The cleaned value.</returns> public static string Clean(string value, StringTrim trim = DefaultStringTrim, StringTransform transform = DefaultStringTransform) { // Handle a null string. if (value == null) { if (transform == StringTransform.NullToEmpty) { return(string.Empty); } else { return(value); } } // Trim the string. string tmp; switch (trim) { case StringTrim.Both: tmp = value.Trim(); break; case StringTrim.Start: tmp = value.TrimStart(); break; case StringTrim.End: tmp = value.TrimEnd(); break; case StringTrim.None: default: tmp = value; break; } // Transform the string. switch (transform) { case StringTransform.EmptyToNull: return((tmp.Length == 0) ? null : tmp); case StringTransform.NullToEmpty: return(tmp ?? string.Empty); case StringTransform.None: default: return(tmp); } }
private void QueryCustom() { string billNO = txtBillNO.Text.Trim(); string customer = ConvertUtil.StringToNull(txtCustomer.Text.Trim()); string salesMan = txtSalesMan.Text.Trim(); string billType = ConvertUtil.ToString(lookUpEditBillType.EditValue); string billState = ConvertUtil.ToString(listBillState.EditValue); string outboundType = ConvertUtil.ToString(lookUpEditStrategeType.EditValue); string material = ConvertUtil.StringToNull(txtMaterial.Text.Trim()); myPre.BindQueryResult(3, billNO, customer, salesMan, billType, StringTrim.DeleteTrim(billState), outboundType, material, dateEditFrom.DateTime.Date, dateEditTo.DateTime.AddDays(1).Date); }
/// <summary> /// Sets a <see cref="String"/> property value and raises the <see cref="PropertyChanged"/> event where applicable. /// </summary> /// <param name="propertyValue">The property value to set.</param> /// <param name="setValue">The value to set.</param> /// <param name="immutable">Indicates whether the value is immutable; can not be changed once set.</param> /// <param name="trim">The <see cref="StringTrim"/> (defaults to <see cref="StringTrim.End"/>).</param> /// <param name="transform">The <see cref="StringTransform"/> (defaults to <see cref="StringTransform.EmptyToNull"/>).</param> /// <param name="beforeChange">Function to invoke before changing the value; a result of <c>true</c> indicates that the property change is to be cancelled; otherwise, <c>false</c>.</param> /// <param name="propertyNames">The names of the properties that changed.</param> /// <returns><c>true</c> indicates that the property value changed; otherwise, <c>false</c>.</returns> /// <remarks>The first property name specified (see <paramref name="propertyNames"/>) is the primary property; therefore, the only property /// where the <see cref="BeforePropertyChanged"/> event is raised. The additional property names allow for the <see cref="PropertyChanged"/> /// event to be raised for other properties where related versus having to raise seperately.</remarks> protected bool SetValue(ref string propertyValue, string setValue, bool immutable = false, StringTrim trim = StringTrim.End, StringTransform transform = StringTransform.EmptyToNull, Func <string, bool> beforeChange = null, params string[] propertyNames) { ValidateSetValuePropertyNames(propertyNames); lock (_lock) { string val = Cleaner.Clean(setValue, trim, transform); var isChanged = val != propertyValue; if (!RaisePropertyChangedWhenSame && !isChanged) { return(false); } if (IsReadOnly && isChanged) { throw new InvalidOperationException(EntityIsReadOnlyMessage); } if (immutable && isChanged && propertyValue != null) { throw new InvalidOperationException(ValueIsImmutableMessage); } if (beforeChange != null) { if (beforeChange.Invoke(setValue)) { return(false); } } if (OnBeforePropertyChanged(propertyNames[0], setValue)) { return(false); } propertyValue = val; TriggerPropertyChanged(propertyNames); return(true); } }
/// <summary> /// Sets a <see cref="String"/> property value and raises the <see cref="PropertyChanged"/> event where applicable. /// </summary> /// <param name="propertyValue">The property value to set.</param> /// <param name="setValue">The value to set.</param> /// <param name="immutable">Indicates whether the value is immutable; can not be changed once set.</param> /// <param name="trim">The <see cref="StringTrim"/> (defaults to <see cref="StringTrim.End"/>).</param> /// <param name="transform">The <see cref="StringTransform"/> (defaults to <see cref="StringTransform.EmptyToNull"/>).</param> /// <param name="propertyNames">The names of the properties that changed.</param> /// <returns><c>true</c> indicates that the property value changed; otherwise, <c>false</c>.</returns> /// <remarks>The first property name specified (see <paramref name="propertyNames"/>) is the primary property; therefore, the only property /// where the <see cref="BeforePropertyChanged"/> event is raised. The additional property names allow for the <see cref="PropertyChanged"/> /// event to be raised for other properties where related versus having to raise seperately.</remarks> protected bool SetValue(ref string propertyValue, string setValue, bool immutable = false, StringTrim trim = StringTrim.End, StringTransform transform = StringTransform.EmptyToNull, params string[] propertyNames) { return(SetValue(ref propertyValue, setValue, immutable, trim, transform, null, propertyNames)); }