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
comments powered by Disqus

Featured

  • 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.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube