2011-01-26 50 views
0

我有一個學生收費方法說,因爲有三個參數studid,feeamount,收集日期。方法定義如下。在C#中需要方法參數類型的幫助.net

public string CollectFee(string studentid, decimal feeamount, DateTime collectiondate) 
{ 
} 

在此同時呼籲從表現layer..user這種方法必須要有進入,如果任何參數的缺失,他將得到的消息一樣.... studentid和FEEAMOUNT和收集日期是所有參數需要。我想在執行CollectFee方法時檢查這些,因爲方法參數包含字符串,小數,日期時間數據類型,我如何在一個條件中檢查是否爲空。

要做到這一點我寫了這個代碼,我不知道是否它的正確與否:

protected void BtnAdd_Click(object sender, EventArgs e) 
{ 
    Student obj = new Student(); 
    string studentid = TxtStdId.Text; 
    decimal collectionamount = 0.0m; 
    if (txtCollectionAmount.Text !=null) 
    { 
     collectionamount = Convert.ToDecimal(txtCollectionAmount.Text); 
    } 
    DateTime collectiondate = Convert.ToDateTime(txtCollectionDate.Text);   
    lblResult.Text=obj.FeesCollection(studentid, collectionamount, collectiondate); 

} 

public class Student 
{ 
public string FeesCollection(string studentid, decimal feesamount, DateTime collectiondate) 
{ 
    string Result = string.Empty; 
    if (studentid == string.Empty || feesamount == 0 || collectiondate == DateTime.MinValue)   
    { 
     Result = "Required fields are empty."; 
    } 
    else 
    { 
     Result = "Success."; 
    } 
    return Result; 
} 

}

,同時從我寫了這個代碼GUI層調用此

但在測試無效狀態時,如保持amounttextbox或datetimetextbox爲空 獲取異常,如輸入s特林格式不正確。任何幫助請。

回答

1

這是因爲Text屬性時是空白的,而不是一個null返回一個空字符串(string.Empty),所以:

if (txtCollectionAmount.Text != string.Empty) 
{ 
    collectionamount = Convert.ToDecimal(txtCollectionAmount.Text); 
} 

..should做的伎倆。

還要注意,用戶可以在這裏鍵入任何內容,並且會得到一個異常。考慮改用Decimal.TryParse

+0

武Juice->如果(studentid == ||的String.Empty == feesamount 0 || collectiondate == DateTime.MinValue)我在這裏檢查collectiondate空或者我沒有分配collectiondate == DateTime.MinValue它會給1/1/0001 ...作爲輸出,這不是我期望的檢查空或不...我怎麼能這樣做.. – Jims 2011-01-26 15:55:39

+0

嗯,`DateTime。如果失敗,TryParse會將該值設置爲DateTime.MinValue,因此您可以將其與該值進行比較。 – 2011-01-26 16:01:43

2

我不會在該方法中實現這一點。

像這樣的驗證在GUI層更好,您應該在那裏返回丟失的確切項目,而不僅僅是失敗或成功。

將驗證放在方法中會使得報告建設性錯誤消息變得更加困難,並且對於未來的修改,您將在商業邏輯中擁有硬編碼的GUI信息。

如果您在後面的代碼中直接檢查缺少的信息,可以突出顯示缺少的項目。

但該方法可能會導致錯誤檢查,特別是對於有效studentid和數量是正確的或likewize。

這樣你可以得到更多的錯誤檢查,並且可以使用不同形式的方法。

如果您已在頁面中進行驗證,那麼有一些非常好的工具可以使用,它還將客戶端驗證的優勢作爲第一行,然後在服務器端添加嘗試的黑客行爲。

微軟長谷在這幾篇文章:http://msdn.microsoft.com/en-us/library/ms229603.aspx

+0

DavidMårtensson->這是不是exat方法..我剛剛給了樣本...成功意味着它會做一些businesslogic – Jims 2011-01-26 15:18:04

0

txtCollectionAmount.Text不是有效的小數。嘗試

decimal dec; 
decimal.TryParse(txtCollectionAmount.Text, out dec); 

這將確保txtCollectionAmount.Text確實是一個小數。

0

TextBox控件可以同時具有數字和非數字數據。您需要爲這種可能性這樣的代碼:

protected void BtnAdd_Click(object sender, EventArgs e) 
{ 
    Student obj = new Student(); 
    string studentid = TxtStdId.Text; 
    decimal collectionamount = 0.0m; 
    if (txtCollectionAmount.Text !=null) 
    { 
     bool canConvert = decimal.TryParse(txtCollectionAmount.Text, out collectionAmount); 

     if (!canConvert) 
     { 
      // ... obviously the text in the textbox was invalid for some reason... 
      // put the handler for the invalid data here. 
     } 
    } 
    DateTime collectiondate = Convert.ToDateTime(txtCollectionDate.Text);   
    lblResult.Text=obj.FeesCollection(studentid, collectionamount, collectiondate); 

}