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.

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
VBA Modules, Functions, Variables, and Constants
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
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.
Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters.
Developing Software Applications Introduction to Variables & Data Types in VB State Transition Diagrams.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Lec4 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 4 Back To Index v Iteration - Looping v Arrays.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Variables and Constants
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Lecture 8 Visual Basic (2).
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Chapter 2: Using Data.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Visual Basic IITG to be expanded. What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
CNS 1120 Exam 1 Review. Programming Language Elements Syntax = the rules to follow Syntax = the rules to follow Semantics = the meaning of symbols Semantics.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter-1 Introduction to Visual Basic GUI- A GUI is a graphical (rather than purely textual) user interface to a computer. The term came into existence.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Programming with Microsoft Visual Basic th Edition
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
CECS 5020 Computers in Education Visual Basic Variables and Constants.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Week 1 Lecture 1 Slide 1 CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton CP2028 Visual Basic Programming 2 v Week.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
VISUAL BASIC 6.0 Designed by Mrinal Kanti Nath.
A variable is a name for a value stored in memory.
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
Visual Basic 6 (VB6) Data Types, And Operators
Variables and Arithmetic Operations
Chapter 4: The Selection Structure
Variables and Arithmetic Operations
Chapter 6 Variables What is VBScript?
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
Introduction to Programming
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Presentation transcript:

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

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

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)

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

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!

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)

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

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)

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

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 +, = 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 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

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

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

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

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

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

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

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()

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

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

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

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’

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

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

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

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

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

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

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

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

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

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

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

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.