Results 1 to 5 of 5

Thread: [RESOLVED] Keyword "Set" -- Never Seen This Usage - Explanation

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    858

    Resolved [RESOLVED] Keyword "Set" -- Never Seen This Usage - Explanation

    I going through some parsing code and never seen this usage of "Set" for a return from a procedure. The procedure is called from multiple sources and are the values added to a Dictionary.
    Comments are mine.

    1) Explanation Please?
    2) Array is returning an Empty value

    Code:
    Private Function parseValue(ByRef str As String, ByRef index As Long)
    'Since return Undefined, Variant returned by Default
    'Called from parse
    'Centralized procedure to parse string / number / object / array / true / false / null
    'Note:
    '1)  parseBoolean, parseNull, parseNumber functions return a single value
    '2)  parseArray, parseObject functions are SET  (?? what does this Set refer to??)
    
       Call skipChar(index)
    
        Select Case m_str(index)
        Case A_DOUBLE_QUOTE, A_SINGLE_QUOTE
            parseValue = parseString(str, index)
           
    '--------  Test  ------
    Debug.Print CStr(parseValue)                  'This Works  (It shows the string retuned but Not associated Key
    '--------  Test End -------
            
            Exit Function
        Case A_SQUARE_BRACKET_OPEN
            Set parseValue = parseArray(str, index)
            Exit Function
        Case A_t, A_f
            parseValue = parseBoolean(str, index)
            Exit Function
        Case A_n
            parseValue = parseNull(str, index)
            Exit Function
        Case A_CURLY_BRACKET_OPEN
            Set parseValue = parseObject(str, index)
            Exit Function
        Case Else
            parseValue = parseNumber(str, index)
            Exit Function
        End Select
    
    End Function
    Last edited by vb6forever; Dec 17th, 2017 at 06:10 PM.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    858

    Re: Keyword "Set" -- Never Seen This Usage - Explanation

    Using Set appears to be correct when assigning an object to a dictionary object. This makes sense

    Code:
    11.9 an object
    The method .Add puts the object into the Dictionary directly.
    The methods (.Items = ; objectvariable=) need the instruction 'Set' to assign the object to the Dictionary item.
    Otherwise the object's default property would be the content of the Dictionary item.
    With CreateObject("scripting.dictionary")
    Set .Items("aa24") = Range("A1:K10")' typename: Range
    Set dict_snb("aa25") = Range("A1:K10")
    .Add "aa26", Range("A1:K10")
    End With

    However for an array this does NOT appear to be the case.
    Code:
    11.7 a 1-dimensional array (typename: Variant()
    With CreateObject("scripting.dictionary")
    .Item("aa18") = Array("aa1", "aa2", "aa3")
    Add "aa19", Split("bb1_cc1_dd1", "_")
    dict_snb("aa20") = Array("aa1", "aa2", "aa3")
    End With

    --------------
    Additional Question
    ---------------------
    What is the best way to deal with in Array when putting it in a Dictionary object?
    For example: Say I have three emails. In an array each would be assigned to its own separate element (index).
    Should the entire array be created as a string and then assigned to a single key or should
    separate keys -- or a subkey type of arrangement used?

    Key Email >> value = Dave.com., Jan.com, Bob.com

    or
    Key Email_1 >> value = Dave.com.
    Key Email-2 >> value = Jan.com
    Key Email_3 >> value = Bob.com

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    858

    Re: Keyword "Set" -- Never Seen This Usage - Explanation

    See Link: https://www.experts-exchange.com/art...ss-in-VBA.html
    From Link:
    1. Dictionary Item can hold an array;
    2. The item associated with a particular key can be changed.

    It seems that it is possible to place an array in Dictionary and work with the array as usual, read and write. But it is not so. Here's an example:

    Sub TestArrInDic()
    Dim dic As New Dictionary 'requires reference to 'Microsoft Scripting Runtime'
    Dim x

    'place an array of variants in dictionary
    dic(1) = Array(7, "foo", Now) 'dic(1) is a shortcut to dic.Item(1) as Item is default property
    'verify that Dictionary Item is really an array
    Debug.Print TypeName(dic(1)) 'Variant()
    Debug.Print LBound(dic(1)), UBound(dic(1)) '0 2
    For Each x In dic(1): Debug.Print x,: Next '7 foo (current date and time)
    Debug.Print
    'try to assign new values to array elements
    dic(1)(0) = -2
    dic(1)(1) = True
    dic(1)(2) = 3.14
    'show array elements
    For Each x In dic(1): Debug.Print x,: Next '7 foo (the same date and time)
    Debug.Print
    End Sub

    Open in new window
    The elements has not changed and you have received no error!
    To workaround you have to extract the whole array to a variable, change it and place it back as a whole:

    Sub TestArrInDic2()
    Dim dic As New Dictionary 'requires reference to 'Microsoft Scripting Runtime'
    Dim x

    'place an array of variants in dictionary
    dic(1) = Array(7, "foo", Now)
    'show array elements
    For Each x In dic(1): Debug.Print x,: Next '7 foo (current date and time)
    Debug.Print
    'retrieve the whole array from Dictionary item
    x = dic(1)
    'assign new values to array elements
    x(0) = -2
    x(1) = True
    x(2) = 3.14
    'put the array back to Dictionary Item
    dic(1) = x
    'show array elements
    For Each x In dic(1): Debug.Print x,: Next '-2 True 3.14
    Debug.Print
    End Sub

    Open in new window
    Another workaround is to create a class containing an array and place an instance of the class in Dictionary.
    Elements of the array will be accessed through property of the class. This method is more complicated in programming but it eliminates the need to rewrite the whole array when you need to change one element.

    Conclusion: the elements of array placed in Dictionary Item are read-only. The array can be changed only as a whole.
    I can explain it as following: if notation dic.Item(1) appeares not immeadetely before assign operator "=" (in the examples above the expression is followed by indexation) then VB treates it as a part of expression and uses Get clause of Dictionary Item property. So a COPY of item content is created, assignment is made to the copy. By the end of statement the copy is wiped like any intermediate data.
    By the way, the behavior of Collection is the same:

    Sub TestArrInCol()
    Dim x
    With New Collection
    .Add Array(7, "foo", Now)
    .Item(1)(0) = -2
    .Item(1)(1) = True
    .Item(1)(2) = 3.14
    For Each x In .Item(1): Debug.Print x,: Next '7 foo (current date and time)
    End With
    End Sub

    Open in new window
    Happy coding!
    Alex

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Keyword "Set" -- Never Seen This Usage - Explanation

    Based on your first posting....

    the function ParseArray() probably returns an object
    the function you posted, ParseValue(), is variant. Therefore, it can contain an object, string, long, etc.

    Based on the comments at top of that procedure, looks like maybe SquareBracket and/or CurlyBracket may be a flag for objects
    'Centralized procedure to parse string / number / object / array / true / false / null
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    858

    Re: Keyword "Set" -- Never Seen This Usage - Explanation

    LaVolpe thanks for responding.

    Turns out that this:

    Code:
    Set parseValue = parseArray(str, index)
    was actually attempting to put a collection, NOT an array, into a dictionary object .
    Never messed with storing collections in dictionaries as an Item.
    Put together this little example.
    Attached Files Attached Files
    Last edited by vb6forever; Dec 18th, 2017 at 09:31 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width