2010-02-18 2 views
1

나는 아래에서 매우 일반적으로 사용되는 가정용 WinForm 응용 프로그램을 가지고 있습니다. 그것은이처럼 보인다 NULL을 할 수있는 그 모든 변수로보고 '비어있는'컨트롤의 변수에 값을 할당하면 더 좋은 방법이 있어야합니다!

  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; 
      } 

이 작업을 수행하려면 말도 안되는 방법이다. Convert this object and Default to NULL이 없습니까?

덧붙여서, 내가 Text 컨트롤의 속성을 사용하더라도 동일한 문제가 있다고 생각하지만 EditValue이 대상입니다.

그럼 더 좋은 방법이 있습니까? 이게 내가 Extension Methods으로 단순화 할 수있는 것입니까? 이 같은

답변

4

가 그냥 몇 가지 재사용 가능한 기능을 추가 ... 예를 들면 : 다음

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); 

(YourControlTypestring .Text 및과 함께 사용중인 모든 컨트롤입니다.3210)

+0

그래서 각 컨트롤 유형에 대해 이와 비슷한 기능을 만들겠습니까? DateTime, LookUp 등 ... –

+0

단일 제어 유형이있는 경우에는 필요하지 않지만 질문에서 명확하지 않습니다. –

+0

죄송합니다! 예, 여러 컨트롤 유형이 있습니다. 'lku'는 LookUp 드롭 다운 박스를,'dat'은 DateEdit Box를 의미합니다. 다른 사람들도 있습니다. 저는 사람들이 대답 할 수있을 정도로 질문을 단순화하기 위해 노력하고있었습니다. –

1

뭔가 ..

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

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