#region
|
|
using System;
|
using System.Data;
|
using CSFrameworkV5.Common;
|
using DevExpress.XtraEditors;
|
|
#endregion
|
|
namespace CSFrameworkV5.Library.CommonClass
|
{
|
/// <summary>
|
/// 输入框检查类
|
/// </summary>
|
public class Assertion
|
{
|
/// <summary>
|
/// 检查Bool值
|
/// </summary>
|
/// <param name="value">当前的值</param>
|
/// <param name="equalValue">需要检查相等的值</param>
|
/// <param name="message">若相值不相等,抛出异常</param>
|
public static void AssertBool(bool value, bool equalValue,
|
string message)
|
{
|
if (value != equalValue) throw new CustomException(message);
|
}
|
|
/// <summary>
|
/// 检查文本框是否为空
|
/// </summary>
|
/// <param name="editor">文本框</param>
|
/// <param name="message">当为空,抛出的消息</param>
|
/// <param name="focus">当为空,文本框设焦点</param>
|
public static void AssertEditorEmpty(BaseEdit editor, string message,
|
bool focus)
|
{
|
if (editor.Text.Trim() == "")
|
{
|
if (focus) editor.Focus();
|
|
throw new CustomException(message);
|
}
|
}
|
|
/// <summary>
|
/// 检查文本框输入的内容为数字
|
/// </summary>
|
/// <param name="editor">文本框</param>
|
/// <param name="message">不是数字,抛出的消息</param>
|
/// <param name="focus">文本框设焦点</param>
|
public static void AssertEditorNumber(BaseEdit editor, string message,
|
bool focus)
|
{
|
AssertEditorEmpty(editor, message, focus);
|
decimal d = 0;
|
if (!decimal.TryParse(editor.Text, out d))
|
{
|
if (focus) editor.Focus();
|
|
throw new CustomException(message);
|
}
|
}
|
|
/// <summary>
|
/// 检查对象对等
|
/// </summary>
|
/// <param name="objA">对象A</param>
|
/// <param name="objB">对象B</param>
|
/// <param name="errMsg">如果不相等提示信息</param>
|
public static void AssertEqual(object objA, object objB, string errMsg)
|
{
|
if (objA != null && objB != null)
|
if (!objA.Equals(objB))
|
throw new Exception(errMsg);
|
}
|
|
/// <summary>
|
/// 检查对象是否为空
|
/// </summary>
|
/// <param name="obj">要检测的对象</param>
|
/// <param name="errMsg">如果为空提示信息</param>
|
public static void AssertNull(object obj, string errMsg)
|
{
|
if (null == obj) throw new Exception(errMsg);
|
}
|
|
/// <summary>
|
/// 检查对象是否为null
|
/// </summary>
|
/// <param name="o">对象</param>
|
/// <param name="message">当为空,抛出的消息</param>
|
public static void AssertObjectIsNull(object o, string message)
|
{
|
if (o == null) throw new CustomException(message);
|
}
|
|
/// <summary>
|
/// 检查数据集是否有数据
|
/// </summary>
|
/// <param name="data"></param>
|
public static void AssertRowNull(DataSet data)
|
{
|
var ret = data == null || data.Tables.Count == 0 ||
|
data.Tables[0].Rows.Count <= 0;
|
if (ret) throw new Exception("该数据集没有数据!");
|
}
|
|
/// <summary>
|
/// 检查数据集是否有数据
|
/// </summary>
|
/// <param name="data"></param>
|
public static void AssertTableEmpty(DataSet ds)
|
{
|
if (ds == null || ds.Tables.Count <= 0 ||
|
ds.Tables[0].Rows.Count <= 0) throw new Exception("该数据集没有数据!");
|
}
|
|
/// <summary>
|
/// 若value参数是零,抛出异常
|
/// </summary>
|
/// <param name="value"></param>
|
/// <param name="message"></param>
|
public static void AssertZero(int value, string message)
|
{
|
if (value == 0) throw new CustomException(message);
|
}
|
}
|
}
|