Over time we collected a few functions in our plugins to help with variants. Beside the internal routines to convert from/to various classes like SAValue, NSObject, CFObject, PROPVARIANT and VARAINT and Xojo variants, we got a lot of useful helps for your daily coding.
We often encounter variants containing arrays and like to assign them to a local variable in Xojo. But you can't just assign an array inside a variant to an array of variant in Xojo. This raises a TypeMismatchException without explanation message. See Feedback case 24167.
Our GetVariantArrayMBS function checks whether the variant contains an array of type object. If so, we return it as an array of variant and you can use it.
When you have a variant of some type of array, you have no way in Xojo to know the upper bound easily (see Feedback case 31799). You would have to query the type of array, then assign to a matching local variable, handle over 20 types for that then know the upper bound. Our GetVariantArrayUboundMBS function makes this easier as it works for all array types.
The GetVariantArrayValueMBS function allows you to query an array values independent of the type of array as variant. No need to query type and assign to local variable, risking a TypeMismatchException. You simply pass index, we lookup type of array and carefully check which variant value to return. As our plugin knows the upper bound, we can do bounds checking and raise an OutOfBoundsException if needed and include a message text of course.
The functions GetVariantArrayValueMBS and GetVariantArrayUboundMBS allow us to look into all arrays in our Web Starter Kit, where we have a memory browser feature.
Need to set a value in an array? You can use SetVariantArrayValueMBS function to set a value in any type of array universally.
The GetVariantAsDictionaryArrayMBS function is relatively new in one of the last releases. Our code to convert JSON to Xojo objects uses dictionary arrays, but how to cast them correctly? Well, in Xojo you have no way to check whether an array is of type object/variant and then check whether all items inside are Dictionary (or nil). Our plugin function does exactly that and helps to process easily arrays of Dictionary stored in variant.
We put in some safe guards, so it will raise an exception if something isn't as expected. Feedback case 61541.
We just added this function to expose our way to know in the plugin, whether a variant contains an unsigned integer. The built-in VarType() function as well as variant.type property will return 2 for both Int32 and UInt32 and 3 for both Int64 and UInt64. Our GetVariantTypeMBS function returns 102 for UInt32 and 103 for UInt64. Adding 100 to the constant value seems to be the easiest for now. There is a feature request for Xojo (Feedback case 36888) to add their own constants and use them everywhere. In case that gets implemented, we could change our functions to return the new values. So be sure you define yourself a kVariantTypeUInt32 = 102 and kVariantTypeUInt64 = 103 constant, so you have one place in future to adjust those values if needed.
The new GetVariantTypeMBS function is coming for version 20.5pr2 of our plugins together with better UInt32/UInt64 variant handling in a dozen classes.
Please do not hesitate to contact us with questions or if you have ideas for new functionality!