从“空”的控件中为变量赋值,一定有更好的方法!
我有一个供内部使用的 winform 应用程序,其中以下内容很常见。
Int32? afhAgreement = null;
if (!lkuReveiewAFHAgreement.Text.Equals(string.Empty))
{
afhAgreement = (Int32)lkuReveiewAFHAgreement.EditValue;
}
DateTime? afhAgreementDate = null;
if (datAFHAgreementCompleted.Text != String.Empty)
{
afhAgreementDate = (DateTime?)datAFHAgreementCompleted.EditValue;
}
Int32? crisisPlan = null;
if (!lkuReview6MonthCrisisPlan.Text.Equals(string.Empty))
{
crisisPlan = (Int32)lkuReview6MonthCrisisPlan.EditValue;
}
DateTime? crisisPlanDate = null;
if (dat6MonthCrisisPlanReviewed.Text != String.Empty)
{
crisisPlanDate = (DateTime?)dat6MonthCrisisPlanReviewed.EditValue;
}
Int32? riskAgreement = null;
if (!lkuReviewRiskAssessment.Text.Equals(string.Empty))
{
riskAgreement = (Int32)lkuReviewRiskAssessment.EditValue;
}
DateTime? riskAgreementDate = null;
if (!datRiskAssessmentReviewed.Text.Equals(string.Empty))
{
riskAgreementDate = (DateTime?)datRiskAssessmentReviewed.EditValue;
}
由于所有这些变量都可以为 NULL,这似乎是一种荒谬的方法。不是有一个Convert this object and Default to NULL
吗?
顺便说一句, EditValue
是一个对象,尽管我相信即使我使用控件的 Text
属性也会遇到同样的问题。
那么,有没有更好的办法呢?我可以使用扩展方法来简化这个过程吗?
I have a winform app for in house use where the below is very common.
Int32? afhAgreement = null;
if (!lkuReveiewAFHAgreement.Text.Equals(string.Empty))
{
afhAgreement = (Int32)lkuReveiewAFHAgreement.EditValue;
}
DateTime? afhAgreementDate = null;
if (datAFHAgreementCompleted.Text != String.Empty)
{
afhAgreementDate = (DateTime?)datAFHAgreementCompleted.EditValue;
}
Int32? crisisPlan = null;
if (!lkuReview6MonthCrisisPlan.Text.Equals(string.Empty))
{
crisisPlan = (Int32)lkuReview6MonthCrisisPlan.EditValue;
}
DateTime? crisisPlanDate = null;
if (dat6MonthCrisisPlanReviewed.Text != String.Empty)
{
crisisPlanDate = (DateTime?)dat6MonthCrisisPlanReviewed.EditValue;
}
Int32? riskAgreement = null;
if (!lkuReviewRiskAssessment.Text.Equals(string.Empty))
{
riskAgreement = (Int32)lkuReviewRiskAssessment.EditValue;
}
DateTime? riskAgreementDate = null;
if (!datRiskAssessmentReviewed.Text.Equals(string.Empty))
{
riskAgreementDate = (DateTime?)datRiskAssessmentReviewed.EditValue;
}
Seeing as all of those variables can be NULL
it seems like this is a ridiculous way to do this. Isn't there a Convert this object and Default to NULL
?
By the way, EditValue
is an object though I believe I have the same issue even if I use the Text
property of the control.
So, is there a better way? Is this something I could simplify with Extension Methods
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需添加一些可重用的函数...例如:
然后(例如):(
其中
YourControlType
是您与string .Text
和一起使用的任何控件对象.EditValue
)Just add some reusable functions... for example:
And then (for example):
(where
YourControlType
is whatever control you are using with astring .Text
andobject .EditValue
)像这样的东西..
something like this..