vbScript - 对数组使用 Select/Case

vbScript - Using Select/Case with an array

我需要以下方面的帮助:

Dim reportesTA(2)
reportesTA(0) = "Report1"
reportesTA(1) = "Report2"

我想对数组列表中的每一项执行某些操作。所以我正在考虑使用 Select/Case。我试过这个但没有用:

Select Case reportesTA
Case 0
//do stuff
Case 1
//do stuff
End Select

有什么方法可以让案例切换吗?像 switch/case 之类的东西?有人有更好的方法来处理数组的每一项吗?非常感谢。

Dim reportesTA(2)
reportesTA(0) = "Report1"
reportesTA(1) = "Report2"

For I = LBound(reportesTA) to UBound(reportesTA)
    Select Case reportesTA(I)
        Case "Report1"
            MsgBox "Report1"
        Case "Report2"
            MsgBox "Report2"
    End Select
Next

解释:

在"FOR"循环中我们遍历数组中的所有元素,从第一个到最后一个。

function "LBound" return 数组中可用元素的最低 ID。

函数 "UBound" return 数组中可用元素的最高 ID。

在"SELECT CASE"中,我们从数组中获取元素的值(是的,它有字符串类型),然后决定我们应该做什么——在这个示例中,我们弹出显示报告名称的消息框。

您需要使用数组的元素而不是数组本身来消除 Select 的情况。

我会这样做,更清楚你在做什么,也更干。

reportesTA = Array("Report1", "Report2")

Sub do_stuff(text)
  WScript.echo text
End Sub

For each element in reportesTA
  Select Case element
    Case "Report1"
      do_stuff "Report1"
    Case "Report2"
      do_stuff "Report2"
  End Select
Next

在此示例中,您宁愿执行以下操作

For each element in reportesTA
  do_stuff element
Next