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

Featured

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

Subscribe on YouTube