2011-01-26 27 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); 

}