Test Your .NET 3.5 Apps: C#: Check for Correct Input: Listing 1

The sample relies on a method that accepts only string values from the UI layer and performs validation within the class. Among other things, this approach allows you to write unit tests to examine all possible combinations for passing bad data to this method.

// C#

private bool ValidateData()
{
  decimal value = 0;

  mstrMsg = string.Empty;

  //  User Must Enter a Resource
  if (ddlResource.SelectedIndex <= 0)
  {
    mstrMsg += "You must choose a Resource." +
      Environment.NewLine;
  }
  if (Utilities.IsDate(txtEntryDate.Text))
  {
    //  Entry Date must not be 
    // older than 7 days
    if (Convert.ToDateTime(txtEntryDate.Text) <
      (DateTime.Now.Subtract(
        new TimeSpan(7, 0, 0, 0))))
    {
      mstrMsg += 
        "Entry Date must be greater than " +
        "7 days ago" +
        Environment.NewLine;
    }
    //  Entry Date Must Be Today's Date or Less
    if (Convert.ToDateTime(
      txtEntryDate.Text) > DateTime.Now)
    {
      mstrMsg += "Entry Date must today's 
        date or less" +
        Environment.NewLine;
    }
  }
  else
  {
    mstrMsg += "Entry Date must be a valid date." 
      + Environment.NewLine;
  }

  //  User Must Enter a Customer
  if (ddlCustomer.SelectedIndex <= 0)
  {
    mstrMsg += "You must choose a customer." +
      Environment.NewLine;
  }
  //  Make sure hours entered are decimal values
  if (decimal.TryParse(txtHours.Text, 
    out value) == false)
  {
    mstrMsg += 
      "Hours must be in 
      decimal format." +
      Environment.NewLine;
  }
  else
  {
    //  Hours must be greater than 0
    if (value <= 0)
    {
      mstrMsg += 
        "Hours must be greater than zero." +
        Environment.NewLine;
    }
    //  Hours must be less than 12
    if (value > 12)
    {
      mstrMsg += "Hours must be less than 12." +
        Environment.NewLine;
    }
  }
  //  User Must Enter a Description
  if (txtDescription.Text.Trim() == string.Empty)
  {
    mstrMsg += "Description must be entered." +
      Environment.NewLine;
  }
  else
  {
    // Description length must be greater
    // than 10 characters
    if (txtDescription.Text.Trim().Length < 10)
    {
      mstrMsg += "Description entered must be 
        greater than 10 characters." + 
        Environment.NewLine;
    }
  }

  return (mstrMsg == string.Empty);
}


' VB.NET

Private Function ValidateData() As Boolean
  Dim value As Decimal

  mstrMsg = String.Empty

  ' User Must Enter a Resource
  If ddlResource.SelectedIndex <= 0 Then
    mstrMsg &= "You must choose a Resource." _
       & Environment.NewLine
  End If
  If IsDate(txtEntryDate.Text) Then
    ' Entry Date must not be older than 7 days
    If txtEntryDate.Text < _
      (Date.Now.Subtract(New TimeSpan( _
        7, 0, 0, 0))) Then
      mstrMsg &= "Entry Date must be greater" & _
        “than 7 days " & Environment.NewLine
    End If
    ' Entry Date Must Be Today's Date or Less
    If txtEntryDate.Text > Date.Now Then
      mstrMsg &= "Entry Date must today's date " _
        & "or less" & Environment.NewLine
    End If
  Else
    mstrMsg &= "Entry Date must be a " & _
      "valid date." & Environment.NewLine
  End If
  ' User Must Enter a Customer
  If ddlCustomer.SelectedIndex <= 0 Then
    mstrMsg &= "You must choose a customer." _
      & Environment.NewLine
  End If
  ' Make sure hours entered are decimal values
  If Decimal.TryParse( _
    txtHours.Text, value) = False Then
    mstrMsg &= _
      "Hours must be in decimal format." & _
      Environment.NewLine
  Else
    ' Hours must be greater than 0
    If value <= 0 Then
      mstrMsg &= _
        "Hours must be greater than zero." & _
        Environment.NewLine
    End If
    ' Hours must be less than 12
    If value > 12 Then
      mstrMsg &= "Hours must be less than 12." _ 
        & Environment.NewLine
    End If
  End If
  ' User Must Enter a Description
  If txtDescription.Text.Trim() = String.Empty Then
    mstrMsg &= "Description must be entered." _
      & Environment.NewLine
  Else
    ' Description length must be greater 
    ' than 10 characters
    If txtDescription.Text.Trim().Length < 10 Then
      mstrMsg &= "Description entered must be " _
       & greater than 10 characters." & _
       Environment.NewLine()
    End If
  End If

  Return (mstrMsg = String.Empty)
End Function
comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.