Page 1 of 1

Debugging How to use the tools Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 20 March 2009 - 03:26 PM

*
POPULAR

Debugging Skills

So coded your application and you have no errors but when you run it has an error or runs not like you expected.
This is a Logical Error where it debugging skills help.

The most important one is the use of breakpoints, these stop the execution of the program and allows you examine things to are occurring within your program;-
like the contents of variable
The stack.

To examine the contents of a variable you can hover over the variable name and a box appears with contents, but when you move the mouse it disappears.
For something a little more permanent you can right-click over the variable and add a watch, this allow you to view the contents as you step through the code.

Stepping Through Your Code
To Step Through your code there are three types of stepping
Step InTo F8 where the stepping, steps into subroutines, function, methods etc.
Step Over Shift + F8 where the stepping steps overs subroutines, function, methods etc.
Step Out Ctrl + Shift + F8 where the stepping, steps out of the current subroutines, function, methods etc.
On to the next code line directly over the call.

The Stack Ctrl + L
Another important tool is the stack visualizer, where you can see the depth of the function call.
Click on a level and the debugger takes to where in your code the function call was made.
[External Code] is code that was used by your program but you are unable access it because it likely to be OS / CLR operation.

These simple skills will help you become code debugging code-ninja :ph34r:

Is This A Good Question/Topic? 6
  • +

Replies To: Debugging

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 24 March 2009 - 08:15 AM

The code for this example was written by Tocon (See: Tocon's Original Posting).
They have kindly have allowed me to use their code as for an example of how debugging skills can help to find the cause of the problem.
Some how when it comes to display the contents of the array the program crash.


Attached Image
Added a Breakpoint to the start of the displayArray subroutine.

Attached Image
Added a Breakpoint to the For Loop, so that every time around the loop the program stops to let us examine things.

Attached Image
Having run the program and in a student, then click on the List by Students button.
The Program has pause at the first breakpoint. Underneath the code window you see the stack window.
Which says you're in the displayArray subroutine, and you got here via the clicking on the ButtonList Button on MainForm1

Attached Image
Let watch the contents of the array called "theArray", by highlight the variable then right click to bring up the menu the selecting Add Watch
Attached Image
This is the expanded view of the contents of theArray, (Warning: Do this on large array may cause Visual Studio to become unresponsive).

Stepping through the array F8, eventually the program throw the error message below
Attached Image

By examining i you'll see that it is 0 (Zero) and the contents of theArray at position 0 is Nothing, which is the cause of the problem.

Just say again thank you to Tocon for allowing the use of their code for this example.
Was This Post Helpful? 2
  • +
  • -

#3 juunas  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 25-March 09

Posted 28 April 2009 - 01:37 AM

Oh my god, thank you! I've always just used some kind of labels to see the contents of a variable. This will be a big help!
Was This Post Helpful? 0
  • +
  • -

#4 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Posted 28 January 2010 - 12:23 PM

Or ...

You could test and perfect each procedure / method before you extend upon your project.

Languages like Java don't have an F5 key to press.

No debugging tools no nothing like that.

All you have is notepad and a compiler.

Well that was all that I had when I studied Java at Uni.

I found that the best way was to decompose the project (break down into as many pieces as humanly possibly) -- then do each proc / method, compile, run and test.

I use the same protocol for any language.

Usually resulting in bug-free projects mostly.

Code 50 lines and just keep hitting that F5 key to RUN.

Easy greasy ...

Trent Jackson
Was This Post Helpful? 0
  • +
  • -

#5 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Posted 16 October 2011 - 03:08 PM

[ mod removed unnecessary quote ]



So what programming did you add to correct this problem? A One? How was this resolved? This is helpful, but applying it is still a bit confusing...sorry.

This post has been edited by modi123_1: 17 October 2011 - 07:20 AM
Reason for edit:: removed excessive quote

Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 17 October 2011 - 07:14 AM

charyl: The point of this tutorial, isn't about solving the problem but learning the basic skills to help you find the cause of the problem. If have fully read this tutorial and replies you would have noticed a link to the original source of the coding use as the example.
Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1291
  • Posts: 2,859
  • Joined: 30-January 11

Posted 17 October 2011 - 07:20 AM

View PostT.Jackson, on 28 January 2010 - 08:23 PM, said:

Or ...

You could test and perfect each procedure / method before you extend upon your project.

Languages like Java don't have an F5 key to press.

No debugging tools no nothing like that.

All you have is notepad and a compiler.

Well that was all that I had when I studied Java at Uni.

I found that the best way was to decompose the project (break down into as many pieces as humanly possibly) -- then do each proc / method, compile, run and test.

I use the same protocol for any language.

Usually resulting in bug-free projects mostly.

Code 50 lines and just keep hitting that F5 key to RUN.

Easy greasy ...

Trent Jackson


Really? I can't believe that a Uni would force you to use notepad and the command prompt to compile and edit your programs when there are so many free IDE's out there (that do include debugging features and an F5 key (if you change the keyboard shortcuts :P)) - Eclipse and Netbeans to name a few.

Although I do agree that breaking down the problem is a very good technique to learn, individually building and compiling them just seems so unproductive to me.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 28 February 2012 - 11:58 AM

Another Example of where debugging skills are useful.

Inheritance Tutorial

Yep, that's right it one of mine. Even I make mistakes and bugs.

View Postshadachi, on 28 February 2012 - 07:25 AM, said:

Awesome Tutorial Adam..It actually allowed me to look at inheritance from a different perspective ..

oh yea..just to let u know if not mistaken

X:= 4 Y:= 5 Z:=0



shouldn't it be in the output section
X:= 4 Y:= 5 Z:=6



and oh yea.. this part i don't really understand. The colon operator u've used.

 _Angle = Angle : _Distance = Distance



Not as the code is currently written, do to a lapse in the programmer's coding.
Let's use our debugging skills to find the issue.




How to think through issue?

OK so know it outputs a value, so let start with the .ToString Method of 3D_XYZ.

 Public Overrides Function ToString() As String
    Return MyBase.ToString() + " " + String.Format("Z:={0}", Me.Z)
  End Function



We can rule out the first parts Return MyBase.ToString() + " " since that call the base-class's implementation of it, and that works.
So let look at the rest + String.Format("Z:={0}", Me.Z).
Nothing there since that just gets the value from the .Z property of this instance Me of the class 3D_XYZ.

So let's examine that propety.

  Public ReadOnly Property Z As Integer
    Get
      Return _Z
    End Get
  End Property


That's just getting the value from the private member field _Z

OK where does it get a value
Private _Z As Integer = 0


From the initialiser is one. What about in the constructor, since it gets create with a value for the Z parameter passed in to it.

  Public Sub New(X As Integer, Y As Integer, Z As Integer)
    MyBase.New(X, Y) ' Call the Constructor of the base-class I'm inheriting from.
  End Sub



So I take the value from the parameters X and Y, passing them down to the base-class used by this class.

MyBase.New(X, Y) ' Call the Constructor of the base-class I'm inheriting from.

What about the Z parameter, what do I do with that?

Spoiler

This post has been edited by AdamSpeight2008: 28 February 2012 - 12:39 PM

Was This Post Helpful? 2
  • +
  • -

#9 Jazzibear  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-February 12

Posted 11 March 2012 - 04:38 AM

HI Adam


Firstly thanks for placing the code for me

Sorry i didnt know

Also thanks for the quick reply

I have used the Debug feature on my code and it was this that iluded me to the fact that there may be a problem with the Loop

This is because if I Debug the loop it will continue till to loop unitl the condition is positive then it will End Sub and represent the Gui however if the condition is negative then it will not End Sub.Instead it will continue to Loop

The only common factor that I can see is that it will not exit the loop unless the Random Number is equal to the attempt e.g. i = attempt

Do you have any other suggestions

Thanks again

Jazz
Was This Post Helpful? -1
  • +
  • -

#10 CameronOk  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 26-August 12

Posted 27 August 2012 - 06:26 AM

Another useful debugging tool is ...

Debug.Writeline(VariableValue)

Using breakpoints can sometimes alter program execution in minute ways, for example if a timer or multi-threading is involved. It is not a good idea to litter your code with breakpoints if you need more than a few. Instead insert debug.writeline where the value of the variable is important.
Was This Post Helpful? 0
  • +
  • -

#11 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Posted 25 December 2012 - 09:52 PM

@ ADAM
very useful tutorial... thanks for again guiding us with such a greate tut ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1