Sub / Function array parameter altered

后端 未结 4 1811
一整个雨季
一整个雨季 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 20:07

    Arrays are reference types. That means that when you pass an Array to your function, what is passed is always a reference, not a copy of the array. The Array in your function refers to the same array object as the Array in your calling code.

    The same thing happens when you do the assign (it is not a copy!) in your second example: all you've done is make yet another reference to the same object. That is why Boeckm's solution works -- the Clone() call does make a new array and assign it values which are copies of the values in the original array.

提交回复
热议问题