Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030...
-
date post
22-Dec-2015 -
Category
Documents
-
view
216 -
download
3
Transcript of Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030...
Lec2 P 1CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
CP2030 VBFCLecture 2
Back to Index
Basic Data Types Arithmetic Operators Inputting Strings Handling Numbers Scope Of Variables
Lec2 P 2CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Variable Identifiers Declaring variables:
Dim VariableName As type
Examples:Students to add example
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
Lec2 P 3CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Visual Basic Standard Data Types
Data type Storage size Example Name Integer 2 bytes iCount Long (integer) 4 bytes lDebt Single(floating-point) 4 bytes fArea Double(floating point) 8 bytes dWidth Currency(scaled integer) 8 bytes cPrice String 1 byte/char sName Variant Varies vData User-defined(Type) size bytes uPaymentDetails
(VB4 provides Byte, Boolean, Date, etc)
Lec2 P 4CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Note
String - used for textual information,length is determined when assigned
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
Lec2 P 5CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Naming Conventions
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
Capitalise the first/significant letter of each word in the name– e.g. iNumberOfSeats
Choose meaningful names– if you cannot pronounce it then it is not meaningful!
Lec2 P 6CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Variable Declaration 1
Variables can be declared in a control event procedure
In this case they are only available within the procedure in which they are declared.
Local to the procedure (Sub routine)
Lec2 P 7CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Constant Identifiers and Naming
Use meaningful names
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 availableConst SEATSAVAILABLE = 100
Note the use of Comments
Lec2 P 8CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Constant Declaration
Constants can be declared in a control event procedure
In this case they are only available within the procedure in which they are declared
Local to the procedure (Sub routine)
Lec2 P 9CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Implicit / Explicit Declaration 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 StringDim iCount As Integer
Explicit declaration is a much better approach to adopt,implicit declaration is for backwards compatibility
Can force Explicit Declaration via Options/Environment option
Lec2 P 10CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Arithmetic Operators
Arithmetic operation Symbol Example
Exponentiation ^ 2^3 = 8 (2*2*2 or 23)
Multiplication & division *, / 2/4 = 0.5Addition & 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"
Lec2 P 11CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Operator Order of Precedence
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:
Arithmetic Comparison LogicalExponentiation (^) Equality (=) NotNegation ( - ) Inequality (<>) AndMultiplication, division (*,/ ) Less than (<) OrInteger division (\) Greater than (>) XorModulo arithmetic (Mod) Less than or Equal to (<=) EqvAddition, subtraction (+,-) Greater than or Equal to (>=) ImpString concatenation (&) Like
Is
Lec2 P 12CP2030 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
Some type conversions must be coded
Lec2 P 13CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Val and Str$
Converting a String to Number
Students to add an example
Converting a Number to a String Students to add an example
Lec2 P 14CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Question Assume that the following variable have been declared :
inum1, inum2, snum3, snum4 and
iresult1, sresult2.
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
Lec2 P 15CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Inputting Strings
eg.
To input a persons first and second name and assign to string variables.
sFirstName = Text1.Text
sSecondName = Text2.Text
Lec2 P 16CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Concatenating Strings Concatenating the input text (strings )
Students to add code
Lec2 P 17CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Handling Numbers VB will automatically convert numbers input from and output to
text and label boxes.
Students to add notes
Lec2 P 18CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Question
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.
For this question use FULL CONVERSIONS via Str$() and Val()
Lec2 P 19CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Referencing properties
Assigning a labels’ caption equal to the text in a text box we would write:
Label1.Caption = Text1.text Label1.Caption = Text1.text
We could write :Form1.Label1.Caption = Form1.Text1.TexForm1.Label1.Caption = Form1.Text1.Text
The Form associated with the statement is taken as the default.
To reference properties on other forms :
Students to add statementStudents to add statement
Lec2 P 20CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
SetFocus Methods 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
Students to add statementStudents to add statement
Our code in our earlier program would be modified to
This sets the Focus back to text box Text1Text1
Lec2 P 21CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
TabIndex
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.
For our earlier program we may want to Tab from control Text1 to Text2 Then Command1
To do this we would set in the properties window– Students to add statements
Lec2 P 22CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Form Load
Code can be attached to a form and executed whenever the form is loaded.
Used for initialisationInitialise variables, arrays, controls
E.g.Text1.Text = ‘xxxxxxx’
Text2.Text = ‘xxxxxxx’
Lec2 P 23CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Scope Of VariablesVariable Declaration 2
Variables can also be declared in the general declarations section of a form (Double click on the form)
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
Lec2 P 24CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Scope of Variables
Shows declarationsat form level, knownas: General Declarations
Shows variabledeclarations withinan event handler
Form1General Declarations
Sub Command1_Click ()
Sub Command2_Click ()
Dim sName1 As StringDim iNum1 As Integer
Dim sName2 As StringDim iNum2 As Integer
Dim sName3 As StringDim iNum3 As Integer
Available variables:sName1, sName2, iNum1, iNum2
Available variables:sName1, sName3, iNum1, iNum3
Lec2 P 25CP2030 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
A static variable can only be declared inside a procedure and only available inside the procedure
Lec2 P 26CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Static Variables: Effects
The effect of using a static variable can be seen below:
Students to add diagrams
This has the same effect as if the variable had been declared at the form’s general declaration levelExcept the scope is local to the procedure
Lec2 P 27CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Scope Of VariablesCode Modules
A code module can be thought of as being a form without a screen attached
In a code module you can declare:
Students to add examples
We can also write code in a module,we will not be looking at this …....yet
Variables declared as Global have scope throughout the application, that is they can be seen across all forms
Lec2 P 28CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Adding a Module to an Application
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
Lec2 P 29CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Code Module Windows Available
A code module consists only ofdeclarations and code
It doesn’t have a form associatedwith it
You can only getaccess to a codeentry window
Lec2 P 30CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Global Variables
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
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
Lec2 P 31CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Visual Basic Declaration Locations
Form 1
Control 1
Code Module 1Dim iNum1 As IntegerGlobal giNum1 As Integer
Dim iNum2 As Integer
Dim iNum3 As IntegerStatic iNum4 As Integer
Control 2Dim iNum5 As IntegerStatic iNum7 As Integer
Form 2
Control 1
Dim iNum8 As Integer
Dim iNum9 As IntegerStatic iNumA As Integer
Control 2Dim iNumB As Integer
Lec2 P 32CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Using Global Variables
Global variables are ideal for storing information which has to be accessed from more than one form
They have scope throughout all forms and modules within an application
Students to add code Students to add code
Global gsName As String
Module1.Bas
Lec2 P 33CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Global Constants
A global constant is basicaly a global variable that you cannot change once the program is running
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
You should use meaningful identifies names Example:
'total number of seats available in hallGlobal Const gTOTALSEATSAVAILABLE% = 100
Lec2 P 34CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Question 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.
Show the code for all command buttons and clearly show where the variables are declared.