Ministry of Higher Education And Scientific Research Babylon University College of Material Engineering Subject: Programming in Visual Basic Polymer Engineering Department 2015-2016 Language Class: Second Year Lecturer: Hussein Mohammed Salman Lecture-7: Variables and Constants Variables are locations in memory used by Visual Basic to hold data and/or information needed by your application. Rules used in naming variables: 1. Must begin with letter. 2. Can’t contain an embedded type-declaration character. 3. Must not exceed 255 characters. 4. They can’t be the same as restricted keywords (a restricted keyword is a word that Visual Basic uses as part of its language. This includes predefined statements such as “If and Loop”, functions such as “Len and Abs”, and operators such as “Or and Mod”). Visual Basic Data Types Variable Declaration There are three ways for a variable to be typed (declared): 1. Default 2. Implicit 3. Explicit 1: Default If variables are not implicitly or explicitly typed, they are assigned the This lecture allowed on: www.uobabylon.iq.edu 1 Ministry of Higher Education And Scientific Research Babylon University College of Material Engineering Subject: Programming in Visual Basic Polymer Engineering Department 2015-2016 Language Class: Second Year Lecturer: Hussein Mohammed Salman variant type by default. The variant data type is a special type used by Visual Basic that can contain numeric, string, or date data. 2:Implicitly To implicitly type a variable, use the corresponding suffix shown above in the data type table. For example, TextValue$ = "This is a string" creates a string variable, while Amount% = 300 creates an integer variable. 3:Explicitly There are many advantages to explicitly typing variables. Primarily, we insure all computations are properly done, mistyped variable names are easily spotted, and Visual Basic will take care of insuring consistency in upper and lower case letters used in variable names. Because of these advantages, and because it is good programming practice, we will explicitly type all variables. Static Function total (num) This makes all the variables in the procedure static regardless of whether they are declared with Static, Dim, and Private. You can place Static in front of any Sub or Function Procedure heading, including event procedures and those declared as Private. Constants: Constant also store values, but as the name implies, those values remains constant throughout the execution of an application. Using constants can make your code more readable by providing meaningful names instead of numbers. There are a number of built –in constants in Visual Basic. There are two sources for constants: System-defined constants are provided by applications and controls. Visual Basic constants are listed in the Visual Basic (VB). User-defined constants are declared using the Const statement. It is a space in memory filled with fixed value that will not be changed. For example: Const X=3.14156 Private Const X=3.14156 Public Const X=3.14156 Constant for procedure Constant for form and all procedure Constant for all forms This lecture allowed on: www.uobabylon.iq.edu 2 Ministry of Higher Education And Scientific Research Babylon University College of Material Engineering Subject: Programming in Visual Basic Polymer Engineering Department 2015-2016 Language Class: Second Year Lecturer: Hussein Mohammed Salman Visual Basic Operators: 1- The simplest operators carry out arithmetic operations. These operations in their order of precedence are: Operation Code Operation ^ Exponent *,/ Multiplication and division \ Integer division Mod Modulus – rest of division -,+ Subtraction and addition 2- To Concatenate two strings, use the & symbol or the + symbol 3- There are six Comparison operators in Visual Basic. Operation Code Comparison > Greater than < Less than >= Greater than or equal to <= Less than or equal to = Equal to < > or > < Not equal to 4-There are three logical operators: Operation Code Operation Not Logical not And Logical and Or Logical or Note: Logical operators follow arithmetic operators in precedence. Examples: Private Sub Command1_ click ( ) Picture1.Print 7\3 Picture1.Print 7 Mod 3 Picture1.Print "My"&" Name" Picture1.Print 10/3*15/3*3/2-9/3/2*4*3 Picture1.Print 4E3-3E2/5/3E1 Picture1.Print 4E-8/2*5E8/6E16*4E14*3 Picture1.Print 4/3^3/4^2*3^4*2^4 This lecture allowed on: www.uobabylon.iq.edu 3 Ministry of Higher Education And Scientific Research Babylon University College of Material Engineering Subject: Programming in Visual Basic Polymer Engineering Department 2015-2016 Picture1.Print Picture1.Print Picture1.Print Picture1.Print Language Class: Second Year Lecturer: Hussein Mohammed Salman 27^1/3-2E2^3*4E-4/4^3 (3-3^3)/((3^2+3^3)/3^5)/3^4 (14+2^5/2^4)^(1/4)+((15-5*4)/(3^2-2^3/2)) (((3^(3^3)/3^3)^(1/3)+3^4)^(1/3)* 5^2)^(1/2) Programming Example Stopwatch Application - Attaching Code All that’s left to do is attach code to the application. We write code for every event a response is needed for. In this application, there are three such events: clicking on each of the command buttons. Click the down arrow in the Object box and select the object named (general). The Procedure box will show (declarations). Here, you declare three form level variables: Option Explicit Dim StartTime As Variant Dim EndTime As Variant Dim ElapsedTime As Variant The Option Explicit statement forces us to declare all variables. The other lines establish StartTime, EndTime, and ElapsedTime as variables global within the form. Select the cmdStart object in the Object box. If the procedure that appears is not the Click procedure, choose Click from the procedure box. Type the following code which begins the timing procedure. Note the Sub and End Sub statements are provided for you: Sub cmdStart_Click () ‘Establish and print starting time StartTime = Now lblStart.Caption = Format(StartTime, "hh:mm:ss") lblEnd.Caption = "" lblElapsed.Caption = "" End Sub In this procedure, once the Start Timing button is clicked, we read the current time and This lecture allowed on: www.uobabylon.iq.edu 4 Ministry of Higher Education And Scientific Research Babylon University College of Material Engineering Subject: Programming in Visual Basic Polymer Engineering Department 2015-2016 Language Class: Second Year Lecturer: Hussein Mohammed Salman print it in a label box. We also blank out the other label boxes. In the code above (and in all code in these notes), any line beginning with a single quote (‘) is a comment. You decide whether you want to type these lines or not. They are not needed for proper application operation. Now, code the cmdEnd button. Sub cmdEnd_Click () ‘Find the ending time, compute the elapsed time ‘Put both values in label boxes EndTime = Now ElapsedTime = EndTime – StartTime lblEnd.Caption = Format(EndTime, "hh:mm:ss") lblElapsed.Caption = Format(ElapsedTime, "hh:mm:ss") End Sub Here, when the End Timing button is clicked, we read the current time (End Time), compute the elapsed time, and put both values in their corresponding label boxes. Finally the cmdExit button: Sub cmdExit_Click () End End Sub This lecture allowed on: www.uobabylon.iq.edu 5 Ministry of Higher Education And Scientific Research Babylon University College of Material Engineering Subject: Programming in Visual Basic Polymer Engineering Department 2015-2016 Language Class: Second Year Lecturer: Hussein Mohammed Salman Exercises: 1) Compute depending on the precedence rules (step by step) : 15/3*8/3*9/2-15/3/12*4*3 = 6/3^3/4^2*3^4*2^4 = (3+3^3)/((3-3^3)/3^7)/3^5 = (23+2^6/2^4)^(1/3)+((10-5*4)/(3^2-2^3/2)) = (((3^(3^3)/3^3)^(1/3)-1)^(1/3)* 2^3)^(1/2) = 2) 3) 4) 5) 6) What are the naming rules of variables ? List at least seven types of variables in VB with its suffix ? Discuss in detail types of the variables declaration? What are the declaration scopes in VB? What the different between the static declaration and other declarations? This lecture allowed on: www.uobabylon.iq.edu 6