It is NOT fixed, numbers initialized with ReDim _Preserve do not start at 0 here is code that proves it:
UDT test 1 is run through code where we don't assume variables start at 0 or ""
How it's supposed to go nice and regular pattern.
UDT test 2 is run through code where we do assume variables are 0 and add or concat to "initial" value.
When variables aren't initialized to 0 or "" get big mess!!!
Surprisingly to me, this test worked fine for just concat new string to "initial" string (after ReDim _Preserve) and didn't have problems until added new integer to "initialized" integer.
So I was mistaken when Luke said it was fixed for numbers and fixed strings or it has since become unfixed.
Code: (Select All)
_Title "UDT assignments with ReDim _Preserve Test" ' b+ 2023-04-12
Type MYTYPE
INT1 As Integer
INT2 As Integer
INT3 As Integer
INFO As String
End Type
ReDim MYVARIABLE(1 To 1) As MYTYPE
Dim As Integer i
For assumeZeroedVariables = 0 To 1
For i = 1 To 100
If i > UBound(MYVARIABLE) Then ReDim _Preserve MYVARIABLE(1 To UBound(MYVARIABLE) + 10) As MYTYPE
If assumeZeroedVariables = 0 Then ' we don't assume new Redim _preserve variables are 0 or ""
' direct assignments
MYVARIABLE(i).INT1 = (i Mod 6) + 1
MYVARIABLE(i).INT2 = ((i + 1) Mod 6) + 1
MYVARIABLE(i).INT3 = ((i + 2) Mod 6) + 1
MYVARIABLE(i).INFO = String$(MYVARIABLE(i).INT1, "X")
' works fine!!
Else ' assume variables are 0 or ""
' aha!!! now we get trouble!!! assuming all new variables are 0 or ""
MYVARIABLE(i).INT1 = MYVARIABLE(i).INT1 + (i Mod 6) + 1
MYVARIABLE(i).INT2 = MYVARIABLE(i).INT2 + ((i + 1) Mod 6) + 1
MYVARIABLE(i).INT3 = MYVARIABLE(i).INT3 + ((i + 2) Mod 6) + 1
MYVARIABLE(i).INFO = MYVARIABLE(i).INFO + String$(MYVARIABLE(i).INT1, "X")
' Result is TROUBLE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
End If
Next
ReDim _Preserve MYVARIABLE(1 To 100) As MYTYPE
' display results
For i = LBound(MYVARIABLE) To UBound(MYVARIABLE)
Print i, MYVARIABLE(i).INT1, MYVARIABLE(i).INT2, MYVARIABLE(i).INT3, MYVARIABLE(i).INFO
If i Mod 20 = 0 Then Print "...zzz": Sleep: Cls
Next
ReDim MYVARIABLE(1 To 1) As MYTYPE ' reset for next test
Next
UDT test 1 is run through code where we don't assume variables start at 0 or ""
How it's supposed to go nice and regular pattern.
UDT test 2 is run through code where we do assume variables are 0 and add or concat to "initial" value.
When variables aren't initialized to 0 or "" get big mess!!!
Surprisingly to me, this test worked fine for just concat new string to "initial" string (after ReDim _Preserve) and didn't have problems until added new integer to "initialized" integer.
So I was mistaken when Luke said it was fixed for numbers and fixed strings or it has since become unfixed.
b = b + ...