Jump to content

Input validation and error returns


Recommended Posts

First off, I would like to say that these forums are great.  I have been able to find the answers to most of my questions, and great examples and ideas.  Thanks in advance for any ideas and suggestions.

If I am creating a function that does input validation and one of the tests fails its easy enough to return an error number for any test that failed.  

What I  would like to do is return all of the tests that have failed.  I can think of a few ways to do this, just curious to what others thoughts/solutions would be.

I can set an different value for each test that failed, ie.

  •     If Test1Failed Then    $iErr = 1
  •     If Test2Failed Then    $iErr = 2
  •     If Test3Failed Then    $iErr = 3

The thoughts I had to return each test that failed are:

  1. return an array where each row is one of the $iErr values
  2. return a string of $iErr values    '1,2,3'
  3. make $iErr values powers of 2, BitOr them together and return that value and use BitAND later to determine which tests failed

Any suggestions on which approach would be better, or any other suggestions would be appreciated.

Thanks,

David

 

 

 

Link to comment
Share on other sites

How about having a string value for each error instead of a number? You would have to initialize the string with a null value, but then you can add an @CRLF to the beginning of each error that accumulates and add that into a MsgBox string. Something like this:

$txtError = ""
If Test1Failed then $txtError = $txtError & @CRLF & "Test one failed"
If Test2Failed then $txtError = $txtError & @CRLF & "Test two failed"
If Test3Failed then $txtError = $txtError & @CRLF & "Test three failed"
If Test4Failed then $txtError = $txtError & @CRLF & "Test four failed"

If $txtError = "" Then
  MsgBox(48, "Info", "No errors!")
Else
  MsgBox(16, "Error!", $txtError)
EndIf

You could even put the code block in a loop and only exit the loop if there are no errors or the user cancels the process.

Who lied and told you life would EVER be fair?

Link to comment
Share on other sites

5 hours ago, ViciousXUSMC said:

There is no better, just preference.

I tend to go the simplest way possible at first, and get it working, then later if I decide to condense the code or make changes go back and make the changes.

Thanks ViciousXUSMC - I also tend to try the simplest approach first and was leaning toward just returning a string.   Then i saw the suggestion that benched42 made below.  That pretty much helped me decide on returning a string, just not a string of numbers.

3 hours ago, benched42 said:

How about having a string value for each error instead of a number? You would have to initialize the string with a null value, but then you can add an @CRLF to the beginning of each error that accumulates and add that into a MsgBox string. Something like this:

$txtError = ""
If Test1Failed then $txtError = $txtError & @CRLF & "Test one failed"
If Test2Failed then $txtError = $txtError & @CRLF & "Test two failed"
If Test3Failed then $txtError = $txtError & @CRLF & "Test three failed"
If Test4Failed then $txtError = $txtError & @CRLF & "Test four failed"

If $txtError = "" Then
  MsgBox(48, "Info", "No errors!")
Else
  MsgBox(16, "Error!", $txtError)
EndIf

You could even put the code block in a loop and only exit the loop if there are no errors or the user cancels the process.

Thanks for the suggestion benched42.  That is good idea.  I think what I will do is build the string as you suggested and include a number with it, just in case i ever need it.  I can extract it with regex etc to pull it out of the string if need.

Something like

If Test2Failed then $txtError = $txtError & @CRLF & "(Error : 2) Test 2 Failed"

Thanks guys, you've been a great help

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...