Search code examples
delphilogicdelphi-xe

Am I able to use a logic function (and, or) between 2 comparisons


I am unsure whether it is generally impossible to use a logic function between to comparisons or if I've used my logic statement incorrectly because when I make all variables (NewUsername, NewUsername2, NewPass, NewPass2) to the characters "hi", it would continue to display the Application.MessageBox.

    procedure TNewUserFrm.ApplyBtnClick(Sender: TObject);
    begin
      if (NewUsername <> NewUsername2) or (NewPass <> NewPass2) then
      begin
        Application.MessageBox('The usernames or passwords do not match. Try again', 'Error');
      end
      else
      begin
        if not modFile.UsersDataSet.Active then modFile.UsersDataSet.Open;
        modFile.UsersDataSet.Append;
        modFile.UsersDataSet.FieldByName('Username').AsString := NewUsername.Text;
        modFile.UsersDataSet.FieldByName('Password').AsString := NewPass.Text;
        modFile.UsersDataSet.Post;
        NewUserFrm.Hide;
      end;
      NewUsername.Text := '';
      NewUsername2.Text := '';
      NewPass.Text := '';
      NewPass2.Text := '';
      ApplyBtn.SetFocus;
    end;

I have tried using "and" statement, "or" statement and I've also tried using nested "if" statements instead but the same result occurs


Solution

  • You are comparing the TEdit controls addresses, not their content. You need to compare their contents.

    if (NewUsername.Text <> NewUsername2.Text) or (NewPass.Text <> NewPass2.Text) then
    

    Writing something like

    NewUsername <> NewUsername2
    

    will always have the value true in this case because these are two different TEdit controls, and their addresses will never be the same.