ICS 021 Week 5 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

ICS 021 Week 5

Description:

Visual Basic provides us with arrays of standard data type and user defined types : ... Set Index property of a control to non-null value ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 23
Provided by: jaweedy
Category:
Tags: ics | week

less

Transcript and Presenter's Notes

Title: ICS 021 Week 5


1
ICS 021 Week 5
  • Week 5
  • Control Arrays
  • Debugging

2
Control Arrays
  • Visual Basic provides us with arrays of standard
    data type and user defined types
  • Dim inum(20) As Integer
  • Dim studrec(100) As Studenttype
  • Visual Basic also provides us with arrays of
    Controls
  • We can create an array of Command buttons or
    List boxes, etc
  • This enables efficient processing of the
    controls

3
Control Arrays 1
  • Control Array is a number of controls that have
    the same event handler, the system passes the
    controls index into the procedure.

0 1 2
4
Control Arrays 2
  • The Control Array event handler can use a select
    statement instead of an if statement.

0 1 2
5
Control Arrays Creating
  • Three ways to create a control array
  • Copy an existing control and paste it onto form
  • Assign same name to more than one control
  • Set Index property of a control to non-null value
  • For each of the above Visual Basic will ask do
    you wish to create a control array

6
  • Creating Control Arrays at run-time Load
    ControlName(Index) Unload ControlName(Index)
  • Add more notes here

7
Demo 1 - control arrays
  • Create a form with two text fields, a label to
    hold an answer and a control array of four
    command buttons.
  • When a button is pressed the operation answer
    should be displayed in Label1 45 - 52 -7
    or 3 5 4

8
Select Case Statement Control Arrays
  • Using the Select Case statement to choose which
    control within a control array has been
    pressedSub Command1_Click (Index As Integer)
  • Dim result As Integer
  • Select Case Index Case 0 call
    add(val(text1.text),val(text2.text),
  • result) Case 1 etc
  • End Selectlabel1.caption result
  • End Sub

9
Demo 2 - Labels as a control array
Create array of Labels at design time
10
(No Transcript)
11
Dialog BoxesMessage Boxes 1
  • Message Boxes are used when you want the user to
    choose from a limited number of options, or just
    to inform them of somethingConst
    MB_OKCANCEL 1Dim iResponse As
    IntegeriResponse MsgBox("Message",MB_OKCANCEL,"
    Title")
  • They are used for warnings, confirmation, etc..

12
Message Boxes 2
  • There are a standard range of button combinations
    and icons available MB_ICONSTOP MB_ICONQUESTION
    MB_ICONEXCLAMATION MB_ICONINFORMATION
  • They can be added together to give the required
    Message BoxConst MB_OKCANCEL 1Const
    MB_ICONQUESTION 32Const MB_BOXSTYLE
    MB_ICONQUESTION MB_OKCANCELDim iResponse As
    IntegeriResponse MsgBox("Message",MB_BOXSTYLE,"
    Title")

13
Message Boxes 3
  • You can act upon the users response to the
    message box Const MB_OKCANCEL 1 Const
    MB_ICONQUESTION 32 Const MB_BOXSTYLE
    MB_ICONQUESTION MB_OKCANCEL Const IDOK 1
    Dim iResponse As Integer show the message
    box iResponse MsgBox("Message",MB_BOXSTYLE,"Ti
    tle") check how the user exited If
    iResponse IDOK Then Label1.Caption You
    OKd Else Label1.Caption You Cancelled
    End If
  • The message box can be application or system modal

14
Debugging - Introduction
  • There are three types of errors
  • Compile errors occur as a result of code that is
    incorrectly constructed including syntax error
  • matched control structure, such as a Next
    statement without a corresponding For statement,
  • or programming mistakes that violate the rules of
    Basic, such as a misspelled word, a missing
    separator,
  • or a type mismatch.
  • syntax errors include passing an incorrect number
    of arguments to an intrinsic function
  • or forgetting to match parentheses

15
Source Of errors
  • Run-time errors occur after the application
    starts to execute.
  • attempting an illegal operation, such as writing
    to a file that doesn't exist
  • or dividing by zero.
  • Logical errors the program doesn't perform as
    intended, and produces incorrect results.

16
How to debug - overview
  • Debugging is a process by which you find and
    resolve errors in your code.
  • To debug code in Visual Basic,
  • 1. Print the code, if you find it easier to read
    code on paper instead of onscreen.
  • 2. Run the application to find trouble spots
  • Run until an error stops execution,
  • or halt execution manually when you suspect an
    error by choosing Break

17
  • 3. Use debugging tools and techniques to isolate
    bugs and to monitor code flow
  • Set breakpoints to halt execution at certain
    points to look for problems such as
  • incorrect variable types,
  • mixups in variable names,
  • flaws in logical comparisons,
  • endless loops,
  • garbled output,
  • problems with arrays, and so on.

18
  • Add a watch expression at design time or while in
    break mode to allow you to monitor the value of a
    particular expression as your code runs.
  • Single step or procedure step through your code
    to trace through the application as it's running.
  • Use the Debug window to test individual lines of
    code or procedures, or to change values.
  • Enter break mode and choose Calls from the Debug
    window to see where your code is currently
    executing and trace the path showing how it got
    there.

19
  • 4. Try out bug fixes and then make edits
  • Test individual lines of new or debugged code in
    the Debug window.
  • Search and replace code for all occurrences of
    an error, checking other procedures, forms, or
    modules with related code.

20
Demo - debugging an application
21
  • Students should ensure that they are familiar
    with the debugging techniques covered
  • Faster development of software
  • Examination questions

22
End of Lecture
Click House to Return to Main Menu
Write a Comment
User Comments (0)
About PowerShow.com