|
Hot-Link Menu in Right-Side Column |
Last Updated 6-30-2010 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Visual Basic Debugger Part IIWe are now going to observe the Array Values in the Visual Basic Watch Window. To activate the Watch Window you will need to place the mouse pointer over the Variable Name in the Visual Basic Code Window. When the pointer is properly located over the Variable Name the contents of the variable will momentarily pop up on your screen. If all you need to do is perform a quick check of the variable contents, this should be adequate. However, if you need to follow the values in a variable as the program progresses through several breakpoints, you will want to enable the Visual Basic Watch Window. Right-Clicking on the variable name will bring up the Visual Basic Debugger Option Menu as indicated in the diagram below:
The Watch Window should appear at the bottom of the screen displaying the current value of the Data( ) array. Expand the array elements by pressing on the '+' sign to the left of the Data( ) variable name. Expanding the Data( ) array should appear as in the following diagram:
Expanding the Data variable in the Visual Basic Watch Window shows us the variable values of 10, 11, 12 as we would expect. Next, we need to set a second breakpoint, to see what happens after we change our array size. Since everything looks good, we can get rid of that Red Dot. Place the mouse pointer over the Red Dot and left-click so it disappears, and we don't have to stop there anymore. The yellow arrow is still there, but it will disappear after we resume program execution. To see what happens to the contents of the array after a ReDim statement, let's place another one of those Red Dots in the Breakpoint Column to the left of the I=I statement. Your code window should look like the diagram below:
Press the Green Triangle in the toolbar (Figure Array-17) to resume program execution. A new yellow arrow should appear to the left of the I=I statement. The Data variable has collapsed in the Watch Window so we need to re-expand it. Doing so gives us a screen that looks like the diagram below:
Looks like we lost all of our hard work, and our data got erased. Man, that can be annoying. Fortunately, those crafty developers at Microsoft felt the same way too. So they invented the ReDim Preserve statement. That way, we have the option of keeping our data or throwing it out if we don't like the way it looks. Let's change our ReDim statement by tacking the keyword Preserve after it, rerun the program and see if that doesn't straighten out our little annoyance. At this point, we need to terminate execution of the program. If you press the Green Triangle to finish the program, then press the 'X' in the upper right corner of the form or, just press the Blue Square in the toolbar, to immediately terminate the program without continuing as indicated in the diagram below:
Once you are back in the code window, place the keyword Preserve after the keyword ReDim as indicated in the diagram below:
Once the code has been modified, it is time to run the program and observe the array entries, to see if we indeed have preserved our data. Press the Green Triangle to start debugging the program. When the parser reaches the Red Dot expand Data in the watch window to see the contents of the array. The contents should appear as indicated in the diagram below:
Looks like it's pretty important to use that Visual Basic Preserve keyword, if you are intending on keeping your data through the ReDim statement. Array resizing works in both directions. If you perform a ReDim statement with a smaller array size parameter, the Array will also shrink in size accordingly. If you make the array smaller, you will lose the data in the elements with a higher index than the array size, even if you use the preserve statement. So, if you are going to shrink the array, make sure you don't need the data in the higher order elements.
In order to continue this guide and work with multidimensional arrays, 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 |