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(1,0)="Toy 2"
TestArray(2,0)="Toy 3"
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!!!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jesus RodriguezIT ManagerCommented:
Use GetUpperBound

for x as integer= 0 to TestArray.GetUpperBound(0)
slightlyoffAuthor Commented:
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:

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

    End Sub
End Class

Open in new window

Fernando SotoRetiredCommented:
Seeming that you created an Array of 200 elements at the beginning each one of those elements are set to Nothing. So to loop through them without throwing an exception you can put the code in a Try / Catch block.

For x As Integer = 0 To uBound(TestArray)
        ' Access the elements of the array and if an exception is happens it leaves the loop.
    Catch ex As Exception
        Exit For
    End Try
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Jesus RodriguezIT ManagerCommented:
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)
        if isnothing(TestArray(x,0)) then
            Exit For
        End If
    Catch ex As Exception
    End Try
slightlyoffAuthor Commented:
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!
Jesus RodriguezIT ManagerCommented:
The other way is through a list of object
'Create a Class like this
Public Class ItemsOnList
        Dim Item_ID As String = String.Empty
        Dim ItemValue_Text As String = String.Empty
End Class

'In your code declare this
Public ListOfItems As New List(Of ItemsOnList)

'To add something to your list will be
Dim It As ItemsOnList = New ItemsOnList With {.Item = "1", .ItemValue = ""}

'To Go through the list
 For each Itm as ItemOnList in ListOfItems
   'Itm(0) will hold the Item
   'Itm(1) will hold the Value

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slightlyoffAuthor Commented:
Thank you for your feed back and help!  I appreciate it!
Jesus RodriguezIT ManagerCommented:
Always welcome
Jacques Bourgeois (James Burger)PresidentCommented:
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:
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)

Open in new window

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.
slightlyoffAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.