Test Your .NET 3.5 Apps: C#: Check the Business Rule Logic: Listing 2
The protected overload method named ValidateData performs the normal business rule checking after the public overload
ValidateData checks the data for correct data types. The protected ValidateData method uses the private variables that have been set with good data from the public ValidateData method. If errors are encountered in either method, then an appropriate error message is generated and appended to a string variable that can be reported back through a public variable on this class.
// C#
public const string SELECT_TEXT = "[Select]";
private string mstrMsg = string.Empty;
public bool ValidateData(
string Resource, string EntryDate,
string Customer, string Hours,
string Description)
{
decimal value = 0;
mstrMsg = string.Empty;
if (Resource != SELECT_TEXT) {
mstrResource = Resource;
}
if (IsDate(EntryDate)) {
mdtEntryDate =
Convert.ToDateTime(EntryDate);
}
if (Customer != SELECT_TEXT) {
mstrCustomer = Customer;
}
// Make sure hours entered are decimal values
if (decimal.TryParse(Hours, out value) == false) {
mstrMsg += "Hours must be in decimal format." +
Environment.NewLine;
}
else {
mdecHours = value;
}
mstrDescription = Description;
return ValidateData();
}
'VB.NET
Public Const SELECT_TEXT As String = "[Select]"
Private mstrMsg As String = String.Empty
Public Function ValidateData(ByVal Resource _
As String, ByVal EntryDate As String, ByVal _
Customer As String, ByVal Hours As String, _
ByVal Description As String) As Boolean
Dim value As Decimal
mstrMsg = String.Empty
If Resource <> SELECT_TEXT Then
mstrResource = Resource
End If
If IsDate(EntryDate) Then
mdtEntryDate = Convert.ToDateTime(EntryDate)
End If
If Customer <> SELECT_TEXT Then
mstrCustomer = Customer
End If
' Make sure hours entered are decimal values
If Decimal.TryParse(Hours, value) = False Then
mstrMsg &= "Hours must be in decimal format." _
& Environment.NewLine
Else
mdecHours = value
End If
mstrDescription = Description
Return ValidateData()
End Function