从“空”的控件中为变量赋值,一定有更好的方法!

发布于 2024-08-21 14:59:34 字数 1593 浏览 9 评论 0原文

我有一个供内部使用的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深缘浅 2024-08-28 14:59:34

只需添加一些可重用的函数...例如:

static T? GetValue<T>(YourControlType control) where T : struct
{
    if (string.IsNullOrEmpty(control.Text)) return null;
    return (T)control.EditValue;
}

然后(例如):(

DateTime? crisisPlanDate = GetValue<DateTime>(dat6MonthCrisisPlanReviewed);

其中 YourControlType 是您与 string .Text一起使用的任何控件对象.EditValue)

Just add some reusable functions... for example:

static T? GetValue<T>(YourControlType control) where T : struct
{
    if (string.IsNullOrEmpty(control.Text)) return null;
    return (T)control.EditValue;
}

And then (for example):

DateTime? crisisPlanDate = GetValue<DateTime>(dat6MonthCrisisPlanReviewed);

(where YourControlType is whatever control you are using with a string .Text and object .EditValue)

鸵鸟症 2024-08-28 14:59:34

像这样的东西..

afhAgreement = (!lkuReveiewAFHAgreement.Text.Equals(string.Empty)) ? (Int32)lkuReveiewAFHAgreement.EditValue : null;

riskAgreement = (!lkuReviewRiskAssessment.Text.Equals(string.Empty))  ? (Int32)lkuReviewRiskAssessment.EditValue : null;

something like this..

afhAgreement = (!lkuReveiewAFHAgreement.Text.Equals(string.Empty)) ? (Int32)lkuReveiewAFHAgreement.EditValue : null;

riskAgreement = (!lkuReviewRiskAssessment.Text.Equals(string.Empty))  ? (Int32)lkuReviewRiskAssessment.EditValue : null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文