Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select.

Similar presentations


Presentation on theme: "Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select."— Presentation transcript:

1 Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select Case v User defined Types v Dialog boxes : Message boxes, Input boxes v Menu Designer

2 Lec3 P 2 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Syntax: If...Then... Statement v In Visual Basic the statement has two main forms, in both the condition is held inside normal (curved) brackets v Single line form 1: If...Then... If ( condition ) Then {actions when condition is TRUE}  Example: If (iMarks<40) Then Label3.Caption = "Fail"

3 Lec3 P 3 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Syntax: If...Then...Else... Statement v Single line form 2: If...Then...Else... If ( condition ) Then {TRUE action} Else {FALSE action}  Example: If (iMarks<40) Then Label3.Caption = "Fail" Else Label3.Caption = "Pass" v This form is really present to aid backwards compatibility v It is VERY BAD practice as it is detrimental to the understandability of the code, you should NOT use it!

4 Lec3 P 4 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Syntax: If...Then... Statement Block Form In C: Students to add code v Allows more than one action (statement) as part of a condition. Allows selections to be nested In VB: Students to add code

5 Lec3 P 5 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton v As in C - if the result of the condition is true ( non zero value) statements are executed. Hence non zero - TRUE zero - FALSE v Unlike C the ‘=‘ symbol is used for both assignment and as an operator. v C uses : Students to add code

6 Lec3 P 6 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton v Example 1: If (iMarks<40) Then Label3.Caption = "Fail" Label3.ForeColor = QBColor(4) ‘red End If v Example 2: Students to add code

7 Lec3 P 7 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Syntax: If...Then...Else... Block Form v In C: if (expr) { statements } else { statements } v In VB - Block form: Students to add block form

8 Lec3 P 8 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton v Example: If (iMarks<40) Then Label3.Caption = "Fail" Label3.ForeColor = QBColor(4) ‘red Else Label3.Caption = "Pass" Label3.ForeColor = QBColor(1) ‘blue End If

9 Lec3 P 9 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Syntax: If...Then...ElseIf...Else.. Block Form – Block form: If ( condition ) Then actions... when condition is TRUE ElseIf ( condition ) Then actions... when condition is TRUE Else actions... when both other conditions are FALSE End If – Example: If (iMarks<40) Then Label3.Caption = "Fail" ElseIf (iMarks<60) Then Label3.Caption = "Pass" Else Label3.Caption = “Merit” End If

10 Lec3 P 10 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Comparison Operators v All The normal operators are available in VB > greater than = is greater than or equal to is not equal to NOT(….)

11 Lec3 P 11 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Switch Statement v In C : switch (val) { case 1: statements; break; case 2: statements; break; default: statements; }

12 Lec3 P 12 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Select Case Statement 1 v In VB - simplest form of the Select statement is: Select Case test expression Case expression list {actions... when expression list is TRUE} Case Else {actions... when all conditions are FALSE} End Select v There are a number of forms of the expression list v Cases can be freely mixed, prior to Case Else

13 Lec3 P 13 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Select Case Statement 2 v e.g.Select Case test expression Case expression 1 {actions... when expression 1 is TRUE} Case expression 2, expression 3 {actions... when expression 2 or 3 are TRUE} Case expression 4 To expression 5 {actions...when in expression 4 To 5 range is TRUE} Case Is = expression 6 {actions... when test expression = expression 6} Case Is >= expression 7 {actions... when test expression >= expression 7} Case Else {actions... when all conditions are FALSE} End Select

14 Lec3 P 14 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Syntax: Select Case Statement v Students to add case syntax eg

15 Lec3 P 15 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Question v Using the form design below, write code for the calculate grade command button using : (a) If Then statements ONLY (b) Select Case statements ONLY ©What about Data Validation ? – 0-39 FAIL, 40-59 PASS, >60 MERIT

16 Lec3 P 16 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton User Defined Types - Overview C++- Data Structures Pascal/Delphi - Records VB - User defined types v Used to hold a group of related data under one name v eg. Students to add examples

17 Lec3 P 17 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Declaration v In C In VB struct DateTypeType DateType {iyear As Integer int iyear;imonth As Integer int imonth;iday As Integer int iday;End Type }; DateType sbirthday;Dim uBithday As DateType

18 Lec3 P 18 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Assignment to User Defined Variables 1 v ‘C’ has far more flexibility - pointers used together with structures ( ie. Pointers to structures and offsets to access the fields of the structure. v Variables of UFDs can be assigned to in two ways: 1. To an individual element/field within the user defined type variable uBithday.iyear = 1998 uBithday.imonth = 6 uBithday.iday = 20 Students to show input and output

19 Lec3 P 19 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Assignment to User Defined Variables 2 2. To the user defined type variable as a whole:  Both variables involved in the assignment must be of the same user defined type uNewBook = uOldBook v Both uNewBook and uOldBook are of the user defined type BookType v This would assign all of the elements/fields of uOldBook to their respective element/field in uNewBook

20 Lec3 P 20 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton User Define Types - Detailed Example This is a sample entry in a card index system Title: Author(s): Pages: ISBN: Year: Understanding & Using Visual Basic Barron, Jonathan C 448 0-314-07155-5 1996

21 Lec3 P 21 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Creating a User Defined Data Type v Students to add notes

22 Lec3 P 22 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Declaring a User Defined Type v A user defined type must be declared within the general declarations section of a code module

23 Lec3 P 23 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Syntax: Type Definition v Declaration starts with keyword Type and ends with End Type v Each element/field should be defined v Example: Students to add type declaration

24 Lec3 P 24 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Declarations of Variables of the Type v Once a type definition is placed in the code module it is available as a type throughout the application  We use exactly the same process here as for any other variable Dim uNewBook As BookType Students to add other declarations

25 Lec3 P 25 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Question - revision v Which declarations could be made where? (i.e. at module level, form level or control handler level)

26 Lec3 P 26 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Nesting of Data Structures v We can nest a data structure within another data structure: 'udt declarations in a code module Type ChargesType cDayHire As Currency cMileage As Currency cInsurance As Currency End Type Type BillType uCharges As ChargesType cTotalBill As Currency cRefundExcess As Currency End Type BillType uCharges cTotalBill cRefundExcess cDayHire cMileage cInsurance ChargesType

27 Lec3 P 27 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Accessing Nested Data Structures v Example of accessing the variable Dim uBill As BillType Sub Command5_Click () ‘calc component charges uBill.uCharges.cDayHire = iDaysOnHire*DAYHIRERATE@ uBill.uCharges.cMileage = (iFinishMiles-iStartMiles) * MILEAGERATE@ uBill.uCharges.cInsurance = iDaysOnHire*DAYINSURANCERATE@ ‘calc total & refund/excess uBill.cTotalBill = uBill.uCharges.cDayHire +uBill.uCharges.cMileage +uBill.uCharges.cInsurance uBill.cRefundExcess = uBill.cTotalBill - DEPOSIT@ End Sub v Remember all assignements are one line only!

28 Lec3 P 28 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Dialog Boxes Message 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 something Const MB_OKCANCEL = 1 Dim iResponse As Integer iResponse = MsgBox("Message",MB_OKCANCEL,"Title")  They are used for warnings, confirmation, etc..

29 Lec3 P 29 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton 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 Box: Const MB_OKCANCEL = 1 Const MB_ICONQUESTION = 32 Const MB_BOXSTYLE = MB_ICONQUESTION + MB_OKCANCEL Dim iResponse As Integer iResponse = MsgBox("Message",MB_BOXSTYLE,"Title")

30 Lec3 P 30 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton 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,"Title") ‘check how the user exited Students to add remainder of code  The message box can be application or system modal

31 Lec3 P 31 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Input Boxes v The Input Box allows you to get information from a user, You can set the title, message, default text and position on the screen v It returns the string that the user entered when they clicked Ok v If you Cancel then an empty string "" is returned  Example: Label1.Caption=InputBox$("Enter Name","Title")

32 Lec3 P 32 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton A Typical Drop-down menu Menu bar Cascading Menus Separator bar Shortcuts Checked option Disabled Options

33 Lec3 P 33 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton The Menu Design Window This code gives Label1 blue text when “Blue Text” option is selected

34 Lec3 P 34 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Demo of menu builder v As above example


Download ppt "Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select."

Similar presentations


Ads by Google