如何对数组中的每个循环使用a ?

时间:2023-02-04 19:40:26

I have an array of Strings:

我有一个字符串数组:

Dim sArray(4) as String

I am going through each String in the array:

我将遍历数组中的每个字符串:

for each element in sarray
  do_something(element)
next element

do_something takes a string as a parameter

do_something将字符串作为参数

I am getting an error passing the element as a String:

我将获得一个错误,将元素作为字符串传递:

ByRef Argument Mismatch

ByRef参数不匹配

Should I be converting the element to a String or something?

我应该把元素转换成字符串还是别的什么?

4 个解决方案

#1


77  

Element needs to be a variant, so you can't declare it as a string. Your function should accept a variant if it is a string though as long as you pass it ByVal.

元素需要是一个变体,所以不能将其声明为字符串。如果是字符串,那么函数应该接受一个变体,只要传递它为ByVal。

Public Sub example()
    Dim sArray(4) As string
    Dim element As variant

    For Each element In sArray
        do_something (element)
    Next element
End Sub


Sub do_something(ByVal e As String)

End Sub

The other option is to convert the variant to a string before passing it.

另一个选项是在传递变量之前将其转换为字符串。

  do_something CStr(element)

#2


25  

A for each loop structure is more designed around the collection object. A For..Each loop requires a variant type or object. Since your "element" variable is being typed as a variant your "do_something" function will need to accept a variant type, or you can modify your loop to something like this:

对于每个循环结构,在集合对象周围设计更多。一个为. .每个循环需要一个变体类型或对象。由于您的“元素”变量是作为一个变体输入的,所以您的“do_something”函数将需要接受一个变体类型,或者您可以将循环修改为以下内容:

Public Sub Example()

    Dim sArray(4) As String
    Dim i As Long

    For i = LBound(sArray) To UBound(sArray)
        do_something sArray(i)
    Next i

End Sub

#3


6  

I use the counter variable like Fink suggests. If you want For Each and to pass ByRef (which can be more efficient for long strings) you have to cast your element as a string using CStr

我使用Fink建议的计数器变量。如果您想要每个并通过ByRef(可以为长字符串更有效),您必须使用CStr将您的元素转换为字符串。

Sub Example()

    Dim vItm As Variant
    Dim aStrings(1 To 4) As String

    aStrings(1) = "one": aStrings(2) = "two": aStrings(3) = "three": aStrings(4) = "four"

    For Each vItm In aStrings
        do_something CStr(vItm)
    Next vItm

End Sub

Function do_something(ByRef sInput As String)

    Debug.Print sInput

End Function

#4


3  

what about this simple inArray function:

那么这个简单的inArray函数呢?

Function isInArray(ByVal stringToBeFound As String, ByVal arr As Variant) As Boolean
For Each element In arr
    If element = stringToBeFound Then
        isInArray = True
        Exit Function
    End If
Next element
End Function

#1


77  

Element needs to be a variant, so you can't declare it as a string. Your function should accept a variant if it is a string though as long as you pass it ByVal.

元素需要是一个变体,所以不能将其声明为字符串。如果是字符串,那么函数应该接受一个变体,只要传递它为ByVal。

Public Sub example()
    Dim sArray(4) As string
    Dim element As variant

    For Each element In sArray
        do_something (element)
    Next element
End Sub


Sub do_something(ByVal e As String)

End Sub

The other option is to convert the variant to a string before passing it.

另一个选项是在传递变量之前将其转换为字符串。

  do_something CStr(element)

#2


25  

A for each loop structure is more designed around the collection object. A For..Each loop requires a variant type or object. Since your "element" variable is being typed as a variant your "do_something" function will need to accept a variant type, or you can modify your loop to something like this:

对于每个循环结构,在集合对象周围设计更多。一个为. .每个循环需要一个变体类型或对象。由于您的“元素”变量是作为一个变体输入的,所以您的“do_something”函数将需要接受一个变体类型,或者您可以将循环修改为以下内容:

Public Sub Example()

    Dim sArray(4) As String
    Dim i As Long

    For i = LBound(sArray) To UBound(sArray)
        do_something sArray(i)
    Next i

End Sub

#3


6  

I use the counter variable like Fink suggests. If you want For Each and to pass ByRef (which can be more efficient for long strings) you have to cast your element as a string using CStr

我使用Fink建议的计数器变量。如果您想要每个并通过ByRef(可以为长字符串更有效),您必须使用CStr将您的元素转换为字符串。

Sub Example()

    Dim vItm As Variant
    Dim aStrings(1 To 4) As String

    aStrings(1) = "one": aStrings(2) = "two": aStrings(3) = "three": aStrings(4) = "four"

    For Each vItm In aStrings
        do_something CStr(vItm)
    Next vItm

End Sub

Function do_something(ByRef sInput As String)

    Debug.Print sInput

End Function

#4


3  

what about this simple inArray function:

那么这个简单的inArray函数呢?

Function isInArray(ByVal stringToBeFound As String, ByVal arr As Variant) As Boolean
For Each element In arr
    If element = stringToBeFound Then
        isInArray = True
        Exit Function
    End If
Next element
End Function