Sub / Function array parameter altered

后端 未结 4 1706
一整个雨季
一整个雨季 2021-01-22 19:14

I have a Sub with an array of strings as parameter:

Private Sub des(ByVal array() As String)

    Dim i As Integer

    For i = 0 To UBound(array)
        array(         


        
4条回答
  •  孤城傲影
    2021-01-22 19:48

    Try this:

        Dim i As Integer
    
        Dim array2() As String
        array2 = array.Clone()
    
        For i = 0 To UBound(array2)
            array2(i) = "hy"
        Next
    

    The key difference is the .Clone(), that actually makes a shallow copy of array, and changing the values in array2 will no longer affect your str value in the main code.

提交回复
热议问题