Skip to content

VBMAN.ToolsDic - Dictionary Tool Object ​

Overview ​

VBMAN.ToolsDic provides extended functionality for dictionary objects, including deep copy, merge, URL encoding conversion, etc.

Methods ​

DeepCopy ​

Deep copy dictionary object

vb
Public Function DeepCopy(srcDic As Dictionary) As Dictionary

Parameters:

  • srcDic - Source dictionary

Returns: New dictionary object

Description:

  • Currently only implements first-level value copy
  • Object types use Set assignment

Example:

vb
Dim original As New Dictionary
original("key1") = "value1"
original("key2") = 123

' Deep copy
Dim copy As Dictionary
Set copy = VBMAN.ToolsDic.DeepCopy(original)

' Modifying copy does not affect original dictionary
copy("key1") = "modified"
Debug.Print original("key1")  ' Output: value1

OverWrite ​

Merge dictionary to target dictionary

vb
Public Sub OverWrite(DistDic As Dictionary, srcDic As Dictionary, Optional OnlyKey As Boolean = True)

Parameters:

  • DistDic - Target dictionary
  • srcDic - Source dictionary
  • OnlyKey - Whether to only merge existing keys (default True)

Description:

  • Supports recursive merging of nested dictionaries
  • OnlyKey=True only updates keys that exist in target dictionary
  • OnlyKey=False adds all keys from source dictionary

Example:

vb
Dim target As New Dictionary
target("a") = 1
target("b") = 2

Dim source As New Dictionary
source("a") = 100  ' Exists
source("c") = 300  ' Does not exist

' Only merge existing keys
VBMAN.ToolsDic.OverWrite target, source, True
Debug.Print target("a")  ' 100
Debug.Print target.Exists("c")  ' False

' Merge all keys
VBMAN.ToolsDic.OverWrite target, source, False
Debug.Print target("c")  ' 300

ToWwwFormUrlencoded ​

Generate www-form-urlencoded content

vb
Public Function ToWwwFormUrlencoded(Dic As Dictionary) As String

Parameters:

  • Dic - Dictionary object

Returns: URL encoded format string

Example:

vb
Dim params As New Dictionary
params("name") = "Zhang San"
params("age") = 25

Dim encoded As String
encoded = VBMAN.ToolsDic.ToWwwFormUrlencoded(params)
' Output: name=Zhang San&age=25

FromWwwFormUrlencoded ​

Parse www-form-urlencoded content

vb
Public Function FromWwwFormUrlencoded(Content As String, Dic As Dictionary) As Boolean

Parameters:

  • Content - URL encoded format string
  • Dic - Dictionary object to receive result

Returns: True=success, False=failure

Example:

vb
Dim params As New Dictionary
Dim success As Boolean
success = VBMAN.ToolsDic.FromWwwFormUrlencoded("name=Zhang San&age=25", params)

Debug.Print params("name")  ' Zhang San
Debug.Print params("age")   ' 25

TowLevelDicAssign ​

Two-level node dictionary assignment

vb
Public Sub TowLevelDicAssign(Dic As Dictionary, Lv1Name As String, Lv2Name As String, Value As Variant)

Parameters:

  • Dic - Dictionary object
  • Lv1Name - First level key name
  • Lv2Name - Second level key name
  • Value - Value to assign

Description:

  • Automatically creates if first level key does not exist
  • Convenient for operating nested dictionary structures

Example:

vb
Dim data As New Dictionary

' Assign two-level structure
VBMAN.ToolsDic.TowLevelDicAssign data, "user", "name", "Zhang San"
VBMAN.ToolsDic.TowLevelDicAssign data, "user", "age", 25

Debug.Print data("user")("name")  ' Zhang San
Debug.Print data("user")("age")   ' 25

Comprehensive Examples ​

Example 1: Dictionary Merge ​

vb
Private Sub MergeDictionaries()
    Dim defaults As New Dictionary
    defaults("host") = "localhost"
    defaults("port") = 8080
    defaults("timeout") = 30

    Dim config As New Dictionary
    config("port") = 9090  ' Override default value

    ' Merge configuration
    VBMAN.ToolsDic.OverWrite config, defaults, False

    Debug.Print config("host")    ' localhost
    Debug.Print config("port")    ' 9090
    Debug.Print config("timeout") ' 30
End Sub

Example 2: HTTP Parameter Processing ​

vb
Private Sub BuildQueryString()
    Dim params As New Dictionary
    params("search") = "vb6"
    params("page") = 1
    params("size") = 20

    ' Generate URL parameters
    Dim query As String
    query = VBMAN.ToolsDic.ToWwwFormUrlencoded(params)

    Debug.Print "https://api.example.com/search?" & query
    ' Output: https://api.example.com/search?search=vb6&page=1&size=20
End Sub

Example 3: Nested Configuration Structure ​

vb
Private Sub BuildNestedConfig()
    Dim config As New Dictionary

    ' Build nested configuration
    VBMAN.ToolsDic.TowLevelDicAssign config, "database", "host", "localhost"
    VBMAN.ToolsDic.TowLevelDicAssign config, "database", "port", 3306
    VBMAN.ToolsDic.TowLevelDicAssign config, "cache", "enabled", True
    VBMAN.ToolsDic.TowLevelDicAssign config, "cache", "ttl", 3600

    ' Output JSON
    Dim json As New cJson
    json("config") = config
    Debug.Print json.Encode
End Sub

Best Practices ​

  1. Deep Copy Note: Current version only deep copies first level, nested objects still need attention
  2. Merge Strategy: Choose OnlyKey parameter based on requirements
  3. URL Encoding: Use with ToolsHttp.UrlEncode to handle special characters
  4. Nesting Level: TowLevelDicAssign only supports two-level structure

VB6 and LOGO copyright of Microsoft Corporation