Solved

VB.NET Multi Demensional Arrays

Posted on 2014-01-08
11
243 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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 250 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 13

Expert Comment

by:Jesus Rodriguez
ID: 39765856
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET bulk update  of a table from another table 4 44
Help with LINQ and XML 10 36
vb.net help 4 29
Modal Popup Extender control 1 20
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

772 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