Useful array and variant functions
We got quite a few utility functions in MBS Xojo Plugins for you to enjoy in your projects.
GetVariantTypeMBS
Let's say you have a variant containing something. Usually you use VarType() or Variant.Type to query the type.
GetVariantTypeMBS(va as variant) as Integer
The GetVariantTypeMBS function does the same, but has an extra code path for UInt32 and UInt64. While Xojo's functions don't distinguish between signed and unsigned integers, the MBS Plugin can do.
Variant contains | Variant.Type | GetVariantTypeMBS |
---|---|---|
Int32 | 2 | 2 |
UInt32 | 2 | 102 |
Int64 | 3 | 3 |
UInt64 | 3 | 103 |
All other types would be same return values.
Here is a sample code, which tries both functions:
Feel free to try this and see the values in t1 and t2.
GetVariantArrayMBS
You may have a variant containing some kind of Xojo array, e.g. an array of dictionary. The GetVariantArrayMBS function checks if a variant contains an object array and returns it to you as a variant array.
GetVariantArrayMBS(VariantContainingArray as variant) as variant()
Normally in Xojo you would have to code different branches for each type of array, so you can try the cast and get an exception when it fails. You may try things like Dictionary, Pair, Window or other classes you expect. But this function returns you whatever object array is in the variant as a variant array. This way you can inspect each element.
Here is a sample code snippet:
This function raises an exception if variant is nil or the variant is not an object array.
GetVariantArrayUboundMBS
You may want to generically know the upper bound of the array in a variant. The GetVariantArrayUboundMBS function can provide this:
GetVariantArrayUboundMBS(v as variant) as Integer
The function returns -1 for an empty array and -2 for a variant, that does not contain an array.
Here is a sample code snippet:
GetVariantArrayValueMBS
Now if you have a variant containing an array of objects, you can directly ask the plugin to return you an object for an index using the GetVariantArrayValueMBS function.
GetVariantArrayValueMBS(v as variant, index as integer) as variant
This function raises an exception if variant is nil, index is out of range or the variant doesn't contain an array. It works with arrays containing object, Int32, Int64, Single, Double, String or Boolean.
Here is a sample code snippet:
SetVariantArrayValueMBS
After getting a value, you may wonder how to set a value and we have the SetVariantArrayValueMBS function for this:
SetVariantArrayValueMBS(v as variant, index as integer, value as variant)
If index is upper bound plus one, a new value is appended.
Here is a sample code snippet:
This function raises an exception if variant is nil, index is out of range or the variant doesn't contain an array. It works with arrays containing object, Int32, Int64, Single, Double, String or Boolean.
ObjectIsAMBS
ObjectIsAMBS is an utility function to do the IsA check with passing the class name as string. This allows to check a variant with class names coming from a variable.
ObjectIsAMBS(o as object, ClassName as string) as boolean
Returns false in case of object being nil.
If you run this, b1 is true and b2 is false, if w contains a dictionary object.
ArrayIsAMBS
Next you may want to apply the IsA check to an array. In ArrayIsAMBS function the plugin walks over array entries and checks the first object for being of the type of that class.
ArrayIsAMBS(v as variant, ClassName as string) as boolean
Here is a sample code snippet:
Returns false in case of variant being nil, not an object array or all values being nil.
GetVariantAsDictionaryArrayMBS
The GetVariantAsDictionaryArrayMBS specialized variant of GetVariantArrayMBS to return an array of Dictionary:
GetVariantAsDictionaryArrayMBS(v as variant) as Dictionary()
Here is a sample code snippet:
Enjoy these utility functions and use them. And the best: You don't even need a license since these functions are free to use!