views:

48

answers:

2

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?

+1  A: 

something like this..

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

riskAgreement = (!lkuReviewRiskAssessment.Text.Equals(string.Empty))  ? (Int32)lkuReviewRiskAssessment.EditValue : null;
Ramesh Vel
+4  A: 

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)

Marc Gravell
So I would create a function similar to this for EACH control type, correct? DateTime, LookUp, etc...
Refracted Paladin
If you have a single control type, then that wouldn't be necessary - but it isn't clear from the question.
Marc Gravell
Sorry about that! Yes, I have multiple control types. In the question `lku` would refer to a LookUp Drop Down Box and `dat` refers to a DateEdit Box. There are others too I was just trying to keep the question simplistic enough that people would answer.
Refracted Paladin
Oh, and thank you, works great!
Refracted Paladin