我可以在 VBscript 中使用 System.Collections.Generic.SortedList 吗?

发布于 2025-01-11 15:58:58 字数 407 浏览 1 评论 0原文

我正在尝试在 VBscript 中生成排序列表,这样做:

Set GNDcons = CreateObject( "System.Collections.Generic.SortedList<string, integer>" )

但是它不起作用,我得到 Scode:800a01ad 是否可以在 VBscript 中使用这种类型?我看到 System.Collections 中还有另一个 SortedList,无法设置数据类型,但不推荐使用。

输入图片此处描述

I'm trying to generate a sorted list in VBscript doing this:

Set GNDcons = CreateObject( "System.Collections.Generic.SortedList<string, integer>" )

however it doesn't work, I get Scode:800a01ad
Is it even possible to use this type in VBscript? I saw there's another SortedList in System.Collections without the possibility of setting the data types but the use was deprecated.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘世孤行 2025-01-18 15:58:58

您可以在 VBScript 中使用 System.Collections.SortedList 类型;只需避免 System.Collections.Generic 类型和需要参数的构造函数即可。

这是一个简单的示例:

Dim sl : Set sl = CreateObject("System.Collections.SortedList")
sl.Capacity = 5
sl.Add "Just", 0
sl.Add "One", 1
sl.Add "Of", 2
sl.Add "Many", 3
sl.Add "Examples", 4

Dim i, k, v
For i = 0 To sl.Count-1
    k = sl.GetKey(i)
    v = sl.Item(k)
    WScript.Echo "Item at index " & i & " contains key " & k & " and value " & v
Next

如果您想模拟键和值的类型约束,那么您可以创建一个 VBScript 包装类。例如:

Option Explicit

Class CSortedList
    Private sl
    Private TKeyVarType
    Private TValueVarType

    Private Sub Class_Initialize()
        TKeyVarType = vbEmpty
        TValueVarType = vbEmpty
        Set sl = CreateObject("System.Collections.SortedList")
    End Sub

    Private Sub Class_Terminate()
        Set sl = Nothing
    End Sub

    Public Property Let TKey(ByVal VarType_)
        TKeyVarType = VarType_
    End Property

    Public Property Get TKey()
        TKey = TKeyVarType
    End Property

    Public Property Let TValue(ByVal VarType_)
        TValueVarType = VarType_
    End Property

    Public Property Get TValue()
        TValue = TValueVarType
    End Property

    Public Property Let Capacity(ByVal v_)
        sl.Capacity = v_
    End Property

    Public Property Get Capacity()
        Capacity = sl.Capacity
    End Property

    Public Property Get Count()
        Count = sl.Count
    End Property

    Public Property Get IsFixedSize()
        IsFixedSize = sl.IsFixedSize
    End Property

    Public Property Get IsReadOnly()
        IsReadOnly = sl.IsReadOnly
    End Property

    Public Property Get IsSynchronized()
        IsSynchronized = sl.IsSynchronized
    End Property

    Public Property Let Item(ByVal key, ByVal value)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        ElseIf VarType(value) <> TValueVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for value"
        Else
            sl.Item(key) = value
        End If
    End Property

    Public Property Get Item(ByVal key)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        ElseIf vbObject = VarType(sl.Item(key)) Then
            Set Item = sl.Item(key)
        Else
            Item = sl.Item(key)
        End If
    End Property

    Public Property Get Keys()
        Set Keys = sl.Keys
    End Property

    Public Property Get SyncRoot()
        Set SyncRoot = sl.SyncRoot
    End Property

    Public Property Get Values()
        Set Values = sl.Values
    End Property

    Public Sub Add(ByVal key, ByVal value)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        ElseIf VarType(value) <> TValueVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for value"
        Else
            sl.Add key, value
        End If
    End Sub

    Public Sub Clear()
        sl.Clear
    End Sub

    Public Function Clone()
        Set Clone = sl.Clone()
    End Function

    Public Function Contains(ByVal key)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        Else
            Contains = sl.Contains(key)
        End If
    End Function

    Public Function ContainsKey(ByVal key)
        ContainsKey = Contains(key)
    End Function

    Public Function ContainsValue(ByVal value)
        If VarType(value) <> TValueVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for value"
        Else
            ContainsValue = sl.ContainsValue(value)
        End If
    End Function

    Public Function GetByIndex(ByVal index)
        If vbObject = VarType(sl.GetByIndex(index)) Then
            Set GetByIndex = sl.GetByIndex(index)
        Else
            GetByIndex = sl.GetByIndex(index)
        End If
    End Function

    Public Function GetEnumerator()
        Set GetEnumerator = sl.GetEnumerator()
    End Function

    Public Function GetKey(ByVal index)
        If vbObject = sl.GetKey(index) Then
            Set GetKey = sl.GetKey(index)
        Else
            GetKey = sl.GetKey(index)
        End If
    End Function

    Public Function GetKeyList()
        Set GetKeyList = sl.GetKeyList()
    End Function

    Public Function GetValueList()
        Set GetValueList = sl.GetValueList()
    End Function

    Public Function IndexOfKey(ByVal key)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        Else
            IndexOfKey = sl.IndexOfKey(key)
        End If
    End Function

    Public Function IndexOfValue(ByVal value)
        If VarType(value) <> TValueVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for value"
        Else
            IndexOfValue = sl.IndexOfValue(value)
        End If
    End Function

    Public Sub Remove(ByVal key)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        Else
            sl.Remove key
        End If
    End Sub

    Public Sub RemoveAt(ByVal index)
        sl.RemoveAt index
    End Sub

    Public Sub SetByIndex(ByVal index, ByVal value)
        If VarType(value) <> TValueVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for value"
        Else
            sl.SetByIndex index, value
        End If
    End Sub

    Public Sub TrimToSize()
        sl.TrimToSize
    End Sub
End Class

With New CSortedList
    .TKey = vbString
    .TValue = vbInteger
    .Capacity = 5
    .Add "Just", 0
    .Add "One", 1
    .Add "Of", 2
    .Add "Many", 3
    .Add "Examples", 4

    Dim i, k, v
    For i = 0 To .Count-1
        k = .GetKey(i)
        v = .Item(k)
        WScript.Echo "Item at index " & i & " contains key " & k & " and value " & v
    Next
End With

注意:上面的包装类尚未经过 100% 测试,但确实将键限制为 Variant 字符串子类型,将值限制为 Variant 整数子类型。生成的列表会自动按键排序。

输出:

Item at index 0 contains key Examples and value 4
Item at index 1 contains key Just and value 0
Item at index 2 contains key Many and value 3
Item at index 3 contains key Of and value 2
Item at index 4 contains key One and value 1

You can use the System.Collections.SortedList type in VBScript; just avoid the System.Collections.Generic types, and constructors requiring parameters.

Here's a simple example:

Dim sl : Set sl = CreateObject("System.Collections.SortedList")
sl.Capacity = 5
sl.Add "Just", 0
sl.Add "One", 1
sl.Add "Of", 2
sl.Add "Many", 3
sl.Add "Examples", 4

Dim i, k, v
For i = 0 To sl.Count-1
    k = sl.GetKey(i)
    v = sl.Item(k)
    WScript.Echo "Item at index " & i & " contains key " & k & " and value " & v
Next

If you want to simulate type constraints for keys and values, then you can create a VBScript wrapper class. For example:

Option Explicit

Class CSortedList
    Private sl
    Private TKeyVarType
    Private TValueVarType

    Private Sub Class_Initialize()
        TKeyVarType = vbEmpty
        TValueVarType = vbEmpty
        Set sl = CreateObject("System.Collections.SortedList")
    End Sub

    Private Sub Class_Terminate()
        Set sl = Nothing
    End Sub

    Public Property Let TKey(ByVal VarType_)
        TKeyVarType = VarType_
    End Property

    Public Property Get TKey()
        TKey = TKeyVarType
    End Property

    Public Property Let TValue(ByVal VarType_)
        TValueVarType = VarType_
    End Property

    Public Property Get TValue()
        TValue = TValueVarType
    End Property

    Public Property Let Capacity(ByVal v_)
        sl.Capacity = v_
    End Property

    Public Property Get Capacity()
        Capacity = sl.Capacity
    End Property

    Public Property Get Count()
        Count = sl.Count
    End Property

    Public Property Get IsFixedSize()
        IsFixedSize = sl.IsFixedSize
    End Property

    Public Property Get IsReadOnly()
        IsReadOnly = sl.IsReadOnly
    End Property

    Public Property Get IsSynchronized()
        IsSynchronized = sl.IsSynchronized
    End Property

    Public Property Let Item(ByVal key, ByVal value)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        ElseIf VarType(value) <> TValueVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for value"
        Else
            sl.Item(key) = value
        End If
    End Property

    Public Property Get Item(ByVal key)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        ElseIf vbObject = VarType(sl.Item(key)) Then
            Set Item = sl.Item(key)
        Else
            Item = sl.Item(key)
        End If
    End Property

    Public Property Get Keys()
        Set Keys = sl.Keys
    End Property

    Public Property Get SyncRoot()
        Set SyncRoot = sl.SyncRoot
    End Property

    Public Property Get Values()
        Set Values = sl.Values
    End Property

    Public Sub Add(ByVal key, ByVal value)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        ElseIf VarType(value) <> TValueVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for value"
        Else
            sl.Add key, value
        End If
    End Sub

    Public Sub Clear()
        sl.Clear
    End Sub

    Public Function Clone()
        Set Clone = sl.Clone()
    End Function

    Public Function Contains(ByVal key)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        Else
            Contains = sl.Contains(key)
        End If
    End Function

    Public Function ContainsKey(ByVal key)
        ContainsKey = Contains(key)
    End Function

    Public Function ContainsValue(ByVal value)
        If VarType(value) <> TValueVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for value"
        Else
            ContainsValue = sl.ContainsValue(value)
        End If
    End Function

    Public Function GetByIndex(ByVal index)
        If vbObject = VarType(sl.GetByIndex(index)) Then
            Set GetByIndex = sl.GetByIndex(index)
        Else
            GetByIndex = sl.GetByIndex(index)
        End If
    End Function

    Public Function GetEnumerator()
        Set GetEnumerator = sl.GetEnumerator()
    End Function

    Public Function GetKey(ByVal index)
        If vbObject = sl.GetKey(index) Then
            Set GetKey = sl.GetKey(index)
        Else
            GetKey = sl.GetKey(index)
        End If
    End Function

    Public Function GetKeyList()
        Set GetKeyList = sl.GetKeyList()
    End Function

    Public Function GetValueList()
        Set GetValueList = sl.GetValueList()
    End Function

    Public Function IndexOfKey(ByVal key)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        Else
            IndexOfKey = sl.IndexOfKey(key)
        End If
    End Function

    Public Function IndexOfValue(ByVal value)
        If VarType(value) <> TValueVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for value"
        Else
            IndexOfValue = sl.IndexOfValue(value)
        End If
    End Function

    Public Sub Remove(ByVal key)
        If VarType(key) <> TKeyVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for key"
        Else
            sl.Remove key
        End If
    End Sub

    Public Sub RemoveAt(ByVal index)
        sl.RemoveAt index
    End Sub

    Public Sub SetByIndex(ByVal index, ByVal value)
        If VarType(value) <> TValueVarType Then
            WScript.StdErr.WriteLine "Error: Wrong type for value"
        Else
            sl.SetByIndex index, value
        End If
    End Sub

    Public Sub TrimToSize()
        sl.TrimToSize
    End Sub
End Class

With New CSortedList
    .TKey = vbString
    .TValue = vbInteger
    .Capacity = 5
    .Add "Just", 0
    .Add "One", 1
    .Add "Of", 2
    .Add "Many", 3
    .Add "Examples", 4

    Dim i, k, v
    For i = 0 To .Count-1
        k = .GetKey(i)
        v = .Item(k)
        WScript.Echo "Item at index " & i & " contains key " & k & " and value " & v
    Next
End With

Note: The above wrapper class has not been 100% tested, but does restrict keys to Variant string subtypes, and values to Variant integer subtypes. The resulting list is automatically sorted by keys.

Output:

Item at index 0 contains key Examples and value 4
Item at index 1 contains key Just and value 0
Item at index 2 contains key Many and value 3
Item at index 3 contains key Of and value 2
Item at index 4 contains key One and value 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文