惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
H
Help Net Security
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
L
LangChain Blog
K
Kaspersky official blog
I
Intezer
Martin Fowler
Martin Fowler
爱范儿
爱范儿
AWS News Blog
AWS News Blog
The Hacker News
The Hacker News
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
CXSECURITY Database RSS Feed - CXSecurity.com
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
U
Unit 42
N
News and Events Feed by Topic
A
Arctic Wolf
G
GRAHAM CLULEY
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
F
Fortinet All Blogs
C
Cisco Blogs
美团技术团队
Vercel News
Vercel News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Hacker News: Front Page
T
Tailwind CSS Blog
I
InfoQ
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
P
Palo Alto Networks Blog
A
About on SuperTechFans
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
云风的 BLOG
云风的 BLOG
TaoSecurity Blog
TaoSecurity Blog
Google Online Security Blog
Google Online Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
H
Heimdal Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
O
OpenAI News
博客园 - Franky
Scott Helme
Scott Helme

博客园 - Lucky Jack

在C#中展示嵌入的RTF文件 SQL进行排序、分组、统计的10个新技巧 select into 和 insert into select的区别 Convert的妙用 DataGridView中回车键的妙用 如何去除C#Strings中的空格? Format String for XML Value - Lucky Jack 如何改变字体大小呢? 如何改变字体风格? C# String小技巧 如何避免按回车键时的嗡鸣声? - Lucky Jack - 博客园 如何嵌入图片资源? Lookupedit使用小记 如何优雅的编程? 文件监视器( FileSystemWatcher) 类的使用 - Lucky Jack 反射也可以这样? - Lucky Jack - 博客园 浅谈对象的初始化顺序 也谈String.IsNullOrEmpty 经典sql
经典的属性设置!
Lucky Jack · 2008-02-20 · via 博客园 - Lucky Jack

今天无意中在一个老外的博客里看到了属性的经典设置,尤其是在Set属性时所做的判断那是相当的经典,至少是我见到的经典,也许是我见识太少了.,他在里面用到了其他基本数据类型的TryParse()方法是很经典.不卖官子了,下面是代码:

  1public class TransactionRequestInfo
  2{
  3    public string FirstName getset; }
  4    public string LastName getset; }
  5    public string Address getset; }
  6    public string City getset; }
  7    public string State getset; }
  8    public string Country getset; }
  9    public string Description getset; }
 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(02));
117                int yr = int.Parse(exp.Substring(22));
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}