• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » VB » VB Arrays

How Arrays are declared and initialized in Visual Basic

By Dinesh Thakur

A standard structure for storing data in any programming language is an array. Whereas individual variables can hold single entities, such as a number, date, or string, arrays can hold sets of related data.

 

An array has a name, as does a variable, and the values stored in it can be accessed by an index.

 

Declaring Arrays : Single Dimensional Array—Used to store long sequences of one dimensional data. Unlike simple variables, arrays must be declared with the Dim statement followed by the name of the array and the maximum number of elements it can hold in parantheses,

e.g. Dim Sal(15)

Multidimensional Array—To store the data in a tabular or matrix form, we need two dimensional array and to store sequences of multidimensional data we need multidimensional array.

In two dimensional array, first index represent the row and the second represents the column.

e.g. Dim Board(5,5) As Integer

 

Dynamic Arrays : Sometimes you may not know how large to make an array. Instead of making it large enough to hold the maximum number of data, you can declare a dynamic array. The size of a dynamic array can vary during the course of the program.

To create a dynamic array, declare it as usual with the Dim statement, but don’t specify its dimensions :

 

Dim DynArray()

Later in the program, when you know how many elements you want to store in the array, use the ReDim statement to redimension the array, this time with its actual size.

 

ReDim DynArr(UserCount)

Here UserCount is a user-entered value.

The ReDim statement can only appear in a procedure.

 

Multi-Dimesional Array

By Dinesh Thakur

 You may need to use two subscripts to identify tabular data, where data are arranged in rows and columns. To define a two-dimentional array or table, the Dim statement specfies the number of rows and columns in the array. The row is horizontal and the column is vertical.

 

Syntax:

Dim ArrayName ( [ LowerLimit To ] UpperLimit, [ LowerLimit To ] UpperLimit ) As Datatype

Example:

Dim strName ( 2, 3 ) As String

Dim strName ( 0 To 2 , 0 To 3 ) As string

Why would a list box control need both a ListBox property and an ItemData property

By Dinesh Thakur

List Index property is used to determine the array subscript. List Index property holds the position or index of the selected item in the list. We could then use this index with one minor change to access the group total. List index property always begins with 0.

 

VB offers another handy property for list boxes and combo boxes that can be a big help when working with indexes. The ItemData property can associate a specific number with each item in the list. Each element of the list property can have corresponding Item Data that does not change the list is sorted.

 

Name some situation in which it is important to perform validation when working with subscripted variables

By Dinesh Thakur

Sometimes, array are referred to as tables or subscripted variables, all referenced by the same name. When we use a control array, the individual controls are referenced as optCoffee(0) or optCoffee(1). The same notation is used with variable arrays. Therefore, in an array for storing names we may have strName(0), strName(1), strName(2) and so on.

Describe the logic of a table lookup

By Dinesh Thakur

Consider the eight scout troops and their ticket sales. Now the groups are not numbered 1 to 8, but 101,103,110,115,121,123,130 and 145.

The group number and the number of tickets sold are still input, and the number of tickets must be added to the correct total. But now we must do one more step – determine to which array element to add the ticket sales, using a table lookup.

 

The first step in the project is to establish a user-defined type with the group numbers and totals and the dimension an array of the new type. Before, any processing is done, load the group numbers into the table.

 

Place the following statements in the general declaration section of a form module:

 

Private Type GroupInfo

intNumber as Integer

intTotal as Integer

End Type

 

Dim mudtGroup(1 to 8) as GroupInfo

 

Then initialize the values of the array element by placing these statements into the form_load procedure:

mudtGroup(1).intNumber = 101

mudtGroup(1).intNumber = 103

mudtGroup(1).intNumber = 110

etc.

 

During the program execution the user still enters the group number and the number of tickets sold into textboxes. The technique used to find the subscript is called a table lookup.

 

 

Define the following terms

By Dinesh Thakur

a) Arrays:

A variable array can contain a list of values, similar to list box or combo box. VB actually stores the list property of a list box or a combo box in an array.

 

b) Element:

Each individual variable is called an element of the array. The individual elements are treated the same as any other variable and may be used in any statement, such as an assignment or a Printer.Print.

c) Subscript:

The subscript which may also be called an index inside the parenthesis is the position of the element within an array. Subscripts may be constants, variables, or numeric expressions. Although the subscripts must be integers, VB will round any non integer subscript.

d) Control array:

A control array is a group of controls that all have the same name. All controls in an array must be the same class. An advantage, of using control array, rather than independent controls, is that the controls share one Click event.

e) Subscripted Variables:

The subscript which may also be called an index inside the parenthesis is the position of the element within an array. Subscripts may be constants, variables, or numeric expressions. Although the subscripts must be integers, VB will round any non integer subscript. The real advantage of using an array is not realized until we use variable for subscripts in place of the constants.

strName(intIndex) = “”

Printer.Print strName(intIndex)

Primary Sidebar

Visual Basic Tutorial

Visual Basic Tutorial

  • VB - File Types Purpose
  • VB - Events
  • VB - Ole
  • VB - Controls Properties
  • VB - Project Steps
  • VB - Pretest Vs Posttest
  • VB - IDE
  • VB - Record Set
  • VB - Common Dialog Box
  • VB - Val Function
  • VB - Sub Vs Function Procedure
  • VB - Terms
  • VB - User Interface Elements
  • VB - Objects and Modules
  • VB - Variable’s Scope
  • VB - Message Box
  • VB - Data Vs Data-bound Control
  • VB - Branching Statements
  • VB - Do/Loop and For-Next Loop
  • VB - Grid Control
  • VB - Error Types
  • VB - Looping Constructs
  • VB - Relational Vs Logical Operator
  • VB - Control Purpose

Other Links

  • Visual Basic - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW