Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 2 Back to Index v Basic Data Types v Arithmetic.

Similar presentations


Presentation on theme: "Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 2 Back to Index v Basic Data Types v Arithmetic."— Presentation transcript:

1 Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 2 Back to Index v Basic Data Types v Arithmetic Operators v Inputting Strings v Handling Numbers v Scope Of Variables

2 Lec2 P 2 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Variable Identifiers  Declaring variables: Dim VariableName As type  Examples: Students to add example v The rules for naming identifiers are: –Must begin with a letter –Can only contain letters, numbers and the underscore character ( _ ) –Must not exceed 40 characters –Cannot be a Visual Basic reserved word

3 Lec2 P 3 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Visual Basic Standard Data Types v Data type Storage sizeExample Name v Integer 2 bytesiCount v Long (integer)4 byteslDebt v Single(floating-point)4 bytesfArea v Double(floating point)8 bytesdWidth v Currency(scaled integer)8 bytescPrice v String1 byte/charsName v VariantVariesvData v User-defined(Type)size bytesuPaymentDetails (VB4 provides Byte, Boolean, Date, etc)

4 Lec2 P 4 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Note v String -used for textual information, length is determined when assigned v Variant - for compatibility with earlier BASICs, the type is determined when it is assigned to. DO NOT USE  Boolean - No boolean type available, (up to VB4) but they can be expressed as an integer - declare as Integer. 0= FALSE anything else= TRUE

5 Lec2 P 5 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Naming Conventions v Prefix each name with a lower case letter showing the data type –i for Integer –l for Long –d for Double –s for String –c for Currency –f for Single Float –u for User Defined v Capitalise the first/significant letter of each word in the name –e.g. iNumberOfSeats v Choose meaningful names –if you cannot pronounce it then it is not meaningful!

6 Lec2 P 6 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Variable Declaration 1 v Variables can be declared in a control event procedure v In this case they are only available within the procedure in which they are declared. Local to the procedure (Sub routine)

7 Lec2 P 7 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Constant Identifiers and Naming v Use meaningful names v By capitilising the whole identifier it is easier to see where constants are used in code  Examples: Const SEATCOST = 2.5 ‘price of a seat 'total number of seats available Const SEATSAVAILABLE = 100 v Note the use of Comments

8 Lec2 P 8 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Constant Declaration v Constants can be declared in a control event procedure v In this case they are only available within the procedure in which they are declared Local to the procedure (Sub routine)

9 Lec2 P 9 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Implicit / Explicit Declaration v Implicit Declaration: You can just use any name, Variable type determined by the data assigned  Explicit Declaration: You must pre-declare any variables used: Dim sName As String Dim iCount As Integer v Explicit declaration is a much better approach to adopt, implicit declaration is for backwards compatibility v Can force Explicit Declaration via Options/Environment option

10 Lec2 P 10 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Arithmetic Operators v Arithmetic operation SymbolExample Exponentiation ^ 2 ^ 3 = 8 (2*2*2 or 2 3 ) Multiplication & division *, / 2 / 4 = 0.5 Addition & subtraction +, - 2 + 2 = 4 Integer division \ 7 \ 2 = 3 (goes 3 times) Modulo arithmetic Mod 7 Mod 2 = 1 (remainder 1) String addition + "abc" + "def " = “abcdef" String concatenation & "abc" & "def " = "abcdef"

11 Lec2 P 11 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Operator Order of Precedence v In expressions containing a mixture of operators the order of evaluation is arithmetic, comparison and logical operators  Within individual categories, operators are evaluated in the order of precedence shown below: ArithmeticComparisonLogical Exponentiation ( ^ )Equality ( = ) Not Negation ( - )Inequality ( <> ) And Multiplication, division ( *, / )Less than ( ) Xor Modulo arithmetic ( Mod )Less than or Equal to ( = ) Imp String concatenation ( & ) Like Is

12 Lec2 P 12 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Assignment  In Basic the = character is both the assignment character and the logical equivalence character: Students to add an example  Some type conversions are automatic: Students to add example  Use the string concatenator & to allow automatic conversion: Students to add example v Some type conversions must be coded

13 Lec2 P 13 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Val and Str$ v Converting a String to Number Students to add an example v Converting a Number to a String Students to add an example

14 Lec2 P 14 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Question v Assume that the following variable have been declared : inum1, inum2, snum3, snum4 and iresult1, sresult2. v Show the statements for the following : 1.Assign inum1 + inum2 to iresult1 2.Assign inum1 + inum2 to sresult2 3.Assign snum3 + snum4 to iresult1 4.Assign snum3 + snum4 to sresult2 5.Assign inum1 + snum2 to iresult1 6.Assign inum2 + snum4 to sresult2

15 Lec2 P 15 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Inputting Strings v eg. To input a persons first and second name and assign to string variables. sFirstName = Text1.Text sSecondName = Text2.Text

16 Lec2 P 16 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Concatenating Strings v Concatenating the input text (strings ) Students to add code

17 Lec2 P 17 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Handling Numbers v VB will automatically convert numbers input from and output to text and label boxes. Students to add notes

18 Lec2 P 18 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Question v Write an application to input two numbers via text boxes. Use four separate commands to calculate either the addition, subtraction, multiplication or division of the numbers. The result is to be displayed in a Label box. v For this question use FULL CONVERSIONS via Str$() and Val()

19 Lec2 P 19 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Referencing properties Label1.Caption = Text1.text v Assigning a labels’ caption equal to the text in a text box we would write: Label1.Caption = Text1.text Form1.Label1.Caption = Form1.Text1.Tex v We could write : Form1.Label1.Caption = Form1.Text1.Text The Form associated with the statement is taken as the default. v To reference properties on other forms : Students to add statement

20 Lec2 P 20 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton SetFocus Methods v We may want to use code to set the Focus (point of input) to a particular control, for example in our earlier program after adding the numbers we may want to set the Focus back to the first text box v Students to add statement v Our code in our earlier program would be modified to Text1 v This sets the Focus back to text box Text1

21 Lec2 P 21 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton TabIndex v If we want to be able to move between controls using the Tab key we can set the TabIndex property - at design or run time. v For our earlier program we may want to Tab from control Text1 to Text2 Then Command1 v To do this we would set in the properties window –Students to add statements

22 Lec2 P 22 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Form Load v Code can be attached to a form and executed whenever the form is loaded. v Used for initialisation Initialise variables, arrays, controls v E.g. Text1.Text = ‘xxxxxxx’ Text2.Text = ‘xxxxxxx’

23 Lec2 P 23 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Scope Of Variables Variable Declaration 2 v Variables can also be declared in the general declarations section of a form (Double click on the form) v In this case they are available anywhere within the form in which they are declared - ie. in any control code on the form BUT not on other forms

24 Lec2 P 24 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Scope of Variables v Shows declarations at form level, known as: General Declarations v Shows variable declarations within an event handler Form1 General Declarations Sub Command1_Click () Sub Command2_Click () Dim sName1 As String Dim iNum1 As Integer Dim sName2 As String Dim iNum2 As Integer Dim sName3 As String Dim iNum3 As Integer Available variables: sName1, sName2, iNum1, iNum2 Available variables: sName1, sName3, iNum1, iNum3

25 Lec2 P 25 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Static Variables  A static variable will hold its value when it goes out of scope: Sub Command1_Click() 'declare variables Dim iDimCount As Integer Static iStaticCount As Integer 'increment variables iDimCount = iDimCount + 1 iStaticCount = iStaticCount + 1 'display variables Label3.Caption = Str$(iDimCount) Label4.Caption = Str$(iStaticCount) End Sub v A static variable can only be declared inside a procedure and only available inside the procedure

26 Lec2 P 26 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Static Variables: Effects v The effect of using a static variable can be seen below: Students to add diagrams v This has the same effect as if the variable had been declared at the form’s general declaration level Except the scope is local to the procedure

27 Lec2 P 27 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Scope Of Variables Code Modules v A code module can be thought of as being a form without a screen attached v In a code module you can declare: Students to add examples v We can also write code in a module, we will not be looking at this …....yet v Variables declared as Global have scope throughout the application, that is they can be seen across all forms

28 Lec2 P 28 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Adding a Module to an Application v Use either the tool bar Module icon or the New Module menu option to add a code module to the application The tool bar Module icon The menu New Module option

29 Lec2 P 29 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Code Module Windows Available v A code module consists only of declarations and code v It doesn’t have a form associated with it v You can only get access to a code entry window

30 Lec2 P 30 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Global Variables v In a code module we can declare a variable using the keyword Global  General form for declaring global variables: Global VariableName As type  Example: Global giTotalSeatsBooked As Integer v Prefix each global variable identifier with a lower case letter g to show that it is a global variable and the lower case letter indicating the data type. e.g. i for integer

31 Lec2 P 31 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Visual Basic Declaration Locations Form 1 Control 1 Code Module 1 Dim iNum1 As Integer Global giNum1 As Integer Dim iNum2 As Integer Dim iNum3 As Integer Static iNum4 As Integer Control 2 Dim iNum5 As Integer Static iNum7 As Integer Form 2 Control 1 Dim iNum8 As Integer Dim iNum9 As Integer Static iNumA As Integer Control 2 Dim iNumB As Integer

32 Lec2 P 32 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Using Global Variables v Global variables are ideal for storing information which has to be accessed from more than one form v They have scope throughout all forms and modules within an application Students to add code Global gsName As String Module1.Bas

33 Lec2 P 33 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Global Constants v A global constant is basicaly a global variable that you cannot change once the program is running v You must initialise a global constant with the required value when it is declared, just as with an ordinary constant  The syntax for a Global Const statement is: Global Const identifier = expression v You should use meaningful identifies names  Example: 'total number of seats available in hall Global Const gTOTALSEATSAVAILABLE% = 100

34 Lec2 P 34 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Question v The application is to calculate the average age of a class of students. Form1 is used to enter and accumulate the students ages and form2 used to calculate the average and display the results. v Show the code for all command buttons and clearly show where the variables are declared.


Download ppt "Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 2 Back to Index v Basic Data Types v Arithmetic."

Similar presentations


Ads by Google