How can I use a for each loop on an array?

前端 未结 4 1548
南旧
南旧 2020-12-07 15:19

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         


        
4条回答
  •  孤城傲影
    2020-12-07 16:18

    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.

    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)
    

提交回复
热议问题