Delphi help with validation

Mobster
Soldato
Joined
9 Apr 2012
Posts
13,151
So I'm forced to use Delphi for this project and I need some help :)

Essentially the user enters coefficients for a constant all the way up to a sextic. These coefficients are validated using a regular expression and if they are valid, then they are converted using StrToFloat.

There is one form (GetFunction) where the user enters the coefficients and on clicking okay, if the coefficients are all valid (out of those which are enabled - done using checkboxes), then the graph is created on the second form (GraphingMode).

The issue I am having is keeping the GetFunction form visible until valid input is received. Currently I have it setup like this:

Code:
procedure TfrmGetFunction.btnAddFunctionClick(Sender: TObject);
  var
    i : integer; // Loop counter.
  begin
    for i := 1 to 7 do
      begin
        CoefficientEdit[i].CoValid := CheckCoefficientBoxesValidInput(CoefficientEdit[i].CoEditBox);
        if not CoefficientEdit[i].CoValid then // If any input box is invalid.
          Exit;
      end;
    frmGraphingMode.CreateGraph(1.001); // Finally if all input boxes valid.
    frmGetFunction.Visible := false;
    frmGraphingMode.Visible := true;
  end;

I am having to use the Exit command which I'm told is a bad idea as it makes the program less structured than it could be.

How do I do this then without using Exit?
 
Sorry is the Exit used when invalid input is found or when valid input is found?

Edit: I'm going assume invalid based upon the comment :p

In that case perhaps people say it's a bad idea because the invalid input needs to be handled somehow with feedback to the user?

Possibly done by raising an exception instead of an Exit? Then whatever calls that procedure handles the exception.

I haven't done Pascal for 15 years mind you :p
 
Last edited:
In that case perhaps people say it's a bad idea because the invalid input needs to be handled somehow with feedback to the user?

I can easily give feedback to the user using this, I think it's because you're essentially exiting a function/procedure without it naturally coming to an end.

I'm just looking for a way to show the GetFunction form until valid input is found in all the input boxes basically :)
 
In situations like this, it is usually a good idea to use a boolean to determine whether something was successful, this gives you a chance to deal with it accordingly, or if all is fine, continue on.

You can also use "Break" to get out of a loop early.

I would try the following:

Code:
procedure TfrmGetFunction.btnAddFunctionClick(Sender: TObject);
  var
    i : integer; // Loop counter.
    bResult: boolean;
  begin
    bResult := True;
    for i := 1 to 7 do
      begin
        CoefficientEdit[i].CoValid := CheckCoefficientBoxesValidInput(CoefficientEdit[i].CoEditBox);
        if not CoefficientEdit[i].CoValid then // If any input box is invalid.
          bResult := False;
          Break;
      end;
    if bResult then
      begin
        frmGraphingMode.CreateGraph(1.001); // Finally if all input boxes valid.
        frmGetFunction.Visible := false;
        frmGraphingMode.Visible := true;
      end
    else
      begin
        // Inform the user that they went wrong
        // By 'Break'ing out of the loop at the point of failure,
        // you also know the value of i, so could feed that back in a message box
      end;
  end;
 
Back
Top Bottom