|
Hot-Link Menu in Right-Side Column |
Last Updated 6-30-2010 |
||||||||||||||||||||||||||||||||||||||||||||||
|
Visual Basic Dynamic Array IntroductionA dynamic array is an array that changes size while the program is running. Unlike the static array in the example above, dynamic arrays do not specify their size when they are declared. Unless you are certain the size of an array won't change, (like 7 days in a week), it's best to use dynamic arrays, so your program can take full benefit of any enhancements added to the system the program is running on. Also, dynamic arrays can shrink in size when your program doesn't need so much memory, allowing other applications to utilize the system's resources. Visual Basic Dynamic Array DeclarationGeneral Syntax (Dim, Private or Public) ArrayName() As DataType(i.e. int, double, or object) For example, the following statement declares a dynamic array to store an unknown number of names. The declaration informs the compiler that there is a dynamic array with a specified name in the program to store String data. No memory is allocated at declaration time: Dim EmployeeName () As String The Keyword Dim is used to declare a local array, Keyword Private is used to declare module-scope arrays, and Keyword Public is used to declare global-scope arrays.
Visual Basic Resizing a Dynamic Array - Resetting DataAt declaration time, no memory is allocated for a dynamic array. In order to store values in a dynamic array, the array must be resized to prepare the array to accept data. There are two ways to resize a Visual Basic dynamic array. ReDim: The keyword ReDim is used to resize a dynamic array to the desired size, while
resetting the existing values in an array to their default values (0 for numeric arrays, and False for Boolean Arrays). An
array can be resized as many times as necessary during the program execution to a bigger or smaller size. The general syntax for
ReDim is: ReDim ArrayName(NewArraySize) Where ArrayName is the name of the Visual Basic dynamic array, and NewArraySize is a whole number greater than or equal to 0, specifying the size of the array. After ReDim, the dynamic array will have NewArraySize + 1 elements. Create the Form_Load method by double-clicking on the blank form as indicated in the diagram below:
Enter the code in the code window as indicated in the diagram below:
Let's take a look at the code before we run the program:
Set a breakpoint as indicated in the diagram below:
Make sure to point to the column just left of the code window. This is the Set/Clear Breakpoint column. The RED dot indicates that a breakpoint has been set. When the code parser comes to the red dot program execution will be suspended while the program developer observes the variable status. Start the debugger by pressing the right triangle as indicated in the diagram below:
The program should execute until the parser reaches our Red Dot in the Breakpoint Column. The Red Dot should have a yellow right arrow (→) pointing to the line the program is ready to execute. The line waiting to be executed will be highlighted as indicated in the diagram below:
In order to continue this guide and completing this debugging session, 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 |