Fatir Rizky
Fatir Rizky

Reputation: 67

List of <String> Loop short

this is my New List (Of String)

    Dim NChooseKListX1, NChooseKListX2, NChooseKListX3, NChooseKListX4, NChooseKListX5, NChooseKListX6, NChooseKListX7, NChooseKListX8,
    NChooseKListX9, NChooseKListX10, NChooseKListX11 As New List(Of String)()

Code: How can I make NChooseKListX after X be its value (PositionalNCKX) like NChooseKListX (PositionalNCX) without writing the value so many times.

  If strNCKXDigit(PositionalNCKX - 1) = strNCKXDigitA(0) Then
                    NChooseKListX (the value must be changed according to PositionalNCKX) .Add(NChooseKLTxt.Lines(RWNCKlines))
                ElseIf strNCKXDigit(PositionalNCKX - 1) = strNCKXDigitA(1) Then
                    NChooseKListX (the value must be changed according to PositionalNCKX).Add(NChooseKLTxt.Lines(RWNCKlines))
     End If

Upvotes: 0

Views: 36

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

Any time you have variable names that are different only by a number and you wish you could somehow get VB to let you use some changing number to access the differnet variable names, you have.. an array!

Or a collection, but let's do this with an array:

   Dim NChooseKListX(10) As List(Of String)

   
   'see how now we access them by index integer. 
   'This loop initializes the array slots so they are all new lists
   'It is necessary otherwise NChooseKListX(...) will be Nothing,
   'so don't throw this loop away thinking it's just a demo; you need it
   For i = 0 to 10 
 
     NChooseKListX(i) = New List(Of String)

   Next i

Remember that arrays are zero based in .net so the first one you called NChooseKListX1 is NChooseKListX(0), all the way up to NChooseKListX(10) (your NChooseKListX11)

Now you can address these lists by some integer variable like PositionalNCKX but remember it has to range between 0 and 10 so you might need to minus some number off it

Upvotes: 1

Related Questions