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