Convert to string


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'**********************************************************************************************************
' 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