Correct syntax for using And Or in If Else statements

Hi all.

I have a bunch of fields to which I’ve added lost focus event handlers that update the current row, but I need to establish if Me. is the first one on the page to have been populated and so set it to an insert statement instead.

To do this I’m trying this:

Var antag As String = Me.Value

If StoryWindow.TitleField.Value isEmpty And
StoryWindow.AuthorField.Value isEmpty And
StoryWindow.DescriptionField.Value isEmpty And
StoryWindow.GenreField.Value isEmpty And
StoryWindow.GuildRegField.Value isEmpty And
StoryWindow.LogLineField.Value isEmpty And
StoryWindow.MacGuffinField.Value isEmpty And
StoryWindow.MotifField.Value isEmpty And
StoryWindow.ProtagonistField.Value isEmpty And
StoryWindow.SceneNotesField.Value isEmpty And
StoryWindow.SynopsisField.Value isEmpty And
StoryWindow.TypeField.Value isEmpty And
Me.ValueNot IsEmpty Then

App.PDB.ExecuteSQL(“INSERT INTO story (antagonist), VALUES (’” +antag+ “’);”)
Catch error As DatabaseException
MessageBox(“Database insert error.” +EndOfLine+ “Unable to create a record from Antagonist.”)
End Try

Else If
StoryWindow.TitleField.Value Not IsEmpty Or
StoryWindow.AuthorField.Value Not IsEmpty Or
StoryWindow.DescriptionField.Value Not IsEmpty Or
StoryWindow.GenreField.Value Not IsEmpty Or
StoryWindow.GuildRegField.Value Not IsEmpty Or
StoryWindow.LogLineField.Value Not IsEmpty Or
StoryWindow.MacGuffinField.Value Not IsEmpty Or
StoryWindow.MotifField.Value Not IsEmpty Or
StoryWindow.ProtagonistField.Value Not IsEmpty Or
StoryWindow.SceneNotesField.Value Not IsEmpty Or
StoryWindow.SynopsisField.Value Not IsEmpty Or
StoryWindow.TypeField.Value Not IsEmpty Or
Me.Value Not IsEmpty Then

App.PDB.ExecuteSQL(“UPDATE story SET antagonist = '” +antag+ “’;”)

Catch error As DatabaseException
MessageBox(“Database update error.” +EndOfLine+ “Couldn’t write " +antag+ " as expected.”)
End Try

End If

but Xojo is throwing an error telling me my if statement needs a Then.

So, clearly, I have something in the wrong order. The Me. field is a part of the StoryWindow set, like all the others.

Can anyone help me in regards to where my ANDs and ORs need to go in order for Xojo to see the THEN?

Sorry for the formatting. I can’t seem to work out how this forum wants me to markup code…


you can do something like this:

If StoryWindow.TitleField.Value <> "" Then


If StoryWindow.TitleField.Value <> "" And _
StoryWindow.AuthorField.Value <> "" Then

the “_” means continue on the next line

1 Like

are all you if clauses in one line? They need to be or you can use “_” (the underscore sign) to “structure” your code. Edit: like @DerkJ was showing simultaneously to my post ;-).

1 Like

Oh, good. I had them on a single line at one stage but it was throwing the same error. I must have had another typo in there as well, confusing things.

If the order of my If… And… Then is correct, I’ll press on. Thank you.

Serves me right for starting with too-big blocks of code instead of sorting one line first. Thank you!

1 Like

yes, that would always be my advise. Think big, start small. Best starting an own and building a complex if clause with own variables. Once it is working transfer it to your main app and replace it with right variables, Use breakpoints to test your logic. Once everything ist working add your database code etc, etc …

1 Like

All good.

One further question if I may. I have been taught that = “” or <>"" is inelegant coding and so I always tried for IsEmpty, Not IsEmpty as a preference.

Totally aside from the fact that, in this case, the first option works and mine doesn’t :slight_smile:, in the Xojo world, should I be feeling guilty about using = “”?

  1. Highlight (select) the lines you want to see as code

  2. click on the </> symbol in the editor

Then you’ll get this sort of thing:

Var i as integer
i = 0

Thanks Tim. My lack of _ broke that too, it seems. :slight_smile:

@Shane_Betts I thought you had defined a constant isEmpty. You are using the keywords in the wrong way. I hope the below dummy code will help you:

Var testStr as string = "Jeannot"
Var testStr2 as string = "Shane"
Var testStr3 as string = ""

// Xojo Syntax for isEmpty - String
if testStr.IsEmpty = true  then
  MessageBox( "String is empty :-(" )
end if

if not ( testStr.IsEmpty = true ) then
  MessageBox( "String is not empty :-)" )
end if

// this is working as well, and probably better as no negative test, usually "Not" is more work 
// for a compiler/parser at least this is true for ABAP in SAP R/3 and SAP/HANA, 
// not sure about Xojo though.
// my mantra: running is more important first. I can look at performance, when I'm running into
// issues.
if testStr2.IsEmpty = false then
  messagebox( "String is not empty :-)" )
end if

// this will work as well
// and I tought that you defined a constant somewhere
Const isEmpty as string = ""

if teststr3 = isEmpty then
  MessageBox ("This string is empty as well")
end if
1 Like

Thanks Jeannot. Yes, I initially was using isEmpty and not isEmpty and obviously had some other issues that were breaking my code. Derk suggested I use = “”, which I did, plus a few other changes and now it works. Good to know that, in terms of isEmpty vs = “”, I could go either way.

1 Like

Yes you can, but be aware that Xojo itself has not a special command to check isEmpty. In my example isEmpty is nothing else than a constant (a not modifiable variable), as such it is equal to your “” … “” looks a bit “outdated” to some developers who believe that Xojo is still pure basic etc. If it works, it works, that’s the most important :wink:

Ouch. Use

if testStr.IsEmpty then

The same goes for

if not ( testStr.IsEmpty = true ) then
if testStr2.IsEmpty = false then

Make that

if not testStr2.IsEmpty then

not = true is quite special. = false is superfluous.


ouch, indeed - I’m getting old :slight_smile: - overlooked that one. Was anyways only an example to get @Shane_Betts up and running.

1 Like

But back to the question of the OP. Are there any known performance differences between testing against “” or using myString.isEmpty or is the same thing happening behind the curtains? I assume it is the same for such string comparison, as the compiler will optimize it anyways.

But does anyone know about performance difference between “positive” if then clauses (using = ) and “negative” ones (using NOT or <> )?

At least in ABAP (but that’s now over 2 decades ago) negative tests had terribly bad performance and had to be avoided. On the other hand in SAP you are usually looping and testing over internal table with hundred thousand of entries … For Xojo I have never “seen” a performance impact, never measured it though …

Back to your initial example and @Beatrix_Willius hint / input, you code would need to look like:

If StoryWindow.TitleField.Value.isEmpty AND StoryWindow.AuthorField.Value.isEmpty ... Then

at least here on the forum, you skipped the “.” (dot) between Value and isEmpty.

Sometimes no difference, sometimes Irrelevant.


IsEmpty is a function, so there will be a performance difference, though probably negligible in most scenarios (a couple of milliseconds, probably?). If you want the most speed, switch all instances to a comparison:

If myString = "" Then

At one point some testing was done to similarly compare the following two variations for objects:

If IsNull(myObject) Then
' and
If myObject = Nil Then

And there was a pretty clear advantage in loop scenarios to using the direct comparison. I can’t find that topic now, and I believe it was on the old-old forums.

1 Like

I think I even remember someone saying that

If myString.Length = 0 Then

Is faster than

If myString = "" Then

But I could be remembering that wrong.


Thank you @Anthony_G_Cyphers for clarification! That makes sense and is equivalent to my experience from other languages. That’s why I am always using the direct comparison and didn’t even know that the isEmpty function exists.

@Shane_Betts so you are fine with your working code. It might look “ugly” to some, but it is the best possible version. Even though performance issues will most likely only occur in a looongggg running loop over a lot of data.

1 Like