Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS285 Introduction - Visual Basic

Similar presentations


Presentation on theme: "CS285 Introduction - Visual Basic"— Presentation transcript:

1 CS285 Introduction - Visual Basic
Programming paradigms: Procedural Programming Declarative Programming Spreadsheet programming Real-Time programming Event Driven Programming Recommended Texts: Visual Basic - an Object Oriented Approach by A McMonnies An Introduction to Visual Basic by D Schneider Department of Computing UniS CS285 VB-1 Department of Computing UniS

2 Procedural Programming
CS316 7/12/99 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 Department of Computing UniS CS285 VB-1 Department of Computing UniS

3 Declarative Programming
CS316 7/12/99 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 Department of Computing UniS CS285 VB-1 Department of Computing UniS

4 Spreadsheet Computations
CS316 7/12/99 Spreadsheet Computations Declare relation between one cell and another = B3*1.1 10 11 Cell B3 Formulae 10 Value Limited capability eg can’t put data into a cell based upon the result of a calculation Department of Computing UniS CS285 VB-1 Department of Computing UniS

5 Department of Computing UniS
CS316 7/12/99 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 via an analog to digital converter Department of Computing UniS CS285 VB-1 Department of Computing UniS

6 Event Driven Programming
CS316 7/12/99 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 Department of Computing UniS CS285 VB-1 Department of Computing UniS

7 Department of Computing UniS
CS316 7/12/99 Visual Basic Integrated Development Environment - IDE provides for all the facilities to build, test and execute a program Exploration of the IDE is the first lab assignment Department of Computing UniS CS285 VB-1 Department of Computing UniS

8

9 IDE Visual Basic - ToolBox
CS316 7/12/99 IDE Visual Basic - ToolBox Department of Computing UniS CS285 VB-1 Department of Computing UniS

10 IDE VB - PropertiesWindow
CS316 7/12/99 IDE VB - PropertiesWindow Department of Computing UniS CS285 VB-1 Department of Computing UniS

11 Department of Computing UniS
CS316 7/12/99 IDE VB Project - Window Department of Computing UniS CS285 VB-1 Department of Computing UniS

12 Department of Computing UniS
CS316 7/12/99 IDE VB Form - TextBox Department of Computing UniS CS285 VB-1 Department of Computing UniS

13 Department of Computing UniS
CS316 7/12/99 IDE VB - Text1 Code Department of Computing UniS CS285 VB-1 Department of Computing UniS

14 Department of Computing UniS
CS316 7/12/99 IDE VB - Execution Run Start Break End Also icons on the ToolBar Department of Computing UniS CS285 VB-1 Department of Computing UniS

15 IDE VB - Event Text1_Click
CS316 7/12/99 IDE VB - Event Text1_Click Department of Computing UniS CS285 VB-1 Department of Computing UniS

16 Department of Computing UniS
CS316 7/12/99 IDE VB - TextBox Events Department of Computing UniS CS285 VB-1 Department of Computing UniS

17 IDE VB - Immediate Window
CS316 7/12/99 IDE VB - Immediate Window Used to display values of variables when program interrupted Department of Computing UniS CS285 VB-1 Department of Computing UniS

18 Department of Computing UniS
CS316 7/12/99 Visual Basic IDE User Form Menu Bar Tool Bar Tool Box Project window - lists the files that constitute the program Properties window Immediate window Locals window Watch window Form layout window Department of Computing UniS CS285 VB-1 Department of Computing UniS

19 Visual Basic Structure
Forms: containing interface objects Form Objects: contain VB Code in event driven subroutines (Sub) General: area for each Form to hold declarations for the Form and definitions of Procedures: Sub Procedures Function Procedures Modules: contains Global Visual Basic Code in the form of Procedures Class Modules (Objects): provide a Object Oriented programming environment Department of Computing UniS CS285 VB-1 Department of Computing UniS

20 Visual Basic Program Structure
Department of Computing UniS CS285 VB-1

21 VB Programming Constructs
Data stored in variables that must be declared Assignment statement = Operators: simple arithmetic (+, -, *, /...) string (text) manipulation date manipulation Event Handlers Department of Computing UniS CS285 VB-1

22 Visual Basic Event Handlers
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 Private Sub Botton1_Click() MsgBox “Hello World” End Sub Department of Computing UniS CS285 VB-1

23 Visual Basic Declarations
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 Department of Computing UniS CS285 VB-1

24 Visual Basic Variable Scope
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 Department of Computing UniS CS285 VB-1

25 Visual Basic 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 Department of Computing UniS CS285 VB-1

26 Visual Basic Module Structure
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) Department of Computing UniS CS285 VB-1

27 VB Local & Static Variables
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() Dim CallCount As Integer CallCount = CallCount + 1 End Sub Counts up Department of Computing UniS CS285 VB-1

28 Visual Basic Variant Type
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 = Assign an integer V = Sqr(V) Assign a double V = “Root 2 = “ & V Assign a string End Sub Department of Computing UniS CS285 VB-1

29 Department of Computing UniS
VB User Defined Types 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 Department of Computing UniS CS285 VB-1

30 Visual Basic - 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 Department of Computing UniS CS285 VB-1

31 Visual Basic Subs & Functions
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 Department of Computing UniS CS285 VB-1

32 Department of Computing UniS
Visual Basic Subs Sub Greeting (Name As String) MsgBox “Hello “ & Name End Sub Greeting (“John”) Greeting “John” Call Greeting (“John”) Department of Computing UniS CS285 VB-1

33 Visual Basic Functions
Function Add (X As Integer, Y As Integer) Add = x + Y End Add MsgBox ( Add(2,3)) Department of Computing UniS CS285 VB-1

34 CS316 7/12/99 End of Section 1 Department of Computing UniS


Download ppt "CS285 Introduction - Visual Basic"

Similar presentations


Ads by Google