slightlyoff
asked on
VB.NET Multi Demensional Arrays
If I set up an array:
Public TestArray(200,2) and I fill it with 3 items
TestArray(0,0)="Toy 1"
TestArray(0,1)=4.99
TestArray(1,0)="Toy 2"
TestArray(1,1)=5.99
TestArray(2,0)="Toy 3"
TestArray(2,1)=4.50
How do I know how many items are in the array?
What's the best way to loop through this array without looping through empty array?
(I was using for x = 0 to uBound(TestArray), but that loops through all 200)
Thanks for your help!!!
Public TestArray(200,2) and I fill it with 3 items
TestArray(0,0)="Toy 1"
TestArray(0,1)=4.99
TestArray(1,0)="Toy 2"
TestArray(1,1)=5.99
TestArray(2,0)="Toy 3"
TestArray(2,1)=4.50
How do I know how many items are in the array?
What's the best way to loop through this array without looping through empty array?
(I was using for x = 0 to uBound(TestArray), but that loops through all 200)
Thanks for your help!!!
ASKER
Thanks for the quick reply!
I'm probably missing something, but when I do that I still loop through all 200 elements, even though only 3 are in the array:
I'm probably missing something, but when I do that I still loop through all 200 elements, even though only 3 are in the array:
Public Class Form1
Public TestArray(200, 2)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TestArray(0, 0) = "Toy 1"
TestArray(0, 1) = 4.99
TestArray(1, 0) = "Toy 2"
TestArray(1, 1) = 5.99
TestArray(2, 0) = "Toy 3"
TestArray(2, 1) = 4.5
MsgBox(TestArray.GetUpperBound(0))
End Sub
End Class
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As Fernando said before, You never will know if the array if populated completed or not because you pre-dimension this what you can do is
For x As Integer = 0 To uBound(TestArray)
Try
if isnothing(TestArray(x,0)) then
Exit For
End If
Catch ex As Exception
End Try
Next
For x As Integer = 0 To uBound(TestArray)
Try
if isnothing(TestArray(x,0)) then
Exit For
End If
Catch ex As Exception
End Try
Next
ASKER
I guess that's the thing - I don't want to dimension it at 200 - but I don't know how many I will need (but I know it'll be less than 200). Is there a better way?
Thanks for your help!
Thanks for your help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Look at this examples on creating list of objects
http://visualbasic.about.com/od/usingvbnet/a/genericlist.htm
http://forums.asp.net/t/1609931.aspx
http://www.dotnetheaven.com/article/creating-a-list-of-objects-using-vb.net
http://visualbasic.about.com/od/usingvbnet/a/genericlist.htm
http://forums.asp.net/t/1609931.aspx
http://www.dotnetheaven.com/article/creating-a-list-of-objects-using-vb.net
ASKER
Thank you for your feed back and help! I appreciate it!
Always welcome
Jesus suggestion of working with a list is probably the best solution, but his way of implementing it is not the best.
The class uses fields instead of properties, and properties are better for a few reasons.
He also uses a String to record the numeric value, which takes up more resources and might need you to force conversions when you use the class.
And although the shortcut he uses to pass data to the constructor works, it is not as efficient as using a real constructor.
His way of accessing the items in the loop will not work.
I would thus modify his code this way:
If you have a lot of data however, arrays sometimes offer a lot more performance. I would go for a collection first, and if you find out that you have performance problems, look for "Dynamic Arrays" in the documentation.
The class uses fields instead of properties, and properties are better for a few reasons.
He also uses a String to record the numeric value, which takes up more resources and might need you to force conversions when you use the class.
And although the shortcut he uses to pass data to the constructor works, it is not as efficient as using a real constructor.
His way of accessing the items in the loop will not work.
I would thus modify his code this way:
Public Class ItemsOnList
Public Property Item As String
Public Property Value As Double
Public Sub New(item As String, value As Double)
Me.Item = item
Me.Value = value
End Sub
End Class
'In your code work like this
Dim Inventory As New Collections.Generic.List(Of ItemsOnList)
'To add something to your list will be
Inventory.Add(New ItemsOnList("Toy 1", 4.99))
Inventory.Add(New ItemsOnList("Toy 2", 5.99))
'...
'To access the value for a specific item:
MessageBox.Show(Inventory(2).Item & " costs " & Inventory(2).Value)
'To Go through the list
For Each entry As ItemsOnList In Inventory
MessageBox.Show(entry.Item & " costs " & entry.Value)
Next
Working with a collection (List is a Collection) makes it easier when you need to add, insert or remove values on the fly.If you have a lot of data however, arrays sometimes offer a lot more performance. I would go for a collection first, and if you find out that you have performance problems, look for "Dynamic Arrays" in the documentation.
ASKER
Thank you for your help! That makes a lot of sense. I started programming with Classic ASP and arrays have always been my first thought when thinking of how to deal with a list of items.
for x as integer= 0 to TestArray.GetUpperBound(0)