CSM18 Visual Basic Section1 Department of Computing UniS 1 Computing • Introduce Visual Basic language - provides excellent facilities for building a user interface • Need to build User-Friendly interfaces • Introduction to Data Base system Access • Control of Access via Visual Basic • Plan and Implement a small project
31
Embed
CSM18 Visual Basic Section1 Department of Computing UniS 1 CSM18 Interactive Computing Introduce Visual Basic language - provides excellent facilities.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CSM18 Visual Basic Section1 Department of Computing UniS1
CSM18 Interactive Computing
• Introduce Visual Basic language - provides excellent facilities for building a user interface
• Need to build User-Friendly interfaces• Introduction to Data Base system Access • Control of Access via Visual Basic• Plan and Implement a small project
CSM18 Visual Basic Section1 Department of Computing UniS2
CSM18 Brief historic overview
Dataoperations
on data
Stored program - keyboard & printer
Keyboard & screen - text only
Keyboard & screen - screen input WYSIWYG
Apple, IBM PC, Microsoft MSDos, Windows
CSM18 Visual Basic Section1 Department of Computing UniS3
CSM18 Procedural Programming
• List of instructions - code
• Execution proceeds from one instruction to next
• Need for sequence control - logical condition• Hence branching and repeating structures• The concept of Algorithm:
a finite number of steps to carryout a specific task eg receipt to bake a cake
• Hence branching and repeating structures
CSM18 Visual Basic Section1 Department of Computing UniS4
CSM18 Algorithms
Algebra
A = B + C
B = A - C
Assignments
A B + C assigns B + C to A
A A + 1 increments A
X A
A B interchanges two numbers
B X
CSM18 Visual Basic Section1 Department of Computing UniS5
CSM18 Declarative Programming
• Declare statements or facts that are true• Declare relations between facts• Interrogate the system - is this true? Response
Yes or No• Good for solving logical problems - don’t have
to tell the computer how to do it as you do in the case of procedural programming via the algorithm
CSM18 Visual Basic Section1 Department of Computing UniS6
CSM18 Spreadsheet computations
• Declare relation between one cell and another
• Limited capability eg can’t put data into a cell based upon the result of a calculation
= B3*1.1
10 11
Cell B3
CSM18 Visual Basic Section1 Department of Computing UniS7
CSM18 Real Time Programming
• Procedural language that can respond to time• At 1200 do . . . . • Used for control of real systems such as power
stations • System can collect data directly from the real
world eg read the temperature of furnace
CSM18 Visual Basic Section1 Department of Computing UniS8
CSM18 Event Driven Programming
• Procedural language to support a user interface• Processes are initiated by an event on screen
eg user clicks a button • Large code split into smaller blocks -
subroutines that are associated with events• A major feature of Visual Basic • All the features of the windows environment are
readily available in Visual Basic
CSM18 Visual Basic Section1 Department of Computing UniS9
CSM18 Visual Basic
• Complex but easy to use development environment
• Integrated Development Environment - IDE provides for all the facilities to build, test and execute a program
• Large code split into smaller blocks - subroutines that are associated with events
CSM18 Visual Basic Section1 Department of Computing UniS10
CSM18 Visual Basic
• A major feature of Visual Basic • All the features of the windows environment
are readily available in Visual Basic
• Exploration of the IDE is the first lab assignment
CSM18 Visual Basic Section1 Department of Computing UniS12
CSM18 Visual Basic IDE
• User Form • Menu Bar• Tool Bar• Tool Box• Project window• Properties window• Immediate window• Locals window• Watch window• Form layout window
CSM18 Visual Basic Section1 Department of Computing UniS13
CSM18 Programming Constructs
• data stored in variables, whose value can change over time
• they can be altered or manipulated by operations, known as program statements
2000 197525
Age = CurrentYear – YearOfBirth
CSM18 Visual Basic Section1 Department of Computing UniS14
CSM18 Programming Constructs
• Assign a value to a variable with the = sign yet conceptually it can be represented as
• Operators work on values & variables to perform• simple arithmetic (+, -, *, /...) • string (text) manipulation• date manipulation
a = 24.5
a 24.5
CSM18 Visual Basic Section1 Department of Computing UniS15
• Values, variables and operators can be put together (using a form of grammar) to form expressions
• An expression
• is a combination of values, variables and operators
• has a value (found by calculating the expression)• is a type of data (e.g. number, string, date…)• can be a direct replacement for a value of the
appropriate type
CSM18 Expressions
CSM18 Visual Basic Section1 Department of Computing UniS16
CSM18 Expressions
Examples:
x+2
“Joe” & “ Bloggs”
Date + 7 (1 week from today)
z * (x + 2) (value 9 if z=3, x=1)
Examples:
x+2
“Joe” & “ Bloggs”
Date + 7 (1 week from today)
z * (x + 2) (value 9 if z=3, x=1)
CSM18 Visual Basic Section1 Department of Computing UniS17
• Variables need to be declared • with the name and the type of data• Integer types (Byte, Integer, Long) for whole
numbers• Floating point types (Single, Double) for numbers
with fractional parts• Dates and times (one type, Date, for both)• Fixed point types (Decimal, Currency) for high
precision (many digits)• String type for text
CSM18 Declarations
CSM18 Visual Basic Section1 Department of Computing UniS18
CSM18 Declarations
Dim index As integer Can be placed anywhere
Private name As String Declarations in General section Only accessible from same module
Public today As Date Declarations in General sectionAccessible anywhere
Dim index As integer Can be placed anywhere
Private name As String Declarations in General section Only accessible from same module
Public today As Date Declarations in General sectionAccessible anywhere
Module - part of the structure of a program or application:
CSM18 Visual Basic Section1 Department of Computing UniS19
CSM18 ModulesThree types of code file• Form module
Defines look and behaviour of Windows
• Class ModuleDefines content and behaviour of objects
• Code (or Standard) ModuleDefines general purpose Subroutines and Functions
Form Module
CodeModule
Class ModuleObject
ObjectObject
Object
FormForm
FormForm
Code
Design Time Run Time
CSM18 Visual Basic Section1 Department of Computing UniS20
• User-interface development• Form modules
Screen layout plus data content plus behaviour
• Description of type of object• Class Module
Can have more than one object of the type
• Definition of general purpose code• Standard Module
Can only be one copy of each module in a program
CSM18 Types of Module
CSM18 Visual Basic Section1 Department of Computing UniS21
• Every module has• General Declarations section• Subroutines (Subs) and Functions
• Form modules also have• User interface elements (which appear on-screen)• Event handlers
• Class modules also have• Event receptors (objects that can respond to
events)
CSM18 Module Structure
CSM18 Visual Basic Section1 Department of Computing UniS22
• Where a variable can be accessed depends on how it is declared• Local scope (Dim) – only within Procedure (Sub or
Function) in which it is declared• Module Scope (Private) – available in every
Procedure within a module• Global Scope (Public) – available throughout
application
• Controlling scope makes it possible to work within contexts, so that variable names can be reused with no conflicts
CSM18 Variable Scope
CSM18 Visual Basic Section1 Department of Computing UniS23
• Public variable is visible throughout all modules
• No two Public variables with the same name
• Private in one module is invisible in another
• Local variable overrides Private or Public with same name
CSM18 Scope Rules
CSM18 Visual Basic Section1 Department of Computing UniS24
• Local variables (within Sub or Function)• Declared with Dim are
reset to zero every time Procedure is called
• Declared with Static, retain their value
Sub Forgettable( )Dim Number As Integer
Number = Number + 1End Sub Always 1 at End Sub
Sub Persistent()Static CallCount As Integer
CallCount = CallCount + 1End Sub Counts up
Sub Forgettable( )Dim Number As Integer
Number = Number + 1End Sub Always 1 at End Sub
Sub Persistent()Static CallCount As Integer
CallCount = CallCount + 1End Sub Counts up
CSM18 Local & Static Variables
CSM18 Visual Basic Section1 Department of Computing UniS25
• A Variant is an all purpose variable• Type is always Variant• Sub-Type matches whatever value is assigned• Sub-Type changes to suit
Private Sub SquareRoot_Click()Dim V As Variant V = 2 Assign an integer V = Sqr(V) Assign a double V = “Root 2 = “ & V Assign a stringEnd Sub
Private Sub SquareRoot_Click()Dim V As Variant V = 2 Assign an integer V = Sqr(V) Assign a double V = “Root 2 = “ & V Assign a stringEnd Sub
CSM18 Variants
CSM18 Visual Basic Section1 Department of Computing UniS26
• A UDT is composed of simple types (Integers, strings etc.)
• Each instance contains one of each component variable
• Must be placed in Standard Modules• Definition is Global (available
throughout program)• Useful for grouping related
information
CSM18 User Defined Types
CSM18 Visual Basic Section1 Department of Computing UniS27
CSM18 An example UDF
Type PhoneEntry Name As String Telephone As StringEnd Type
Dim PE As PhoneEntry PE.Name = “Fred Bloggs” PE.Telephone = “2468” Print PE.Name, PE.Telephone
CSM18 Visual Basic Section1 Department of Computing UniS28
All executable statements must be part of a Sub or Function in VB
• Sub – an operation, delineated by Sub and End Sub, containing a sequence of statements
• a Sub ‘call’ is a statement that invokes the Sub (executes the operation)
• Function – similar to a Sub, but returning a value as a result
• a function call is an expression – can be used as a value in another expression or statement
• Both Subs and Functions can have parameters or arguments – variables used to get information in and/or out of the Sub/Function
CSM18 Subs & Functions
CSM18 Visual Basic Section1 Department of Computing UniS29
CSM18 Subs & Functions
Sub Greeting (Name As String)
MsgBox “Hello “ & Name
End Sub
Greeting (“John”)
Greeting “John”
Call Greeting (“John”)
CSM18 Visual Basic Section1 Department of Computing UniS30
• A special form of Sub that Visual Basic associates with an event
• Mouse operation• Key-press• Signal from another application
• An event handler is called automatically when the event happens