1

我创建了这个类,

Interval.cls
public x as new collection
public y as new collection
public z as string

我希望遍历属性,因为我希望用户在我的子表单中选择输入 x,y,z

sub test()

dim inter as new Intervals

inter.x.add "a"
inter.x.add "a"
inter.x.add "b"
inter.x.add "b"
inter.x.add "b"

userString1 = x
userString2 = a

' 我想让它动态化,这样无论用户想要什么,我都可以提供结果。'我只是想让用户字符串与我的属性进行比较成为可能

for each i in inter

'所以我希望这个循环中的 i 是我的属性 x,y,z 所以我可以做 if 语句 if ( i = userString1) then

end if
next i
end sub

我知道我可以在课堂上做一个 tweek 以使其可迭代,我不知道该怎么做

任何帮助表示赞赏

4

2 回答 2

2
'in class
Public Property Get Item(i As Integer) As Variant

   Select Case ndx
      Case 1: Item = Me.x
      Case 2: Item = Me.y
      Case 3: Item = Me.z
   End Select

End Property

'in sub
Dim c as Collection
Dim s as String

For i = 1 to 3
   if i < 3 then 
      set c = inter.Item(i)
      'iterate through collection
   else
      s = inter.Item(i)
   end if
next i

像这样的东西可能是最简单的方法,我没有测试它,但希望它至少能让你开始

于 2013-08-22T16:55:43.423 回答
0

这是你所追求的吗?

'in class
Public x As New Collection
Public y As New Collection
Public z As String
Public Property Get Item(i As Integer) As Variant

   Select Case i
      Case 1: Item = Me.x
      Case 2: Item = Me.y
      Case 3: Item = Me.z
   End Select

End Property

Sub try()

Dim userString2 As String
Dim userString1 As String
Dim inter As Interval
Dim i As Integer

Set inter = New Interval

inter.x.Add "a"
inter.x.Add "a"
inter.x.Add "b"
inter.x.Add "b"
inter.x.Add "b"

userString2 = "aabbb"

For i = 1 To inter.x.Count

       userString1 = userString1 & inter.x.Item(i)
Next i

 If userString1 = userString2 Then
 '<do whatever>
 End If
End Sub

好的,忘记上课而只使用数组怎么样?字符串数组可以是任意长度,您可以通过调用过程动态控制它的大小。

Sub tryWithArray(ByRef StringArray() As String)

Dim userString2 As String
Dim i As Integer

userString2 = "b"


For i = 1 To UBound(StringArray())
    If userString2 = StringArray(i) Then
        'do something
    End If
Next i

End Sub
于 2013-08-22T21:47:51.607 回答