Where is the Set data type?

Hello! I’m new to Xojo and I’m tying to find the Set data type.

I’ve done a good bit of searching through the language reference and the forum, and I can’t seem to find anything. Is it called something different in Xojo?

Thank you

That doesn’t exist. The closest is a Dictionary, using just the Key with no corresponding Value.

var mySet as new Dictionary
mySet.Value( "a" ) = nil
mySet.Value( "b" ) = nil
mySet.Value( "A" ) = nil // Won't add it again

if mySet.HasKey( "b" ) then
  ...
end if

You could also design a class around this if you want a better or clearer API.

1 Like

its more or less a array where you add a value only once. and after that the values not change.
you could use extends to add this feature to an array, using a method to check
if value is not there, or create a simple class that simulate your set if you need 100% the same.

As I would understand it, one advantage of Kem’s approach is that Searches (and establishing whether something is or is not a member) will be faster using a Dictionary than an Array. Searching in a large Dictionary using a Key is fast. Searching a large Array using a value is not so fast.

Thanks for the replies. Kem’s solution seems to be the best, since using an array would forgo the runtime advantages of a dictionary.

@Kem, I have a question about line 4 of your example. Are string keys in Xojo case-insensitive?

From: https://docs.xojo.com/Dictionary

An object that contains a list of key-value pairs. Both keys and values are Variants. ASCII String keys are case-insensitive, but non-ASCII String keys can be case-sensitive. String keys are encoding-sensitive.

and

String Key Differences

Dictionaries use hashes for their lookup functions. Because of this, string keys need to match exactly; otherwise the dictionary will not consider them equal. The only exception to this rule is with regard to string case. The characters “a” and “A” are treated as identical in dictionary keys because of a case-insensitive hashing function. However, non-ASCII characters such as “é” and “É” are not treated as identical in dictionary keys, even though they are equal in a direct string comparison.

While a UTF-16 string can be compared to a UTF-8 function in your code, they will provide different hashes when working with dictionaries.

Edit: also a good read:

2 Likes

Thanks for finding that link. I’ve updated it with API 2.0 calls.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.