Visual Basic An Object Oriented Approach 4 Simple
Visual Basic: An Object Oriented Approach 4: Simple Programming in VB
The Global object in VB n Global n n this is the default object – no need to name it a huge library of in-built Subs and Functions n n Maths, Strings, Date and Time, simple user interactions etc. all standard programming operations are provided by Global in VB
Objects in all VB programs n App n n Clipboard n n A Collection of all of the Forms (Windows) in this program Printers/Printer n n An object for transferring information between Windows programs Forms n n The Application object – provides program specific info – name, folder A Collection of all printers installed on the system, and the default printer Screen n An object that provides information about the main display device
Subs and Functions n All executable statements must be part of a Sub or Function in VB n Sub – an operation, delineated by Sub and End Sub, containing a sequence of statements n n Function – similar to a Sub, but returning a value as a result n n a Sub ‘call’ is a statement that invokes the Sub (executes the operation) 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
Event Handlers n A special form of Sub that Visual Basic associates with an event n n Mouse operation Key-press Signal from another application An event handler is called automatically when the event happens n Program can respond to external stimuli
A Sample Sub Parameter Sub Greeting(someone As String) Sub framework Msg. Box “Hello ” & someone Sub body End Sub Calling the Sub Greeting “Fred” Result
A Sample Function Parameters Function framework Function Product(n 1 As Integer, n 2 As Integer) As Integer Product = n 1 * n 2 End Function Calling the Function X = Product(Y, Z) Function body Return type Result is that X contains the product of Y and Z e. g. Y = 3, Z = 4 -> X = 12
Modules and Classes n Three types of code file n Form module n n Class Module n n Defines look and behaviour of Windows Defines content and behaviour of objects Code (or Standard) Module n Defines general purpose Subs and Functions
Selecting module type n User-interface development n n n Description of type of object n n n Form modules Screen layout plus data content plus behaviour Class Module Can have more than one object of the type Definition of individual items or general purpose code n n Standard Module Can only be one copy of each module in a program
Module Structure n Every module has n n n Class modules (including forms) also have n n n (General Declarations) section Subs and Functions section (may have none in it) Event receptors (objects that can respond to events) Event handlers Form modules also have n User interface elements (which appear on-screen)
Simple Data Types n Default variable type is Variant n Can accommodate any simple data item n n n 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 n n n 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
Declaring Variables n Visual Basic automatically allocates variables as they are used n bad idea to rely on this n n Suggest declaring variables before use n Declaration n no security of type, possibility of creating new variables with typos allocates storage space before use assigns a type to a variable Can set up VB to enforce prior declaration n Good security measure
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 n Match variable type to information being stored in it n Efficient in terms of space – each variable occupies only the space it needs n Efficient in terms of time – quicker to process if VB has knowledge of storage format
Variable Scope n Where a variable can be accessed on depends on how it is declared n n 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
Scope Rules Public X As Integer Private Y As Single n Module 1 Sub Scope. Test( ) Dim X As String X = “Hello Mum” Y = 3. 14 End Sub Private Y As String Sub Test 2() X=4 Sub Test() End Sub X=2 Y = “A String” End Sub Module 2 No two Publics with the same name (unless qualified by a class or form) n n Private in one module is invisible in another n n X in Module 1 is accessible in Module 2 Y is Single in Module 1, String in Module 2 Local variable overrides Private or Public with same n X is String in Module 1, Scope. Test, Integer elsewhere
Local and Static Variables n Local variables (within Sub or Function) n n 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 Persistent() Dim Call. Count As Integer Call. Count = Call. Count + 1 End Sub ‘ Counts up
Variants n A Variant is an all purpose variable n n n Type is always Variant Sub-Type matches whatever value is assigned Sub-Type changes to suit Private Sub cmd. Square. Root_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
User Defined Types n n n 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 Phone. Entry Name As String Telephone As String End Type Dim PE As Phone. Entry PE. Name = “Fred Bloggs” PE. Telephone = “ 2468” Print PE. Name, PE. Telephone
Objects and References n An Object is created in memory in response to New n n A Reference is an object variable, and provides access to an object n n Does not occupy a variable Does not contain it Do not assign an object to a reference, Set the reference n Object does not move Declares a reference variable Dim My. Object As My. Class Set My. Object = New My. Class Creates an object Sets the reference
References n Can have multiple references to one object n n Each accesses same object Object persists in memory while there is at least one reference to it Set My. Object = New My. Object Set My. Reference = My. Object. Value = 6 Print My. Reference. Value Set My. Object = Nothing Set My. Reference = Nothing ‘ object is destroyed at last ‘ statement ‘ 6
- Slides: 20