Tải bản đầy đủ - 0 (trang)
Appendix D. Visual Basic Debugging Tools

Appendix D. Visual Basic Debugging Tools

Tải bản đầy đủ - 0trang

AppendixD.VisualBasicDebugging

Tools

Errorsinprogramsarecalledbugs,andtheprocessoffinding

andcorrectingthemiscalleddebugging.SincetheVisual

Basiceditordoesnotdiscovererrorsduetofaultylogic,these

errorspresentthemostdifficultiesindebugging.Onemethodof

discoveringalogicalerrorisbydesk-checking,thatis,tracing

thevaluesofvariablesonpaperbywritingdowntheirexpected

valueafter"mentallyexecuting"eachlineintheprogram.Desk

checkingisrudimentaryandhighlyimpracticalexceptforsmall

programs.

AnothermethodofdebugginginvolvesplacingMsgBox

statementsatstrategicpointsintheprogramanddisplayingthe

valuesofselectedvariablesorexpressionsuntiltheerroris

detected.Aftercorrectingtheerror,theMsgBoxstatementsare

removed.Formanyprogrammingenvironments,deskchecking

andMsgBoxstatementsaretheonlydebuggingmethods

availabletotheprogrammer.

TheVisualBasicdebuggeroffersanalternativetodesk

checkingandMsgBoxstatements.Itallowsyoutopauseduring

theexecutionofyourprograminordertoviewandaltervalues

ofvariables.Thesevaluescanbeaccessedthroughthe

Immediate,Watch,andLocalswindows,knownasdebugging

windows.



[Page627(continued)]



TheThreeProgramModes

Atanytime,aprogramisinoneofthreemodesdesignmode,

runmode,ordebugmode.(Debugmodeisalsoknownas

breakmode.)Whenthecurrentmodeis"run"or"debug,"the

words"Running"or"Debugging"aredisplayed(inparentheses)

intheVisualBasictitlebar.Theabsenceofthesewords

indicatesthatthecurrentmodeis"design."



[Page628]

(Thisitemisdisplayedonpage627intheprintversion)

Titlebarandtoolbarsduringdesignmode.



[Viewfullsizeimage]



Titlebarandtoolbarsduringrunmode.



[Viewfullsizeimage]



Titlebarandtoolbarsduringdebugmode.



[Viewfullsizeimage]



Withtheprogramindesignmode,youplacecontrolsonaform,

settheirinitialproperties,andwritecode.Runmodeisinitiated

bypressingF5ortheStartbutton.Debugmodeisinvoked

automaticallywhenarun-timeerroroccurs.YoucanuseDebug

optionstobreaktheprogramatspecifiedplaces.Whilethe

programisindebugmode,youcanhoverthecursoroverany

variabletoobtainitscurrentvalue.Also,youcanusethe

debuggingwindows,suchastheImmediate,Watch,andLocals

windows,toexaminevaluesofexpressions.Whenyouenter

debugmode,theToolbarcontainsaContinuebutton.Youcan

clickonittocontinuewiththeexecutionoftheprogram.







[Page628(continued)]



SteppingthroughaProgram

Theprogramcanbeexecutedonestatementatatime,with

eachpressofanappropriatefunctionkeyexecutinga

statement.Thisprocessiscalledstepping.Aftereachstep,

valuesofvariables,expressions,andconditionscanbe

displayedinthedebuggingwindows,andthevaluesofvariables

canbechanged.

Whenaprocedureiscalled,thelinesoftheprocedurecanbe

executedoneatatime,referredtoassteppingintothe

procedure,ortheentireprocedurecanbeexecutedatonce,

referredtoassteppingoveraprocedure.Astepovera

procedureiscalledaprocedurestep.Inaddition,youcan

executetheremainderofthecurrentprocedureatonce,

referredtoassteppingoutoftheprocedure.Thethreetoolbar

buttonsshowninFigureD.1canbeusedforstepping.



FigureD.1.ThetoolbarbuttonsusedtoStepInto,

StepOver,andStepOut.



Asanotherdebuggingtool,VisualBasicallowstheprogrammer

tospecifycertainlinesasbreakpoints.Then,whenthe

programisrun,executionwillstopatthefirstbreakpoint

reached.Theprogrammercantheneitherstepthroughthe

programorcontinueexecutiontothenextbreakpoint.Also,the

programmercanplacethecursoronanylineintheprogram

andhaveexecutionstopatthatlinewitha"RuntoCursor"

command.Programexecutionnormallyproceedsinorder



throughthestatementsintheprocedure.However,atanytime

theprogrammercanspecifythenextstatementtobeexecuted.

Thetasksdiscussedpreviouslyaresummarizednext,alongwith

ameanstocarryouteachtask.Thetasksinvokedwithfunction

keysalsocanbeproducedfromthemenubar,thecontext

menu(producedbyclickingtherightmousebutton),orthe

toolbar.



[Page629]

Runtocursor



PressCtrl+F8



StepInto



PressF8



StepOver



PressShift+F8



StepOut



PressCtrl+Shift+F8



Setabreakpoint



Movecursortoline,pressF9



Removeabreakpoint



Movecursortolinecontaining

breakpoint,pressF9



Clearallbreakpoints



PressCtrl+Shift+F9



Continueexecutiontonext







breakpointortheendoftheprogram



PressF5



Stopdebugging



Ctrl+Alt+Break







[Page629(continued)]



TheImmediateWindow

WhileinBreakmode,youcansetthefocustotheImmediate

windowbyclickingonit(ifvisible),bypressingCtrl+Alt+I,

orbyhoveringthecursorover"Windows"intheDebugmenu

andclickingonImmediate.Whenyoutypeastatementintothe

ImmediatewindowandpresstheEnterkey,thestatementis

executedatonce.Astatementoftheform

?expression



displaysthevalueoftheexpressiononthenextlineofthe

Immediatewindow.(Thequestionmarkisshorthandfor

Debug.Print.)Astatementoftheform

var=value



assignsavaluetoavariable.InFigureD.2,thevariable

numVarhadthevalue10whentheprogramwasinterrupted.



FigureD.2.Threestatementsexecutedinthe

Immediatewindow.



[Page629(continued)]



TheWatchWindow

TheWatchwindow,whichonlycanbeviewedinBreakmode,

permitsyoutoviewthevaluesofvariablesandexpressions.

TheWatchwindowinFigureD.3showsthevaluesofone

variableandtwoexpressions.Ifyoudon'tseeaWatchwindow

whenyouenterBreakmode,hoverthecursoroverWindowsin

theDebugmenu,andthenclickonWatch.



FigureD.3.AtypicalWatchwindow.



[Page630]

AlthoughyoucantypedirectlyintotheWatchwindow,the

easiestwaytoaddanexpressiontothewindowistoright-click

onavariableintheCodewindowandthenclickon"AddWatch"

inthecontextmenu.Youcanthenaltertheexpressioninthe

NamecolumnoftheWatchwindow.Todeleteanexpression

fromtheWatchwindow,right-clickontheexpressionandthen

clickon"DeleteWatch."Also,youcandirectlyalterthevalueof

anyvariableintheWatchwindowandhavethevaluesofthe

otherexpressionschangeaccordingly.



[Page630(continued)]



TheLocalsWindow

WhileinBreakmode,youcaninvoketheLocalswindowfrom

theDebugmenubypositioningthecursoroverWindowsand

thenclickingonLocals.Thiswindowautomaticallydisplaysthe

names,values,andtypesofallvariablesinthecurrent

procedure.SeeFigureD.4.Youcanalterthevaluesofvariables

atanytime.Inaddition,youcanexamineandchange

propertiesofcontrolsthroughtheLocalswindow.



FigureD.4.AtypicalLocalswindow.



[Viewfullsizeimage]



[Page630(continued)]



SixWalkthroughs

Thefollowingwalkthroughsusethedebuggingtoolswiththe

programmingstructurescoveredinChapters3,4,5,and6.



[Page630(continued)]



SteppingthroughanElementaryProgram:

Chapter3

Thefollowingwalkthroughdemonstratesseveralcapabilitiesof

thedebugger:

1. Createaformwithabutton(btnPush)andatextbox

(txtBox).

2. Double-clickonthebuttonandenterthefollowingevent

procedure:

PrivateSubbtnPush_Click(...)HandlesbtnPush.Click

DimnumAsInteger

num=CInt(InputBox("Enteranumber:"))

num+=1

num+=2

txtBox.Text=CStr(num)

EndSub



3. PlacethecursoronthelinebeginningPrivateSub,pressthe

rightmousebutton,andclickon"RuntoCursor."The

programwillexecute,andtheformwillappear.



[Page631]

4. Clickonthebutton.IntheCodewindow,ayellowarrow

pointstothePrivateSubstatement.

5. PressF8.Theyellowarrownowpointstothestatement

containingInputBoxtoindicatethatitisthenextstatement



tobeexecuted.(PressingF8isreferredtoassteppinginto.

Youcanalsosteptothenextstatementofaprogramwith

theStepIntooptionfromtheDebugmenuorwiththeStep

Intoicon.)

6. PressF8.ThestatementcontainingInputBoxisexecuted,

andanInputdialogboxrequestinganumberappears.

Respondtotherequestbytyping5andclickingtheOK

button.

7. PressF8againtoexecutethestatementnum+=1.

8. Letthemousehoveroveranyoccurrenceofthevariablenum

forasecondorso.Thecurrentvalueofthevariablewillbe

displayedinasmallbox.

9. PressCtrl+Alt+Breaktostopdebugging.(Youalsocan

stopdebuggingbyclickingon"StopDebugging"intheDebug

menu.)

10. Movethecursortotheline

num+=2

andthenpressF9.Areddotappearsonthegrayborderto

theleftoftheline.Thisindicatesthatthelineisabreakpoint.

PressingF9isreferredtoastogglingabreakpoint.

11. PressF5andclickonthebutton.Respondtotherequestby

entering5andpressingOK.Theprogramexecutesthefirst

threelinesandstopsatthebreakpoint.Thebreakpointlineis

notexecuted.

12. OpentheImmediatewindowbypressingCtrl+Alt+I.If



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Appendix D. Visual Basic Debugging Tools

Tải bản đầy đủ ngay(0 tr)

×