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
Try
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
Try
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…
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 ;-).
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.
yes, that would always be my advise. Think big, start small. Best starting an own test.app 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 …
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 , in the Xojo world, should I be feeling guilty about using = “”?
@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
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.
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
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 …
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.
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.