Insert a module into your project and paste this code:
Public Function SelectRowWithText(Extends p as DesktopPopupMenu, Text as String) As Boolean
var Selected as Boolean = False
#Pragma BreakOnExceptions False
Try
p.SelectRowWithText(Text)
Selected = True
Catch e as RuntimeException
//Insert some other action here if desired
End Try
#Pragma BreakOnExceptions True
Return Selected
End Function
What advantage does a returned Boolean value have? If there is an exception, I put the whole thing in a try-catch and react accordingly if the string was not found. A Boolean value only gives me an indication that I have to react to a missing string…
A simple event “UnhandledExceptions” prevents the app from crashing.
Not really in my use cases - with a boolean returned if have to go back to the place, where the popup is populated and decide which entry should be selected instead…
Yes, there’s this too. A handle-all situations handler, and the following lines of code in the original method won’t execute. Still not better than handling a boolean/return value, IMO.
Hard to tell exactly how much slower but at least in my testing more than 10x slower and sometimes even more.
It is noticable for sure in loops for sure.
I am not selecting any DesktopPopupMenus values in a loop, maybe this is the reason I’ve never seen any slowdowns, when a value is not available and the exception handling in my try-catch block is invoked…
Try-catch is, besides slower code and possible memory leaks, bad practice anyway.
The main reason why I think it is wrong with DesktopPopupMenu.SelectRowWithText is that no other Xojo control with similar method is handling this with an exception too.
Dim start As Double = System.Microseconds
For i as Integer = 0 to 9999
PopupMenu1.SelectRowWithText("C")
PopupMenu1.SelectRowWithText("A")
Next
Dim elapsed As Double = ((System.Microseconds - start)/1000)
Dim start2 As Double = System.Microseconds
For i as Integer = 0 to 9999
try
PopupMenu1.SelectRowWithText("C")
Catch err
end try
try
PopupMenu1.SelectRowWithText("A")
Catch err
end try
Next
Dim elapsed2 As Double = (System.Microseconds - start2)/1000
Label1.Text = "SelectRowWithText x 10000 times" + EndOfLine + EndOfLine + _
"Without Try... Catch: " + chr(9) + elapsed.ToString(locale.Current, "0.00") + " milliseconds" + EndOfLine + _
"Using Try... Catch: " + chr(9) + elapsed2.ToString(locale.Current, "0.00") + " milliseconds"
I wouldn’t say Try/Catch is bad practice, but it is not beginner friendly. I could see a new user to the language become stumped over this.
Eventually every Xojo user learns the necessity of try/catch but it requires a lot more effort than I think Xojo Inc would want beginners to have to put in.
If Xojo had checked exceptions they would be nice to work with. Without them you are basically programming in the dark as you have no idea if and what exceptions you might be dealing with.
At least when a function returns an error message, your code won’t compile unless you write code to handle or ignore the result.