vba 在 QTP 中创建数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28047547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating arrays in QTP
提问by user3782816
I am trying to create an array of integers in QTP ( the ints are 9, 16, 25,34,43). I think the code to instantiate it should be (but I could be wrong since I have never created an array in QTP before),
我正在尝试在 QTP 中创建一个整数数组(整数是 9、16、25、34、43)。我认为实例化它的代码应该是(但我可能错了,因为我以前从未在 QTP 中创建过数组),
Dim pages(5)
pages(0) = 9
pages(1) = 16
...
Then I have a for loop with a variable that goes from 1 to 50 and based off of the value of the variable it does one thing and if the variable is one of the values in the array it does something else. For that I have,
然后我有一个 for 循环,它有一个从 1 到 50 的变量,它根据变量的值做一件事,如果变量是数组中的值之一,它会做其他事情。为此我有,
For g = 1 to 50
if g<> 9 and g<> 16 and g<> 25 and g<>34 and g<> 43 Then
DoCoolStuff...
else
DoBoringStuff...
End If
Next
My question is, is there a command that will allow me to replace that ugly if statement with something like if g <> in pages*?
我的问题是,是否有一个命令可以让我用 if g <> in pages* 之类的东西替换那个丑陋的 if 语句?
回答by PaulFrancis
If you want a Dimensioned Array, then that is the only way to declare an array. If you wanted a Non dimensioned array you then can use,
如果您想要一个多维数组,那么这是声明数组的唯一方法。如果你想要一个非维度数组,你可以使用,
Dim pages()
pages = Array(9, 16, 25, 34, 43)
However, you can also do this,
但是,您也可以这样做,
Dim pages()
ReDim pages(5)
pages = Array(9, 16, 25, 34, 43)
Coming to your problem, you can get this going by using the Filter function. Although there is a very small problem. Filtermethod takes in String, so even with that function your will match 1, 2, 3, 4, 5, 6 along with the real/actual values 9, 16, 25, 34, 43.
谈到您的问题,您可以使用 Filter 功能来解决这个问题。虽然有一个很小的问题。Filter方法接受字符串,因此即使使用该函数,您也会匹配 1、2、3、4、5、6 以及真实/实际值 9、16、25、34、43。
As,
作为,
- 1 occurs in 16.
- 2 occurs in 25.
- 3 occurs in 34 and 43.
- 4occurs in 34 and 43.
- 5 occurs in 25.
- 6 occurs in 16.
- 1 发生在 16。
- 2 发生在 25。
- 3 发生在 34 和 43。
- 4 发生在 34 和 43。
- 5 发生在 25。
- 6 发生在 16。
It still thinks they occur in the String. One way to get around this is to Format the numbers as a two literal. Something like.
它仍然认为它们出现在字符串中。解决此问题的一种方法是将数字格式化为两个字面量。就像是。
Dim pages(), g As Integer
ReDim pages(5)
pages = Array("09", "16", "25", "34", "43")
For g = 1 To 50
If UBound(Filter(pages, Format(g, "00"))) > -1 Then
'Do Cool Stuff here
Else
'Do Boring Stuff here
End If
Next
EDIT :
编辑 :
The other way is to create a User Defined Function that could Loop through your Array and find if the Value is Found in your Array. Something like,
另一种方法是创建一个用户定义的函数,它可以循环遍历您的数组并查找是否在您的数组中找到该值。就像是,
Public Function FindArrayElement(SearchArray As Variant, LookupValue As Integer) As Boolean
Dim aCtr As Integer
For aCtr = 0 To UBound(SearchArray)
If CLng(SearchArray(aCtr)) = LookupValue Then
FindArrayElement = True
Exit Function
End If
Next
FindArrayElement = False
End Function
The function takes in two Arguments. The first is the Array in which the values are defined, the second is the Value looked up for. So your Original code would change to.
该函数接受两个参数。第一个是定义值的数组,第二个是查找的值。所以您的原始代码将更改为。
Dim pages(), g As Integer
ReDim pages(5)
pages = Array(9, 16, 25, 34, 43)
For g = 1 To 50
If FindArrayElement(pages, g) Then
'Do Cool Stuff here
Else
'Do Boring Stuff here
End If
Next
回答by TheBlastOne
First, I, too, would suggest to initialize Pages
like this:
首先,我也建议Pages
像这样初始化:
Dim Pages(): Pages=(9,16,25,34,43)
Second, and independently from the first aspect, you could use this code to check if g
is contained in Pages
:
其次,独立于第一个方面,您可以使用此代码检查是否g
包含在Pages
:
Dim Elem
Dim Found: Found=false
For Each Elem in Pages
If Elem = g then
Found=true
Exit For
End If
End For
If Found then
DoBoringStuff
else
DoCoolStuff
End If
The For
..Each
loop iterates as many times as there are elements in the Pages
array. For each iteration, Elem
is set to one Pages
array element.
所述For
..Each
循环迭代多次有中的元件Pages
阵列。对于每次迭代,Elem
设置为一个Pages
数组元素。
Note that the comparison is between Integers, as requested.
请注意,根据要求,比较是在整数之间进行的。