?
Solved

VB.NET Multi Demensional Arrays

Posted on 2014-01-08
11
Medium Priority
?
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 63

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
Industry Leaders: 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

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!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

764 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