# CSM18 Visual Basic Section1 Department of Computing UniS 1 CSM18 Interactive Computing Introduce Visual Basic language - provides excellent facilities.

## Presentation on theme: "CSM18 Visual Basic Section1 Department of Computing UniS 1 CSM18 Interactive Computing Introduce Visual Basic language - provides excellent facilities."— Presentation transcript:

CSM18 Visual Basic Section1 Department of Computing UniS 1 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 UniS 2 CSM18 Brief historic overview Data operations 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 UniS 3 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 UniS 4 CSM18 Algorithms Algebra A = B + C B = A - C Assignments A B + Cassigns B + C to A A A + 1increments A X A A Binterchanges two numbers B X

CSM18 Visual Basic Section1 Department of Computing UniS 5 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 UniS 6 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 1011 Cell B3

CSM18 Visual Basic Section1 Department of Computing UniS 7 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 UniS 8 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 UniS 9 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 UniS 10 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 UniS 12 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 UniS 13 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

CSM18 Visual Basic Section1 Department of Computing UniS 14 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 UniS 15 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 UniS 16 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 UniS 17 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 UniS 18 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 section Accessible 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 section Accessible anywhere Module - part of the structure of a program or application:

CSM18 Visual Basic Section1 Department of Computing UniS 19 CSM18 Modules Three types of code file Form module Defines look and behaviour of Windows Class Module Defines content and behaviour of objects Code (or Standard) Module Defines general purpose Subroutines and Functions Form Module Code Module Class Module Object Form Code Design TimeRun Time

CSM18 Visual Basic Section1 Department of Computing UniS 20 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 UniS 21 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 UniS 22 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 UniS 23 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 Module 1 Public X As Integer Private Y As Single Sub ScopeTest( ) Dim X As String X = “Hello Mum” Y = 3.14 End Sub Sub Test2() X = 4 End Sub Module 2 Private Y As String Sub Test() X = 2 Y = “A String” End Sub X in Module 1 is accessible in Module 2 Y is Single in Module 1, String in Module 2 X is String in Module1, ScopeTest, Integer elsewhere Module 1 Public X As Integer Private Y As Single Sub ScopeTest( ) Dim X As String X = “Hello Mum” Y = 3.14 End Sub Sub Test2() X = 4 End Sub Module 2 Private Y As String Sub Test() X = 2 Y = “A String” End Sub X in Module 1 is accessible in Module 2 Y is Single in Module 1, String in Module 2 X is String in Module1, ScopeTest, Integer elsewhere

CSM18 Visual Basic Section1 Department of Computing UniS 24 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 + 1 End Sub Always 1 at End Sub Sub Persistent() Static CallCount As Integer CallCount = CallCount + 1 End Sub Counts up Sub Forgettable( ) Dim Number As Integer Number = Number + 1 End Sub Always 1 at End Sub Sub Persistent() Static CallCount As Integer CallCount = CallCount + 1 End Sub Counts up CSM18 Local & Static Variables

CSM18 Visual Basic Section1 Department of Computing UniS 25 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 string End 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 string End Sub CSM18 Variants

CSM18 Visual Basic Section1 Department of Computing UniS 26 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 UniS 27 CSM18 An example UDF Type PhoneEntry Name As String Telephone As String End Type Dim PE As PhoneEntry PE.Name = “Fred Bloggs” PE.Telephone = “2468” Print PE.Name, PE.Telephone

CSM18 Visual Basic Section1 Department of Computing UniS 28 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 UniS 29 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 UniS 30 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 Program can respond to external stimuli CSM18 Event Handlers Private Sub Botton1_Click() MsgBox “Hello World” End Sub

End of Section 1

Download ppt "CSM18 Visual Basic Section1 Department of Computing UniS 1 CSM18 Interactive Computing Introduce Visual Basic language - provides excellent facilities."

Similar presentations