What's the best way in a method to return BooleanValue AND error messages

Something like this, no need of a boolean:

Public Sub AssignReplacement(db As MySQLCommunityServer, id_user As String, id_main As String)

  Var q As String = "UPDATE remplacement SET id_user_assigned = ? WHERE id_horaire_base = ? ;" 
  
  If db.Connect Then
    db.ExecuteSQL(q, id_user, id_main) // It may fail, catch the error at the caller level
  Else
    Raise New DatabaseException("Couldn't connect", -1) // failed, raise some exception with a message too
  End If
  
  // All done without errors, return
  
End Sub


//****** Now use it somewhere *****

Try
  AssignReplacement(mydb, "user123", "horaire12")
Catch e As DatabaseException   // it'll reach here if it fails
  MessageBox "Replacement failed : " + e.Message
End

1 Like