I am trying to make an array of an array of a custom type.
VB.NET:
Public Class playDetail
Public Property Name as string
Public Property Url as string
End Class
Dim play1 as playDetail()
Dim play2 as playDetail()
Dim play3 as playDetail()
how do i declare an array that store play1,2 and 3?
The first thing to note is that when you declare a variable as a Class Type then you must remember to instantiate that object in memory to be able to use it. You do that using the New Keyword so your variable declarations should look something like this:-
VB.NET:
Dim play1 As New playDetail
Dim play2 As New playDetail
Dim play3 As New playDetail
To create a collection of PlayDetail objects I would recommend using a List(Of T) rather than an Array. Simply put, a List is just a more flexible Array. i.e:-
VB.NET:
Dim myPlayList As New List(Of playDetail)
You can then add your objects to your List. i.e:-
VB.NET:
With myPlayList
.Add(play1)
.Add(play2)
.Add(play3)
End With
Finally, you can interact with your List using a For Loop. i.e:-
VB.NET:
For Each currentPlayDetail As playDetail In myPlayList
MsgBox(currentPlayDetail.Name)
Next
I think what you want is a List(Of List(Of playDetail)).
Dim playLists As New List(Of List(Of playDetail))
For Each playDetailList As List(Of playDetail) In playLists
For Each play As playDetail In playDetailList
' ...
Next
Next
I disagree with my colleagues. You should use a List(Of T) if you do, or may, need the functionality that it provides. If you only need the functionality of an array though, you should just use an array. It's not completely clear whether an array or collection is appropriate for you but we should not just assume that a collection is best.
An array would be best if you know exactly how many elements there will be and that number won't change. While "resizing" an array is possible, it's something you should generally avoid. I use the quotes because, in actual fact, an array object is never resized, but rather a new array is created and the elements copied from the old one. Using a collection is not a way to avoid that though. A List(Of T) uses an array internally and it does resize that array as the collection grows. It just does it automatically and in a generally more efficient manner.
Assuming that an array is the better option, an array of arrays is a jagged array. It's like a multi-dimensional array in some ways but different in others. Most importantly, in a 2D array, all "rows" are the same length and all "columns" are the same height while a jagged array is so named because the "rows" can be different lengths, given it a jagged appearance when written out. A 2D array is a single array object while a jagged array is a 1D array containing other 1D arrays.
So, using a jagged array would look something like this:
'An array containing three other arrays.
Dim jaggedArray As Integer(2)()
jaggedArray(0) = New Integer() {1, 2, 3}
jaggedArray(1) = New Integer() {1, 2, 3, 4, 5}
jaggedArray(2) = New Integer() {1, 2, 3, 4}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.