经典的属性设置!
今天无意中在一个老外的博客里看到了属性的经典设置,尤其是在Set属性时所做的判断那是相当的经典,至少是我见到的经典,也许是我见识太少了.
,他在里面用到了其他基本数据类型的TryParse()方法是很经典.不卖官子了,下面是代码:
1
public class TransactionRequestInfo
2
{
3
public string FirstName { get; set; }
4
public string LastName { get; set; }
5
public string Address { get; set; }
6
public string City { get; set; }
7
public string State { get; set; }
8
public string Country { get; set; }
9
public string Description { get; set; }
10
11
private decimal _amount;
12
public decimal ChargeAmount
13
{
14
get
15
{
16
return _amount;
17
}
18
set
19
{
20
_amount = Decimal.Round(value, 2);
21
}
22
}
23
24
private string _zip;
25
public string Zip
26
{
27
get
28
{
29
return _zip;
30
}
31
set
32
{
33
int res;
34
if (int.TryParse(value, out res))
35
{
36
if (res > 99999)
37
{
38
throw new ArgumentException("Zip Code Value invalid");
39
}
40
else
41
{
42
_zip = res.ToString().PadLeft(5, ‘0′);
43
}
44
}
45
else
46
{
47
throw new ArgumentException("Zip code must be numeric");
48
}
49
}
50
51
}
52
53
private string _securityCode;
54
public string SecurityCode
55
{
56
get
57
{
58
return _securityCode;
59
}
60
set
61
{
62
int res;
63
if (int.TryParse(value, out res))
64
{
65
if (res > 999)
66
{
67
throw new ArgumentException("Security Code Value invalid");
68
}
69
else
70
{
71
_securityCode = res.ToString().PadLeft(3, ‘0′);
72
}
73
}
74
else
75
{
76
throw new ArgumentException("Security code must be numeric");
77
}
78
}
79
80
}
81
82
private string _cardNumber;
83
public string CardNumber
84
{
85
get
86
{
87
return _cardNumber;
88
}
89
set
90
{
91
long res;
92
if (long.TryParse(value, out res))
93
{
94
_cardNumber = res.ToString();
95
}
96
else
97
{
98
throw new ArgumentException("Card Number may only contain numbers");
99
}
100
}
101
}
102
103
private string _expDate;
104
public string ExpDate
105
{
106
get
107
{
108
return _expDate;
109
}
110
set
111
{
112
int res;
113
if (int.TryParse(value, out res))
114
{
115
string exp = res.ToString().PadLeft(4, ‘0′);
116
int month = int.Parse(exp.Substring(0, 2));
117
int yr = int.Parse(exp.Substring(2, 2));
118
119
if (yr > DateTime.Now.Year ||
120
(yr == DateTime.Now.Year && month >= DateTime.Now.Month))
121
{
122
_expDate = month.ToString().PadLeft(2, ‘0′) +
123
"/" + yr.ToString().PadLeft(2, ‘0′);
124
}
125
else
126
{
127
throw new ArgumentException("Expiration Date already passed");
128
}
129
}
130
else
131
{
132
throw new ArgumentException("Expiration Date must be numeric");
133
}
134
}
135
}
136
}
public class TransactionRequestInfo2
{3
public string FirstName { get; set; }4
public string LastName { get; set; }5
public string Address { get; set; }6
public string City { get; set; }7
public string State { get; set; }8
public string Country { get; set; }9
public string Description { get; set; }10

11
private decimal _amount;12
public decimal ChargeAmount13
{14
get15
{16
return _amount;17
}18
set19
{20
_amount = Decimal.Round(value, 2);21
}22
}23

24
private string _zip;25
public string Zip26
{27
get28
{29
return _zip;30
}31
set32
{33
int res;34
if (int.TryParse(value, out res))35
{36
if (res > 99999)37
{38
throw new ArgumentException("Zip Code Value invalid");39
}40
else41
{42
_zip = res.ToString().PadLeft(5, ‘0′);43
}44
}45
else46
{47
throw new ArgumentException("Zip code must be numeric");48
}49
}50

51
}52

53
private string _securityCode;54
public string SecurityCode55
{56
get57
{58
return _securityCode;59
}60
set61
{62
int res;63
if (int.TryParse(value, out res))64
{65
if (res > 999)66
{67
throw new ArgumentException("Security Code Value invalid");68
}69
else70
{71
_securityCode = res.ToString().PadLeft(3, ‘0′);72
}73
}74
else75
{76
throw new ArgumentException("Security code must be numeric");77
}78
}79

80
}81

82
private string _cardNumber;83
public string CardNumber84
{85
get86
{87
return _cardNumber;88
}89
set90
{91
long res;92
if (long.TryParse(value, out res))93
{94
_cardNumber = res.ToString();95
}96
else97
{98
throw new ArgumentException("Card Number may only contain numbers");99
}100
}101
}102

103
private string _expDate;104
public string ExpDate105
{106
get107
{108
return _expDate;109
}110
set111
{112
int res;113
if (int.TryParse(value, out res))114
{115
string exp = res.ToString().PadLeft(4, ‘0′);116
int month = int.Parse(exp.Substring(0, 2));117
int yr = int.Parse(exp.Substring(2, 2));118

119
if (yr > DateTime.Now.Year ||120
(yr == DateTime.Now.Year && month >= DateTime.Now.Month))121
{122
_expDate = month.ToString().PadLeft(2, ‘0′) +123
"/" + yr.ToString().PadLeft(2, ‘0′);124
}125
else126
{127
throw new ArgumentException("Expiration Date already passed");128
}129
}130
else131
{132
throw new ArgumentException("Expiration Date must be numeric");133
}134
}135
}136
}



浙公网安备 33010602011771号