Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How should I code this?
Message
From
09/05/2009 04:15:46
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01398722
Message ID:
01398828
Views:
56
>>>Is it OK or not really a good code
>>>
>>>
>>>string strOrderNo = this.txtOrderNo.Text;
>>>                        int? OrderNo;
>>>                        if (strOrderNo.Equals(String.Empty))
>>>                            OrderNo = null;
>>>                        else
>>>                            OrderNo = Convert.ToInt32(strOrderNo);
>>>
>>>                        _dtOrder = biz.Find(OrderNo, this.dtgStartDate.Value, this.dtgEndDate.Value);
>>>
>>
>>That should be fine. I'd probably do something like this, but it's mostly a style preference:
>>
>>
>>int? orderNo = null;
>>if (!string.IsNullOrEmpty(strOrderNo))
>>   orderNo = Convert.ToInt32(strOrderNo);
>>
>>dtOrder = biz.Find(orderNo, this.dtgStartDate.Value, this.dtgEndDate.Value);
>>
>Or just
dtOrder = biz.Find((orderNo == String.Empty) ?  (int?) null :  Convert.ToInt32(orderNo), this.dtgStartDate.Value, this.dtgEndDate.Value);
But null converts to zero so you'd probably need:
int? i2 = (orderNo == String.Empty) ?  (int?) null : (orderNo==null)? (int?)null : Convert.ToInt32(orderNo);
which gets a little less readable. In practice probably better to use TryParse() anyway.....
Best,
Viv
Previous
Reply
Map
View

Click here to load this message in the networking platform