Debugging

The Locals Window

The Locals Window allows you to see all the current variables and their values during Break Mode. It also shows all the controls and their properties. The Locals window is useful for checking values and properties while in break mode. For example, if you have the following procedure in your code and have a TextBox (Text1) and a command Button (Command1), and Visual Basic has entered Break Mode at the Stop Statement:

Sub Command1_Click()
    ' Declare the variables
    Dim gNum As Long
    Dim gString As String
    ' Fill the variables
    gNum = 1
    gString = Text1.Text
    Stop
End Sub

Now, show the Locals Window by selecting Locals Window from the View Window. This is shown below:


Locals Window

You will see that there are three items on the list. These are listed below

Me - This refers to the current form. You will notice that there is a + sign next to it. If you press this, you will see a list of all the current forms properties, along with any controls that are on the form. To see the properties of these controls, press the + sign next to them.

gNum - This is the variable that is declared inside the Command1_Click() procedure. Next to it you will see its value, which will be 1. If Visual Basic is in Break Mode outside of this procedure, this variable would not be listed. This is because it can only be accessed in the Command1_Click() procedure.

gString - This is the other variable in the Command1_Click() procedure. Next to it you will see its value, which will be the contents of Text1

There may also be other variables listed here. These are variables that have been declared Public, and can be accessed anywhere. To try this out, enter the following code and add two command buttons.

Public gPublicVar As String
Sub Command1_Click()
    ' Declare the variables
    Dim gNum As Long
    ' Fill the variables
    gNum = 1
    ' Fill the public variable
    gPublicVar = "hello"
    Stop
End Sub

Sub Command2_Click
    ' Declare the variables
    Dim gAnotherNum As Long
    gAnotherNum = 12
    Stop
End Sub

Press Command1 first. Visual Basic will enter break mode. In the Locals Window, both gNum (which is declared in the Command1_Click() procedure) and gPublicVar (which is declared at the top of the form, and can be accessed from any procedure) are listed. gAnotherNum is not listed as it can only be accessed from the Command2_click procedure.

Now, press F5 to resume code execution and press Command2. Visual Basic will enter break mode. In the Locals Window, gNum will not be shown, but both gAnotherNum (declared in this procedure) and gPublicVar (declared Publically) are listed.

Press the + sign next to Me, and then scroll down until you find Command2 and press the + sign next to that. This will now list all the properties of Command2, including its caption and its name. In the column farthest to the right there is a column listing the Type. Next to Command2 will be Command Button.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is anticipated with distaste, performed with reluctance, and bragged about forever.” - Dan Kaminsky