Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic: An Object Oriented Approach 4: Simple Programming in VB.

Similar presentations


Presentation on theme: "Visual Basic: An Object Oriented Approach 4: Simple Programming in VB."— Presentation transcript:

1 Visual Basic: An Object Oriented Approach 4: Simple Programming in VB

2 The Global object in VB Global this is the default object – no need to name it a huge library of in-built Subs and Functions Maths, Strings, Date and Time, simple user interactions etc. all standard programming operations are provided by Global in VB

3 Objects in all VB programs App The Application object – provides program specific info – name, folder Clipboard An object for transferring information between Windows programs Forms A Collection of all of the Forms (Windows) in this program Printers/Printer A Collection of all printers installed on the system, and the default printer Screen An object that provides information about the main display device

4 Subs and 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) defined – variables used to get information in and/or out of the Sub/Function

5 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

6 A Sample Sub Sub Greeting(someone As String) MsgBox “Hello ” & someone End Sub Sub Greeting(someone As String) MsgBox “Hello ” & someone End Sub Sub framework Sub body Parameter Greeting “Fred” Calling the Sub Result

7 A Sample Function Function Product(n1 As Integer, n2 As Integer) As Integer Product = n1 * n2 End Function Function Product(n1 As Integer, n2 As Integer) As Integer Product = n1 * n2 End Function Function framework Function body Parameters X = Product(Y, Z) Calling the Function Return type Result is that X contains the product of Y and Z e.g. Y = 3, Z = 4 -> X = 12

8 Modules and Classes 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 Subs and Functions

9 Selecting module type 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 individual items or general purpose code Standard Module Can only be one copy of each module in a program

10 Module Structure Every module has (General Declarations) section Subs and Functions section (may have none in it) Class modules (including forms) also have Event receptors (objects that can respond to events) Event handlers Form modules also have User interface elements (which appear on-screen)

11 Simple Data Types Default variable type is Variant Can accommodate any simple data item Single values: numbers, dates, times, currency, strings of text characters Will deal automatically with changing types Specific types of data should be stored in Typed variables 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

12 Declaring Variables Visual Basic automatically allocates variables as they are used bad idea to rely on this no security of type, possibility of creating new variables with typos Suggest declaring variables before use Declaration allocates storage space before use assigns a type to a variable Can set up VB to enforce prior declaration Good security measure

13 Sample variable declarations Dim index As integer‘ Can be placed anywhere Private name As String‘ Must be in general declarations section ‘ Only accessible from same module Public today As Date‘ Must be in general declarations section ‘ Accessible anywhere Dim index As integer‘ Can be placed anywhere Private name As String‘ Must be in general declarations section ‘ Only accessible from same module Public today As Date‘ Must be in general declarations section ‘ Accessible anywhere Match variable type to information being stored in it Efficient in terms of space – each variable occupies only the space it needs Efficient in terms of time – quicker to process if VB has knowledge of storage format

14 Variable Scope Where a variable can be accessed on depends on how it is declared Local scope (Dim) – only within Procedure (Sub or Function) it is declared in 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

15 Scope Rules No two Publics with the same name (unless qualified by a class or form) X in Module 1 is accessible in Module 2 Private in one module is invisible in another Y is Single in Module 1, String in Module 2 Local variable overrides Private or Public with same name X is String in Module1, ScopeTest, Integer elsewhere 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 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 Private Y As String Sub Test() X = 2 Y = “A String” End Sub Private Y As String Sub Test() X = 2 Y = “A String” End Sub Module 1 Module 2

16 Local and 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 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

17 Variants 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 cmdSquareRoot_Click() Dim V As Variant ‘ same as ‘Dim V’ V = 2 ‘ Assign an integer V = Sqr(V)‘ Assign its square ‘ root (converts to double) V = “Root 2 = “ & V ‘ It will be converted ‘ to a string) End Sub Private Sub cmdSquareRoot_Click() Dim V As Variant ‘ same as ‘Dim V’ V = 2 ‘ Assign an integer V = Sqr(V)‘ Assign its square ‘ root (converts to double) V = “Root 2 = “ & V ‘ It will be converted ‘ to a string) End Sub

18 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 Type PhoneEntry Name As String Telephone As String End Type 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 Dim PE As PhoneEntry PE.Name = “Fred Bloggs” PE.Telephone = “2468” Print PE.Name, PE.Telephone

19 Objects and References An Object is created in memory in response to New Does not occupy a variable A Reference is an object variable, and provides access to an object Does not contain it Do not assign an object to a reference, Set the reference Object does not move Dim MyObject As MyClass Set MyObject = New MyClass Dim MyObject As MyClass Set MyObject = New MyClass Declares a reference variable Sets the reference Creates an object

20 References Can have multiple references to one object Each accesses same object Object persists in memory while there is at least one reference to it Set MyObject = New MyObject Set MyReference = MyObject MyObject.Value = 6 Print MyReference.Value ‘6 Set MyObject = Nothing Set MyReference = Nothing ‘ object is destroyed at last ‘ statement Set MyObject = New MyObject Set MyReference = MyObject MyObject.Value = 6 Print MyReference.Value ‘6 Set MyObject = Nothing Set MyReference = Nothing ‘ object is destroyed at last ‘ statement


Download ppt "Visual Basic: An Object Oriented Approach 4: Simple Programming in VB."

Similar presentations


Ads by Google