Skip to main content

What is debugging in VB?

Breakpoints and Debugging Tools

Actually bugs or errors are of two types. They are logic and syntax errors.

In VB.net syntax errors are notified by the interpreter while trying to run the program. 

In VB breakpoints are used to understand the logic errors in our program. 

Now we can look into Breakpoints and its concepts.

 

VB NET Breakpoints

A breakpoint is like a note to VB.NET to stop your programme at a particular place. You add one to your code by clicking in the margins. A brown circled then appears, indicating where the code will break. The following two images show how to add one:

                 Click in the margins to add a Breakpoint 

When you click in the margins, to the left of a line of code, a brown circle appears:

                   
                   A Breakpoint has been added  

Notice that the line where you want VB.NET to break is highlighted brown.
Run your programme, and click the button. You are immediately returned to the coding window. The place where you put the Breakpoint will now have a yellow arrow on top of the brown circle. The brown highlighted line will now be yellow:

The Breakpoint is now highlighted in yellow

The yellow highlight indicates where in your code VB.NET is. To continue checking your code, press F10 on your keyboard (you can also press F11, but this will jump into any Subs or Functions you've set up.)
The next line in your code will be highlighted:

The programme has advanced to the next line

The yellow arrow, and the yellow highlight, jump down one line. Press the F10 key again. Then hold you mouse on the letter variable. The value this variable currently holds will be displayed:

Hold your mouse over a variable

The first time round the loop, the value in letter is "e" (The "c" next to it means that the variable type is Character).
If the "e" of "Debugging" is getting checked first, what happened to the "D"? Straight away, this indicates a problem. And the problem is that the Substring method starts counting from zero. So halt your programme by clicking "Debug > Stop Debugging", or click the Stop icon on the toolbar. Change the line in question to this:
letter = strText.Substring(0)

Run your programme again, and click the button. When you are returned to your code, press the F10 key and check the value of the letter variable. It should now be this:

Hold your mouse over the variable again

This time, the code is catching the first letter of the word when the loop begins, and not the second one.

Is that it? Have we found the cause of our problems? Stop your programme. Click on the brown circle to get rid of the Breakpoint. Run it again, and see what happens.

The number of G's counted is still zero! So the logic error has not yet been tracked down. Create another Breakpoint at the same place, and try again.
You can continue pressing the F10 key until you've spotted the error. Or you can use another debugging tool - the Locals window.

While your programme is still in Debug mode (the yellow line will still be there, if it is), click Debug > Windows > Locals from the menu bar. You should see the following in the bottom left of your screen:

The locals window

Locals means "Local variables". That is, variables declared in this section of the code. The variables i, letter and LetterCount are showing in the window. The value of these variables is also displayed: 0, Nothing and 0. Press F10 and these values will change. This is the Locals window after one go round the loop:

Press the F10 key to advance the programme

The variable i is now 2; letter is "D", and LetterCount, is still 0. Keep pressing F10 and go round the loop a few times. What do you notice?
You should notice that the value in letter never moves on. It is "D" all the time. And that's why LetterCount never gets beyond 0.

Question asked on Quora.com
 

Comments

Popular posts from this blog

How can we lose weight by eating fruits and cereals?

Now we can split fruits and cereals in two parts. First we are going to look how fruits help in weight loss.Here the list of 11 fruits that help to loss wight. Fruit is nature's ready-made snack packed with vitamins, fiber, and other nutrients that support a healthy diet. Fruit is also generally low in calories and high in fiber, which may help you lose weight. In fact, eating fruit is linked to a lower body weight and a lower risk of diabetes, high blood pressure, cancer, and heart disease. Here are 11 of the best fruits to eat for weight loss. 1. Grapefruit Grapefruit is a cross between a pomelo and an orange and is commonly associated with dieting and weight loss.    Half a grapefruit contains just 39 calories but provides 65% of the reference daily intake (RDI) for of vitamin C. Red varieties also provide 28% of the RDI for vitamin A ( 1 Trusted Source ).  What's more, grapefruit has a low glycemic index (GI), which means it r...

Can I create tab control in vb.net without using toolbox control?

First we create a form in vb and we are going to add a tab by coding. Code is Here tab is created when form loads..... the output is as follows.. Now we can add some controls to each tab. in tab page1 a text box in tab page2 a combobox in tab page3 a listbox in tabpage4 a button. The code is The output is as follows

What are the properties of TextBox in Visual Basic?

         VB.Net - TextBox Control Text box controls allow entering text on a form at runtime. By default, it takes a single line of text, however, you can make it accept multiple texts and even add scroll bars to it. Let's create a text box by dragging a Text Box control from the Toolbox and dropping it on the form.     The Properties of the TextBox Control The following are some of the commonly used properties of the TextBox control − Sr.No. Property & Description 1 AcceptsReturn Gets or sets a value indicating whether pressing ENTER in a multiline TextBox control creates a new line of text in the control or activates the default button for the form. 2 AutoCompleteCustomSource Gets or sets a custom System.Collections.Specialized.StringCollection to use when the AutoCompleteSourceproperty is set to CustomSource. 3 AutoCompleteMode Gets or sets an option that controls how automatic compl...