Please take a look at the following code, which is a simplified extract from my actual code:
-----
Public Sub Main()
Dim arrWhatever(9) As Whatever
Dim arrTemp(4) As String
For i As Integer = 0 To 9
arrWhatever(i) = New Whatever
'ReDim arrTemp(4) '-> When activated, correct inputs are passed. When deactivated, new inputs overwrite all previously passed inputs.
For j As Integer = 0 To 4
arrTemp(j) = "File" & 5 * i + j
Next j
arrWhatever(i).Inputs = arrTemp '-> When ReDim statement is NOT activated, for e.g. i=3, arrTemp is not only passed to arrWhatever(3).Inputs but also to arrWhatever(0).Inputs, arrWhatever(1).Inputs and arrWhatever(2).Inputs (i.e. all previous Whatever classes), which is not supposed to happen !!!
Next i
Debug.Print(arrWhatever(0).Inputs(0))
Debug.Print(arrWhatever(1).Inputs(0))
Debug.Print(arrWhatever(2).Inputs(0))
Debug.Print(arrWhatever(0).Inputs(1))
Debug.Print(arrWhatever(1).Inputs(1))
Debug.Print(arrWhatever(2).Inputs(1))
End Sub
Public Class Whatever
Public Inputs() As String
Public Outputs() As String
Public Sub GenerateOutputs()
'Generates Outputs from Inputs
End Sub
End Class
-----
I am generating an array of 10 Whatever classes and want to pass different strings to the public array Inputs() of each Whatever class.
I am creating a temporary array arrTemp with the strings I need and then pass this array to the public array Inputs() of each Whatever class.
1.) When I do NOT use the ReDim statement to delete all elements of my temporary array arrTemp, the following happens:
First loop (i=0):
The code fills arrTemp with the following strings: { "File0", "File1", "File2", "File3", "File4"}
Then the strings are passed to arrWhatever(0).Inputs
Second loop (i=1):
The code overwrites the previous values of arrTemp and fills it with the following strings: { "File5", "File6", "File7", "File8", "File9"}
Then the strings are passed to arrWhatever(1).Inputs
When this is being done, arrWhatever(0).Inputs is also filled with these news strings, overwriting its previous values, although the instruction for the program is clearly to pass the new strings only to arrWhatever(1).Inputs
For the whole program, debug output is as follows:
File45
File45
File45
File46
File46
File46
2.) When I DO use the ReDim statement to delete all elements of my temporary array arrTemp before filling it with new values, everything works perfectly.
For the whole program, debug output is as follows:
File0
File5
File10
File1
File6
File11
Question: Why do I need to use the ReDim statement for my program to work correctly?
Please do NOT question the architecture of my program. It's just a simplified version and there is a reason why the architecture is like it is.
All I would like to understand is why my program works perfectly with the ReDim statement but overwrites all previously passed inputs when I do NOT use the ReDim statement.
Any help would be higly appreciated. I find it scary NOT to understand the logic behind this phenomenon.
Thanks,
Alex
-----
Public Sub Main()
Dim arrWhatever(9) As Whatever
Dim arrTemp(4) As String
For i As Integer = 0 To 9
arrWhatever(i) = New Whatever
'ReDim arrTemp(4) '-> When activated, correct inputs are passed. When deactivated, new inputs overwrite all previously passed inputs.
For j As Integer = 0 To 4
arrTemp(j) = "File" & 5 * i + j
Next j
arrWhatever(i).Inputs = arrTemp '-> When ReDim statement is NOT activated, for e.g. i=3, arrTemp is not only passed to arrWhatever(3).Inputs but also to arrWhatever(0).Inputs, arrWhatever(1).Inputs and arrWhatever(2).Inputs (i.e. all previous Whatever classes), which is not supposed to happen !!!
Next i
Debug.Print(arrWhatever(0).Inputs(0))
Debug.Print(arrWhatever(1).Inputs(0))
Debug.Print(arrWhatever(2).Inputs(0))
Debug.Print(arrWhatever(0).Inputs(1))
Debug.Print(arrWhatever(1).Inputs(1))
Debug.Print(arrWhatever(2).Inputs(1))
End Sub
Public Class Whatever
Public Inputs() As String
Public Outputs() As String
Public Sub GenerateOutputs()
'Generates Outputs from Inputs
End Sub
End Class
-----
I am generating an array of 10 Whatever classes and want to pass different strings to the public array Inputs() of each Whatever class.
I am creating a temporary array arrTemp with the strings I need and then pass this array to the public array Inputs() of each Whatever class.
1.) When I do NOT use the ReDim statement to delete all elements of my temporary array arrTemp, the following happens:
First loop (i=0):
The code fills arrTemp with the following strings: { "File0", "File1", "File2", "File3", "File4"}
Then the strings are passed to arrWhatever(0).Inputs
Second loop (i=1):
The code overwrites the previous values of arrTemp and fills it with the following strings: { "File5", "File6", "File7", "File8", "File9"}
Then the strings are passed to arrWhatever(1).Inputs
When this is being done, arrWhatever(0).Inputs is also filled with these news strings, overwriting its previous values, although the instruction for the program is clearly to pass the new strings only to arrWhatever(1).Inputs
For the whole program, debug output is as follows:
File45
File45
File45
File46
File46
File46
2.) When I DO use the ReDim statement to delete all elements of my temporary array arrTemp before filling it with new values, everything works perfectly.
For the whole program, debug output is as follows:
File0
File5
File10
File1
File6
File11
Question: Why do I need to use the ReDim statement for my program to work correctly?
Please do NOT question the architecture of my program. It's just a simplified version and there is a reason why the architecture is like it is.
All I would like to understand is why my program works perfectly with the ReDim statement but overwrites all previously passed inputs when I do NOT use the ReDim statement.
Any help would be higly appreciated. I find it scary NOT to understand the logic behind this phenomenon.
Thanks,
Alex