I’m working with the DesktopSearchField and have a method to prevent duplicates in the list of recent searches:
Public Sub AddToSearchRecentItems(searchText As String)
If fldSearchSections.RecentItems.Count > 0 Then
For i As Integer = 0 To fldSearchSections.RecentItems.LastIndex
If fldSearchSections.RecentItems(i) = searchText Then Return
Next
End If
fldSearchSections.AddRecentItem(searchText)
I’m getting two errors with this above code:
Too many arguments: got 1, expected only 0.
If fldSearchSections.RecentItems(i) = searchText Then Return
Type mismatch error. Expected Int32, but got String
If fldSearchSections.RecentItems(i) = searchText Then Return
Not sure what’s going on here. Any help is appreciated.
As the error message describes, the RecentItems method does not take any arguments, i.e., “expected only 0”.
It only returns an array of strings.
The second error is fairly common when a method or property is called incorrectly. If you fix the first error, the second should disappear.
In which case, I might rewrite your logic like so:
// the following code has not been tested
Var items() As String = fldSearchSections.RecentItems()
// the follow If statement is not typically necessary
If items.Count > 0 Then
For i As Integer = 0 To items.LastIndex
If items(i) = searchText Then Return
Next
End If
fldSearchSections.AddRecentItem(searchText)
It’s understandable. With Xojo’s implementation of API 2.0 a few years ago, they renamed a major number of control methods to be more explicit in their meaning.
In the example of a DesktopPopupMenu, the RowTextAt method takes an index argument and returns the string for the given menu option. Notice the At suffix on the end of the name. That should help identify similar methods in the future.
Note: the At convention applies to methods that are sometimes readonly as well as for reading and writing, but you’d need to read the documentation to know which is which for some controls.
That’s exactly the reason of the initial confusion, IMO. RecentItems does not have the At suffix so it’s “less clear” that it’s not an array property (even if I personally dislike most of API2 changes).
That’s true, but it can’t be enumerated either (it’s a method rather than a property). I don’t pretend to understand the API2 convention, but when one sees the “At” suffix, I think it’s more obvious to expect that an index can be provided.