REDIM, TYPE, and STRING Woes
#9
It is NOT fixed, numbers initialized with ReDim _Preserve do not start at 0 here is code that proves it:
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. Sad
b = b + ...
Reply


Messages In This Thread
REDIM, TYPE, and STRING Woes - by TerryRitchie - 04-12-2023, 05:57 AM
RE: REDIM, TYPE, and STRING Woes - by mnrvovrfc - 04-12-2023, 10:24 AM
RE: REDIM, TYPE, and STRING Woes - by bplus - 04-12-2023, 03:50 PM
RE: REDIM, TYPE, and STRING Woes - by mnrvovrfc - 04-12-2023, 04:34 PM
RE: REDIM, TYPE, and STRING Woes - by DSMan195276 - 04-12-2023, 04:15 PM
RE: REDIM, TYPE, and STRING Woes - by mnrvovrfc - 04-12-2023, 04:33 PM
RE: REDIM, TYPE, and STRING Woes - by bplus - 04-12-2023, 04:41 PM
RE: REDIM, TYPE, and STRING Woes - by bplus - 04-12-2023, 05:59 PM
RE: REDIM, TYPE, and STRING Woes - by bplus - 04-12-2023, 06:09 PM
RE: REDIM, TYPE, and STRING Woes - by DSMan195276 - 04-12-2023, 10:14 PM
RE: REDIM, TYPE, and STRING Woes - by SMcNeill - 04-12-2023, 10:20 PM
RE: REDIM, TYPE, and STRING Woes - by DSMan195276 - 04-12-2023, 10:33 PM
RE: REDIM, TYPE, and STRING Woes - by Kernelpanic - 04-12-2023, 11:48 PM
RE: REDIM, TYPE, and STRING Woes - by DSMan195276 - 04-13-2023, 05:17 AM



Users browsing this thread: 1 Guest(s)