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

Slides:



Advertisements
Similar presentations
Integrated Business Applications with Databases (D3) Jenny Pedler
Advertisements

CSM18 Visual Basic Section1 Department of Computing UniS 1 CSM18 Interactive Computing Introduce Visual Basic language - provides excellent facilities.
Pemrograman VisualMinggu …2… Page 1 MINGGU Ke Dua Pemrograman Visual Pokok Bahasan: Console Application Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Chapter 7: User-Defined Functions II
The Web Warrior Guide to Web Design Technologies
Variables and Constants
VBA Modules, Functions, Variables, and Constants
Using Visual Basic 6.0 to Create Web-Based Database Applications
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Programming on the Go Chapters 1and 2 Siddharth Patel
Review of C++ Programming Part II Sheng-Fang Huang.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Variables and Constants
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Why to Create a Procedure
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
Lecture 8 Visual Basic (2).
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Chapter 6: User-Defined Functions
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
Outline Software and Programming Program Structure Tools for Designing Software Programming Languages Introduction to Visual Basic (VBA)
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 C++ Syntax and Semantics, and the Program Development Process.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Applications Development
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
CNS 1120 Exam 1 Review. Programming Language Elements Syntax = the rules to follow Syntax = the rules to follow Semantics = the meaning of symbols Semantics.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter-1 Introduction to Visual Basic GUI- A GUI is a graphical (rather than purely textual) user interface to a computer. The term came into existence.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Programming with Microsoft Visual Basic th Edition
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
Copyright © Don Kussee 1120-Ch3 #481 CNS 1120 Chapter 3 Data Types and Variables 1120-Ch3.PPT.
CHAPTER THREE Representing Data: Constants and Variables.
INTRODUCTION CHAPTER #1 Visual Basic.NET. VB.Net General features It is an object oriented language  In the past VB had objects but focus was not placed.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Representing Data: Constants and Variables
Chapter 9: Value-Returning Functions
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
2. Understanding VB Variables
Microsoft Visual Basic 2005 BASICS
CS285 Introduction - Visual Basic
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
C Language B. DHIVYA 17PCA140 II MCA.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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