Home > Blockchain >  Initialize Array after ReDim
Initialize Array after ReDim

Time:10-14

Is there a better way to do this? I do not need to preserve the data in the array.

ReDim Test(4)
For i = 0 to Test.Count()-1
    Test(i) = New MyObject
Next

CodePudding user response:

If you fill your array as shown below the ReDim is not necessary.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Test1() As Integer
    Test1 = {0, 1, 2, 3}
    For Each i In Test1
        Debug.Print(i.ToString)
    Next
    Dim Test2() As Coffee
    'I have a Coffee class with a parameterized constructor
    Test2 = {New Coffee("Folgers", 0), New Coffee("Pete's", 1), New Coffee("Tully's", 2), New Coffee("Green Mountain", 3)}
    For Each c In Test2
        Debug.Print(c.Name)
    Next
End Sub

CodePudding user response:

I found what I was looking for

test = Enumerable.Repeat(New MyObject, 4)
  • Related