htm11h
htm11h

Reputation: 1779

populating a 2 column multi dimensional array VB.net

I am having issues assinging values to a multi-dimensional array.

I am trying to build a 2 col, unlimited row array and populate with data from a data reader.

Dim tblArry(,) As String = {}

If reader.HasRows Then
  While reader.Read()
      ' Call Read before accessing data.
      Dim tempTbl As String = "tblLang" & reader.Item(0)
      Dim tempTblTwoLett As String = reader.Item(1)

      tblArry = New String(tempTbl, tempTblTwoLett)

' Tried this too....
tblArry = {New String(tempTbl, New String(tempTblTwoLett)} 
' and this...
tblArry = New String(tempTbl), New String(tempTblTwoLett)

When I use a Jagged Array I can get this to work. The Reader portion is working just fine, but the coding is just not clean for this problem.

Any suggestions would be appreciated.

Thanks

Upvotes: 1

Views: 3468

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

Use a List instead of an array.

Dim langs As New List(Of String())()

While reader.Read()
  Dim temp(1) As String
  temp(0) = "tblLang" & reader.Item(0)
  temp(1) = reader.Item(1)

  langs.Add(temp)
End While

.Net makes a distinction between array types and collections types. Array types are meant to have fixed sizes, so appending to the end like you want to do doesn't work well. Collections are meant to be more flexible.

Regardless, why in the world would you expect to append to the end of an array by assigning to it!? The best you could hope for there is that you replace the entire array.

Upvotes: 2

Related Questions