'********************************************************************************************************** ' Name: stringify ' Author: mielk | 2014-09-20 ' ' Description: Function to convert the given parameter into String. ' This function works similar to VBA built-in function VBA.CStr but it doesn't throw ' an error if value passed as a input parameter cannot be converted to String. ' ' ' Parameters: ' value Value to be converted into String. ' ' ' Returns: ' String The string representation of the given parameter. ' * For primitives value, function returns the same value as VBA built-in function ' VBA.CStr. ' * For arrays, function returns all its values converted to String. ' * For object, function check if it contains function toString and returns its ' result. If there is no such method for this object, value defined in OBJECT_TAG ' constant is returned. ' ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2014-09-20 mielk Function created. '********************************************************************************************************** Public Function stringify(value As Variant) As String Const METHOD_NAME As String = "stringify" '------------------------------------------------------------------------------------------------------ Const OBJECT_TAG = "[Object]" '------------------------------------------------------------------------------------------------------ On Error Resume Next 'For missing and empty parameters, empty String is returned. ----------------------------------------| If Not VBA.IsMissing(value) And Not VBA.isEmpty(value) Then '| '| 'There is different logic for obtaining String representation of object and -----------------| '| 'primitive value, so function needs to check which one is the given parameter. '| '| If VBA.IsObject(value) Then '| '| stringify = value.toString '| '| If VBA.Len(stringify) = 0 Then stringify = OBJECT_TAG '| '| Else '| '| stringify = VBA.CStr(value) '| '| End If '| '| '--------------------------------------------------------------------------------------------| '| '| End If '| '----------------------------------------------------------------------------------------------------| End Function