Arrays and Dynamic Arrays in VB.net

By: Steven Holzner Emailed: 1588 times Printed: 2037 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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)


VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(4)


1. View Comment

To the,
sir/mam, i want to do java certification course.kindly suggets me how can i do it


View Tutorial          By: CERTIFICATION OF JAVA at 2011-03-03 02:27:05
2. View Comment

thanks for he info, was looking everywhere for someone to explain it in simply easy to understand terms, cheers!

View Tutorial          By: John at 2011-05-16 11:28:54
3. View Comment

Thanks so much for the tutorial!! Was extremely useful to me.

(The CAPCHA was too difficult, had to refresh several times)


View Tutorial          By: Debarupa at 2012-05-10 20:23:28
4. View Comment

you really dont know how much i want to thank u...
Really thanks a lot


View Tutorial          By: Angana Sengupta at 2013-01-23 18:34:51

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions 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
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
Your first VB.NET Crystal Reports - A step by step guide
Arrays and Dynamic Arrays in VB.net
How to export from database to excel using VB.net
How to export from DataGridView to excel using VB.net
How to create an XML file in VB.NET using Dataset
For Each…Next Loop in VB.net
Using Resume Next and Resume Line in VB.net
Visual Basic Statements
Using On Error GoTo 0 in VB.net
File Extensions Used in VB .NET
Using Select Case in VB.net
Do Loop in VB.net
While Loop in VB.net
How To Connect To A MySql Database in VB.net
How to Open and Edit Cells in an Excel 2007 file in VB.NET
Most Emailed Articles (in VB.net)
“Using If with And” and Comparing two integers using If
How to find IP Address of Host using .net code
While Loop in VB.net
Export Chart from Excel 2007 as Picture file in VB.NET 2005
Using Excel 2007 Chart VB.net Picture Box
How to export from DataGridView to excel using VB.net
Multi Threaded Server Socket Programming in VB.net
Using Finally in VB.net
The Select Case statement in VB.net
The Option and Imports Statements in VB .NET
Arrays and Dynamic Arrays in VB.net
Handling Strings in VB.net
Operators in VB.net
If…Else Statements in VB.net
Do Loop in VB.net