我有一个学生收费方法说,因为有三个参数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特林格式不正确。任何帮助请。
武Juice->如果(studentid == ||的String.Empty == feesamount 0 || collectiondate == DateTime.MinValue)我在这里检查collectiondate空或者我没有分配collectiondate == DateTime.MinValue它会给1/1/0001 ...作为输出,这不是我期望的检查空或不...我怎么能这样做.. – Jims 2011-01-26 15:55:39
嗯,`DateTime。如果失败,TryParse会将该值设置为DateTime.MinValue,因此您可以将其与该值进行比较。 – 2011-01-26 16:01:43