|
Hot-Link Menu in Right-Side Column |
Last Updated 6-30-2010 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Visual Basic.NET ArraysIntroductionWithout using arrays, it is still possible to perform computations, process numeric data and so on. In some applications, one has to collect the data values, store them in the program, and be able to access each value later on. For example, consider an application designed for book keeping in a bookstore. It requires entering the name, ISBN, author's name and other data for each book, one at a time. The entered data should be stored in the computer's memory for certain processing required by the office. Without using arrays, you can write a program to get the data for one book, store it in variables and do some processing. But once the next book's data is entered, it will be stored in the same variables, hence erasing the previously stored data. What is the solution? Is it possible to declare enough variables to store the data for all the books in the store? That would be similar to the old-fashioned paper-based bookkeeping; besides it would be practically impossible. Luckily, programming languages have a data structure called arrays, which provides a solution for this kind of problem. By using arrays, many data values of the same type can be stored in the computer's memory under one single name. In other words, an array can be thought of as a variable that can store many data values of the same data type, instead of just a single value. Below are examples of arrays of different objects:
DefinitionAn array is a data structure that is used to store data values of the same data type in the computer's memory under one name. Like a variable, to use an array in a Visual Basic program, the array must be declared before it is used. Declaration of One-Dimensional ArrayAt declaration time, the name of the array, data type of the values that can be stored in it, and its size should be specified. To name an array, follow the same naming rules and standards as those for variables. Declaration syntax for VB.NET is as follows: Dim|Private|Public ArrayName(LastIndex) As DataType
Array IndexAt declaration time, enough memory is allocated to store values of the specified data type. Each of the allocated memory locations is referenced by a nonnegative Index value for that element. The Index of the first element in an array in Visual Basic.NET is always 0. The elements are accessed sequentially, as can be seen in the following examples: Example 1Declare an array to store the area codes of ten cities in Alaska. Do they have ten cities in Alaska? Assume that the area codes are three-digit whole numbers, i.e. (0, 1, 2, 3... 9): Dim AreaCode(10) As Integer With the above declaration, ten consecutive memory locations of two bytes each will be allocated in the computer's memory, and the array indexes proceed from 0 to 9, with the first element being 0, and the last element being 9. The following is a represenation of how the declaration Dim AreaCode(10) would appear in the computer's memory:
Notice that value of all the elements in an array in Visual Basic.NET are initialized to 0 when the array is decalred. Accessing Array ElementsEach array element can be used like a single variable of the same data type. To access an array element, state the name of the array with the element you want to access in parentheses. For example: AreaCode(0) will access the first element in the array described in Example 1 above.
Example 2Declare an array to store the prices of 5 video games. Dim Price(5) as float With the above declaration, five consecutive memory locations of four bytes each will be allocated in the computer's memory, and the array indexes proceed from 0 to 4, with the first element being 0, and the last element being 4. The following is a represenation of how the declaration Dim Price(5) would appear in the computer's memory:
Perform the following operations on this array:
Working with One-Dimensional ArraysArrays and loops go hand in hand. When working with arrays, one often needs to display the entire array, find the maximum or minimum value stored int the array, search for a certain value in the array, and so on. In such cases, a loop structure is needed. Example 3Perform the following operations in sequence:
Create the Form by following the steps below:
In order to continue this guide and finish Example #3 where we fill an Array with random values press the Button Below:
|
Home Visual Basic Introduction to VB.NET.NET Framework VS2008 IDE How VB is Compiled Start Visual Studio Windows Form App Save Your Work VB OOP ProgrammingVisual Basic Code Exit Code Button Event Code Coding RecommendationsIf/Then/Else Error List Window Comment Syntax Help Window Language Essentianl Built-In Data Types Declare Variables Declare ConstantsCode Arithmetic Expressions Assignment Statements Operator Precedence Type Casting Math Class String DeclarationConversion Functions Conversion Methods Formatting Functions String Formatting Variable Scope EnumerationsNullable Types Loop Constructs For Next LoopDo While Loop Do Until Loop Do...Loop-WhileDo...Loop-Until Exit Do | Exit For Do...LoopNested Loops Arrays Array DeclarationRnd( ) Function Listbox ControlKeyPressEventArgs Parallel Arrays Key Event ArgsDynamic Arrays Redimension ArraySet Breakpoint Start Debugger ReDim Preserve MultiDimensional Arrays DataGridView ControlLength and Sort Methods Structures Pad RightSplit Method IsNumeric Function Multiform Projects Add Form To Project Form Object Methods Form Show Method ShowDialog Method Form Close Method Form Accept Button Multiform Project Example ASP.NET Web Programming Create Data SourceConfigure Access Data Source Add Product Class Extract Local Database Data Order PageLoad VB CodeAdd New Web Page Set Start Page Display Cart Aspx CodeDisplay Cart Design View Sorted List Definition VB.NET Session State Create CartItem Class GetCartContents FunctionAdd To Cart Event Handler Remove Cart Item EventClear Cart Event Handler |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||