Solved

VB.NET Multi Demensional Arrays

Posted on 2014-01-08
11
240 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
Comment Utility
Use GetUpperBound

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

Author Comment

by:slightlyoff
Comment Utility
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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
Comment Utility
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
 
LVL 13

Expert Comment

by:Jesus Rodriguez
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 250 total points
Comment Utility
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 13

Expert Comment

by:Jesus Rodriguez
Comment Utility
0
 
LVL 1

Author Closing Comment

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

Expert Comment

by:Jesus Rodriguez
Comment Utility
Always welcome
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now