?
Solved

VB.NET Multi Demensional Arrays

Posted on 2014-01-08
11
Medium Priority
?
249 Views
Last Modified: 2014-01-08
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!!!
0
Comment
Question by:slightlyoff
11 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39765696
Use GetUpperBound

for x as integer= 0 to TestArray.GetUpperBound(0)
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 39765733
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

        MsgBox(TestArray.GetUpperBound(0))
    End Sub
End Class

Open in new window

0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 39765738
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)
    Try
        ' Access the elements of the array and if an exception is happens it leaves the loop.
    Catch ex As Exception
        Exit For
    End Try
Next
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39765749
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
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 39765760
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!
0
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 1000 total points
ID: 39765845
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 = ""}
ListOfItems.Add(It)

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

Open in new window

0
 
LVL 1

Author Closing Comment

by:slightlyoff
ID: 39765872
Thank you for your feed back and help!  I appreciate it!
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39765885
Always welcome
0
 
LVL 40
ID: 39765947
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)
		Next

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.
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 39765964
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question