1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#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);
        }
    }
}