Interviewer Insights  
About Us  
Comments, Suggestions  
  or General Feedback  
Interested in writing for  
Tech Talk Tips?  

 

 


Application Developer
Using arrays in Visual Basic

by Mark Horninger


Arrays are types of variables used in Visual Basic. All Visual Basic programmers, ranging in experience from beginner to advanced, need to have an understanding of arrays. An advantage to using arrays is the ability to refer to a group or series of variables by one name. This can make programming more efficient. The following set of questions will help determine the level of expertise a programmer has with arrays. A mid level programmer should be able to answer all of these questions correctly. A junior level programmer should be able to answer at least 7 of these questions correctly.

In the following example:

Dim Z(3)

How many elements are in the array?

Answer: 4, elements 0 through 3.
The above array is a one-dimensional array consisting of 4 elements. Arrays can be multi-dimensional, as shown in the following question.


In the following example:

Dim X(1,2)

How many elements can be stored?

Answer: 6
This is a 2 dimensional array. A graphical representation of the array looks like:

1,0
1,1
1,2
0,0
0,1
0,2

(each box represents an element in the array) You can have up to 32 dimensions in an array [x(1,2,3,...32)].

Arrays can also be declared using the following syntax:

Dim X(100 To 200) As Integers


What will happen if the following code is executed?

Dim X(100 To 200) As Integer
    X(5) = 4

Answer: This would yield a subscript out of range error. The lower bound of the array is 100 and 5 is less than that. Declaring an array using the to syntax creates a lower and upper boundary. If the candidate answers that the 5th element is set to 4 this is incorrect.


I need to use an array to hold information but I'm not sure of the size. What should I do?

Answer: Use a dynamic array. Dynamic arrays can be resized programmatically using the ReDim statement.


How do I resize a dynamic array?

Answer: Use the ReDim statement. The ReDim statement allows you to resize an array on the fly. The following example demonstrates resizing an array first to 100 elements then to 1000 elements:

Dim X() as Integer
ReDim X(100)
'
'
'
ReDim X(1000)


What is the statement LBound(x) used for?

Answer: The LBound statement is used with an array to determine the lower boundary of an array. This is particularly useful with dynamic arrays.

In the following code fragment LBound(X) = 10

DIM X(10 to 400)
RMsgbox LBound(X)


What is the statement UBound(x) used for?

Answer: The UBound statement is used with an array to determine the upper boundary of an array. This is particularly useful with dynamic arrays.

In the following code fragment LBound(X) = 400

DIM X(10 to 400)
RMsgbox UBound(X)


Whenever I re-dimension an array with the ReDim statement all of the information in the array is lost. How can I prevent this from happening?

Answer: Use the preserve keyword with the ReDim statement. The preserve statement will keep the information from being erased. Note that in a multi-dimensional array only the last element can be resized if you use the preserve statement.


What is the Option Base statement used for?

Answer: Option Base sets the default lower boundary of an array. It can be set to either 1 or 0. If Option Base isn't set in Visual Basic, the default is 0. Setting the Option Base to anything other than 0 or 1 will yield an error.


Question 10: (somewhat advanced) I need to allocate an array for some information, it may contain as many as 10000 elements or as few as 2 elements. Should I just use an array of 10000 or use a dynamic array?

Answer: Since Visual Basic allocates memory for static arrays when they are created, in this case it would be best to use a dynamic array. There are some exceptions to this, as it does take time (processing time) to resize an array, but the general rule of thumb for this case is to use dynamic arrays.


About the author


Mark Horninger, A+, MCSE+I, MCSE, MCSD, MCDBA, is President and founder of Haverford Consultants Inc. (http://www.haverford-consultants.com), located in the suburbs of Philadelphia, PA. He develops custom applications and system engineering solutions, specializing primarily in Microsoft operating systems and Microsoft BackOffice products. He has over 12 years of computer consulting experience and has passed 31 Microsoft Certification Exams. During his career, Mark has worked on many extensive and diverse projects including database development, client server and web-based application development, training, embedded systems development and Windows NT and 2000 project rollout planning and implementations. Mark is a contributing author to the books: MCSE Windows 2000 Professional Study Guide, Designing SQL Server 2000 Databases for .NET Enterprise Servers, VB .NET Developers Guide and Configuring and Troubleshooting Windows XP Professional.


REVIEWNET is a trademark and service mark of ReviewNet Corporation © 1997-2002,
Jenkintown, PA - All Rights Reserved. This information is confidential and proprietary
to ReviewNet Corporation. Use, duplication or misappropriation is strictly prohibited.