Lists in VBScript

后端 未结 3 1941
孤独总比滥情好
孤独总比滥情好 2020-12-03 10:36

I\'m trying to create a simple list in a VBscript, but I\'m unable to find something similar.

Basically, I\'m working on Active directory, and I need to get all the

3条回答
  •  孤街浪徒
    2020-12-03 11:11

    Here is an alternative as well... An actual List class that I created some time ago. You can freely use/modify it to suit your needs. The original class I built actually depends on another custom class called ArrayIterator.

    Here is how to use it...

    Set myList = New List
    myList.Add("a")
    myList.Add("b")
    myList.Add("c")
    
    ' Iterate through the List using ArrayIterator. You can of course use other methods...
    Set myListItr = myList.GetIterator
    While myListItr.HasNext
      MsgBox myListItr.GetNext
    Wend
    
    ' Iterate through the List by getting the underlying Array.
    Dim element
    For Each element In myList.GetArray
      MsgBox element
    Next
    

    Source code for List class:

    Class List
      Private mArray
    
      Private Sub Class_Initialize()
        mArray = Empty
      End Sub
    
      ' Appends the specified element to the end of this list.
      Public Sub Add(element)
        If IsEmpty(mArray) Then
          ReDim mArray(0)
          mArray(0) = element
        Else
          If mArray(UBound(mArray)) <> Empty Then
            ReDim Preserve mArray(UBound(mArray)+1)        
          End If
          mArray(UBound(mArray)) = element
        End If
      End Sub
    
      '  Removes the element at the specified position in this list.
      Public Sub Remove(index)
        ReDim newArray(0)
        For Each atom In mArray
          If atom <> mArray(index) Then
            If newArray(UBound(newArray)) <> Empty Then
              ReDim Preserve newArray(UBound(newArray)+1)
            End If
            newArray(UBound(newArray)) = atom
          End If
        Next
        mArray = newArray
      End Sub
    
      ' Returns the number of elements in this list.
      Public Function Size
        Size = UBound(mArray)+1
      End Function
    
      ' Returns the element at the specified position in this list.
      Public Function GetItem(index)
        GetItem = mArray(index)
      End Function
    
      ' Removes all of the elements from this list.
      Public Sub Clear
        mArray = Empty
      End Sub
    
      ' Returns true if this list contains elements.
      Public Function HasElements
        HasElements = Not IsEmpty(mArray)
      End Function
    
      Public Function GetIterator
        Set iterator = New ArrayIterator
        iterator.SetArray = mArray
        GetIterator = iterator
      End Function
    
      Public Function GetArray
        GetArray = mArray
      End Function
    
    End Class
    

    Source code for ArrayIterator class:

    Class ArrayIterator
      Private mArray
      Private mCursor  
    
      Private Sub Class_Initialize()
        mCursor = 0
      End Sub
    
      Public Property Let SetArray(array)
        mArray = array    
      End Property
    
      Public Function HasNext
        HasNext = (mCursor < UBound(mArray)+1)
      End Function
    
      Public Function GetNext
        GetNext = mArray(mCursor)
        mCursor = mCursor + 1
      End Function
    End Class
    

提交回复
热议问题