对于ArrayList类,VBScript中是否有IsEqual对象?

时间:2012-12-24 07:07:41

标签: excel-vba vbscript wsh vba excel

请找到以下代码:

Option Explicit
Dim TempArr
Dim ArrListChildDetails :  Set ArrListChildDetails = CreateObject("System.Collections.ArrayList")

现在我已经为它分配了一些Range对象:

ArrListChildDetails = ob3.Cells(ParentMatchRowNum,Width+1).Resize(, UBound(TempArr, 1) + 1)
TempArr=ArrListChildDetails.ToArray()   
ArrListChildDetails .Sort();

现在是否可以比较两个数组,如果它们的元素是相同的,相同的位置和计数呢?表示ArrayList Class中可用的任何IsEql()

2 个答案:

答案 0 :(得分:1)

我不知道,你从哪里得到IsEqual(),但你可以使用ArrayList / Object方法.Equals()来确定一个ArrayList是否被另一个名称/变量引用(参见VBScript的Is运算符) )。

要检查两个不同ArrayLists的计数,顺序和元素是否相等,您必须滚动自己的函数。为了帮助您入门:

Option Explicit

Dim alA  : Set alA  = CreateObject("System.Collections.ArrayList")
Dim alB  : Set alB  = CreateObject("System.Collections.ArrayList")
Dim alC  : Set alC  = CreateObject("System.Collections.ArrayList")
Dim alA2 : Set alA2 = alA

alA.Add "one"
alA.Add "two"

alB.Add "one"
alB.Add "two"

alC.Add "one"
alC.Add "owt"

WScript.Echo "alA: ", Join(AlA.ToArray())
WScript.Echo "alA2:", Join(AlA2.ToArray())
WScript.Echo "alB: ", Join(AlB.ToArray())
WScript.Echo "alC: ", Join(AlC.ToArray())

WScript.Echo "alA.Equals(alA2):", CStr(alA.Equals(alA2))
WScript.Echo "alA.Equals(alB):" , CStr(alA.Equals(alB))
WScript.Echo "alA.Equals(alC):" , CStr(alA.Equals(alC))

WScript.Echo "ALEqual(alA, alA):", CStr(ALEqual(alA, alA))
WScript.Echo "ALEqual(alA, alA2):", CStr(ALEqual(alA, alA2))
WScript.Echo "ALEqual(alA, alB):", CStr(ALEqual(alA, alB))
WScript.Echo "ALEqual(alA, alC):", CStr(ALEqual(alA, alC))

Function ALEqual(alL, alR)
  ALEqual = True
  If alL Is AlR Then Exit Function
  ALEqual = alL.Count = alR.Count
  If ALEqual Then
     Dim i
     For i = 0 To alL.Count - 1
         If alL(i) <> alR(i) Then
            ALEqual = False
            Exit Function
         End If   
     Next
  End If  
End Function

输出:

alA:  one two
alA2: one two
alB:  one two
alC:  one owt
alA.Equals(alA2): Wahr
alA.Equals(alB): Falsch
alA.Equals(alC): Falsch
ALEqual(alA, alA): Wahr
ALEqual(alA, alA2): Wahr
ALEqual(alA, alB): Wahr
ALEqual(alA, alC): Falsch

答案 1 :(得分:1)

快速而肮脏的解决方案,但在大多数情况下足够:

WScript.Echo Join(ArrListChildDetails.ToArray(),"|") = Join(ArrListChildDetails2.ToArray(),"|")
编辑:一些解释:比较两个ArrayLists(或两个数组),转换为一个数组并将它们连接在一起,由一些未在数组中使用的特殊字符分隔,然后比较字符串,当然这只是可用于具有可以字符串化的值类型的一维数组。如果它们具有相同内容的相同数量的元素,则比较将返回-1(真)和0(否则)