Arrays and Dynamic Arrays in VB.net
By: Steven Holzner in VB.net Tutorials on 2008-11-25
It's time to start coding that database program. But wait a moment-how are you going to handle the data? It's just a simple program, so you don't want to start tangling with the full database techniques. An array would be perfect; how do you set one up?
Arrays are programming constructs that let you access your data by numeric index. To dimension arrays, you can use Dim (standard arrays), ReDim (dynamic arrays), Static (arrays that don't change when between calls to the procedure they're in), Private (arrays private to the form or module they're declared in), Protected (arrays restricted to a class or classes derived from that class), Public (arrays global to the whole program), and more as discussed in the topic "Declaring Variables." I'll start with standard arrays.
Standard Arrays
You usually use the Dim statement to declare a standard array; here are a few examples of standard array declarations:
Dim Data(30) Dim Strings(10) As String Dim TwoDArray(20, 40) As Integer Dim Bounds(10, 100)
The Data array now has 30 elements, starting from Data(0), which is how you refer to the first element, up to Data(29). 0 is the lower bound of this array, and 19 is the upper bound (following the lead of Java, in VB .NET, the lower bound of every array index is 0, and you can no longer use the Option Base statement or To keyword that used to be available to set custom lower bounds). The Bounds array has two indices, one of which runs from 0 to 9, and the other of which runs from 0 to 99.
I can treat an array as a set of variables accessible with the array index, as here, where I'm storing a string in Strings(3) (that is, the fourth element in the array) and then displaying that string on the console:
Dim Data(30) Dim Strings(10) As String Dim TwoDArray(20, 40) As Integer Dim Bounds(10, 100) Strings(3) = "Here's a string!" System.Console.WriteLine(Strings(3))
You can also initialize the data in an array if you don't give an array an explicit size; here's the syntax to use, where I'm initializing an array with the values 10, 3, and 2:
Dim Data() = {10, 3, 2}
Dynamic Arrays
You can use the Dim statement to declare an array with empty parentheses to declare a dynamic array. Dynamic arrays can be dimensioned or redimensioned as you need them with the ReDim statement (which you must also do the first time you want to use a dynamic array). Here's how you use ReDim:
ReDim [Preserve] varname(subscripts)
You use the Preserve keyword to preserve the data in an existing array when you change the size of the last dimension. The varname argument holds the name of the array to (re)dimension. The subscripts term specifies the new dimension of the array.
This is one of those topics that is made easier with an example, so here's an example using dynamic arrays, in which we declare an array, dimension it, and then redimension it:
Dim DynaStrings() As String ReDim DynaStrings(10) DynaStrings(0) = "String 0" 'Need more data space! ReDim DynaStrings(100) DynaStrings(50) = "String 50"
You can find the upper bound of an array with the UBound function, which makes it easy to loop over all the elements in an array using a For loop like this: For intLoopIndex = 0 To UBound(intArray)-
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Throwing an Exception in VB.net
Comments