« Brackets Highlighting… | Home | Import CSV with Matri… »

The VariantTypeString function for Xojo

We got a handy function to query a type string for a variant. Useful when displaying variant content in our apps together with stringValue to get text representation. It uses our new GetVariantTypeMBS function to detect unsigned integer types.

The function is using BitwiseAnd to detect if the array bit is set and then BitwiseAnd with the ones complement to remove the bit for the type. For speed we use a dictionary filled on first call. And in case ever a new type is introduced, the break statement should stop in debugging and we can check the new type value and add it to the list.

Please let us know if you like it or have improvement ideas.

Function VariantTypeString(v as Variant) As string // Xojo's VarType doesn't know Unsigned integers 'Dim type As Integer = VarType(v) // MBS VarType can detect unsigned integer Dim type As Integer = GetVariantTypeMBS(v) Dim IsArray As Boolean = BitwiseAnd(type, Variant.TypeArray) = Variant.TypeArray // type without array type = BitwiseAnd(type, Bitwise.OnesComplement(Variant.TypeArray)) // build a dictionary to map types on first call Static TypeMap As Dictionary If TypeMap = Nil Then TypeMap = New Dictionary TypeMap.Value(Variant.TypeBoolean) = "Boolean" TypeMap.Value(Variant.TypeCFStringRef) = "CFStringRef" TypeMap.Value(Variant.TypeColor) = "Color" TypeMap.Value(Variant.TypeCString) = "CString" TypeMap.Value(Variant.TypeCurrency) = "Currency" TypeMap.Value(Variant.TypeDate) = "Date" TypeMap.Value(Variant.TypeDateTime) = "DateTime" TypeMap.Value(Variant.TypeDouble) = "Double" TypeMap.Value(Variant.TypeInt32) = "Int32" TypeMap.Value(Variant.TypeInt64) = "Int64" TypeMap.Value(Variant.TypeInteger) = "Integer" TypeMap.Value(Variant.TypeNil) = "Nil" TypeMap.Value(Variant.TypeObject) = "Object" TypeMap.Value(Variant.TypeOSType) = "OSType" TypeMap.Value(Variant.TypePString) = "PString" TypeMap.Value(Variant.TypePtr) = "Ptr" TypeMap.Value(Variant.TypeSingle) = "Single" TypeMap.Value(Variant.TypeString) = "String" TypeMap.Value(Variant.TypeStructure) = "Structure" TypeMap.Value(Variant.TypeText) = "Text" TypeMap.Value(Variant.TypeWindowPtr) = "WindowPtr" TypeMap.Value(Variant.TypeWString) = "WString" // MBS extra types TypeMap.Value(Variant.TypeInt32+100) = "UInt32" TypeMap.Value(Variant.TypeInt64+100) = "UInt64" End If // lookup type #if DebugBuild then If Not TypeMap.HasKey(type) Then Break // missing type End If #endif If IsArray Then Return "Array of " + TypeMap.Lookup(type,"?") Else Return TypeMap.Lookup(type,"?") End If End Function
19 11 20 - 14:30