Это зависит от того, какую переменную вы передаете в MakeRoomForArrayItem
.Если ваш массив содержит переменную типа значения, такую как Integer или Boolean, он будет работать, потому что оператор присваивания
Set ItemArray(i + 1) = ItemArray(i)
копирует значение.Но если вы используете переменную, которая передается по ссылке, вы не копируете ее значение, а копируете ссылку на переменную.В этом случае вы, кажется, передаете переменную класса, которая будет передана по ссылке.
РЕДАКТИРОВАТЬ: Когда вы сделали вызов New CInterval
, вы фактически присвоили новую переменную вместо копирования ссылки на предыдущую переменную.Вот почему ваше исправление сработало.Без вашего исправления у вас был только один «слот» в памяти для хранения значения, но ваш массив ссылался на эту память несколько раз.После исправления у вас было столько «слотов» в памяти, сколько было вызовов New CInterval
, и каждый элемент массива ссылался на новую ячейку памяти.
Возможно, поможет следующий код:
Set interval1 = New CInterval ' you have a single CInterval instance
Set interval2 = New CInterval ' changes to interval1 or interval2 do not affect each other
Set interval3 = interval2 ' changes to interval3 also make changes to interval2,
' because they are the same object.
Dim arr as CInterval(3)
' Create a single array that has 3 elements,
' which will hold 3 references to CInterval instances.
' Those references may or may not be to the same actual CInterval instance.
Set arr(0) = interval1 ' the first element of the array references the first object instance
Set arr(1) = interval1 ' the second element of the array also references the first object instance
Set arr(2) = interval2 ' the third element of the array references the second object instance.
' Changes to this element will affect both interval2 and interval3, because they are references to the same object in memory.