Introduction to VB 6 PPCC Course Winter 2004
Introduction to VB 6 PPCC Course Winter 2004 Week 0 – Background Information 1/27/2004 Copyright © 2004, Tore Bostrup PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup
Course Outline, Week 0 Background Information – Computer Science Basics Binary Numbers Data Types Instructions and Data Compilers and Interpreters Variables and Assignments – Object Orientation Basics What is an object Visual Basic built-in “Objects” – Windows Environment Basics Message-based and Event-Driven Architecture – The different VB’s (VB, VBA, VBScript) 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 2
Binary Numbers From the earliest days of computers, they have been built with technology that provides two states: – ON – OFF This means that all the data is represented by a series of 0’s and 1’s Logically, a 0 is OFF and a 1 is ON Physically, that is not always the case We call the unit that holds a 0 or a 1 a BIT. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 3
Binary Numbers Computer memory is organized in groups of bits, often called a WORD. The computer architecture defines the WORD length. Most of today’s PC’s have a word length of 32 bits. Both Intel and AMD have 64 -bit architectures in production, but very few vendors have started to offer these in anything other than server class machines. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 4
Binary Numbers So what can we store in a group of bits? – One bit: 0 or 1, i. e. two values – Two bits: 00, 01, 10, 11 (0, 1, 2, 3), i. e. four values – Three bits: 000, 001, 010, 011, 100, 101, 110, 111 (0, 1, 2, 3, 4, 5, 6, 7), i. e. eight values – Generally: n bits can hold 2^n values, 0 to 2^n – 1 – Get used to the 2^n values: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 5
Binary Numbers Decoding a binary value: – 01011110 – Most significant bit is rightmost (of course) – Starting with the least significant bit, and proceeding towards the most significant bit, we get: 0*2^0 + 1*2^1 + 1*2^2 + 1*2^3 + 1*2^4 + 0*2^5 + 1*2^6 + 0*2^7 Or 0+2+4+8+16+0+64+0 = 94 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 6
Binary Numbers Regardless of the WORD length, it is often useful to deal with groups of eight bits. This is sufficient to represent all western languages’ common symbols. A group of 8 bits is called a BYTE. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 7
Binary Numbers A BYTE: – 8 bits: 0000 to 1111 0 through 255 (2^8 – 1) ASCII – ASCII (American Standard Coding for Information Interchange) is a standard for how to map byte values to characters. ASCII actually only defines the first 128 values ANSI – American National Standards Institute One of many ANSI standards are for character encoding and is used by Windows. Often confused with ASCII. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 8
ASCII Charts The following ASCII chart is from – http: //www. cdrummond. qc. ca/cegep/informat/Professeurs/Alain/files/ascii. htm The ASCII-II chart on that page does not match the Windows ANSI character codes, so disregard it… The ANSI Chart is from – http: //h 18009. www 1. hp. com/fortran/docs/vf-html/lref/pg 9 ansi 2. htm 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 9
ASCII Chart 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 10
ANSI Character Codes Chart 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 11
Binary Numbers Beyond ASCII – ANSI standards have expanded on the ASCII codes to include multiple languages, etc. – 8 bits is not sufficient to accommodate several Far East languages. A 16 -bit character encoding known as UNICODE is used to accommodate these languages. – Starting with Windows 2000, text files are stored as UNICODE characters by default. – Unicode characters for western languages tend to consist of a byte containing a zero value, and a byte containing the ASCII/ANSI value. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 12
Binary Numbers Because binary numbers quickly get lots of digits, it is common to use Hexadecimal (Hex for short) values. That means that instead of using powers of 10, we use powers of 16, with the digits 0 through 9 and A (=10) through F (=15). One hex digit is represented by four bits, and a Byte value can be shown as two hex digits (00 – FF). 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 13
Binary Numbers Negative Numbers – In order to allow negative values, the first bit (most significant) is interpreted as a “sign bit”. – 0 means the number is positive, 1 means it is negative. – Instead of interpreting the remaining bits the same, negative numbers are interpreted by INVERTING each bit. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 14
Negative Numbers One’s Complement – By only inverting the bits, we get two different values for 0 – a positive 0 (0000) and a negative 0 (1111). – This is known as One’s Complement. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 15
Negative Numbers Two’s Complement – To avoid this duplicity, we first subtract 1 from the POSITIVE value to be inverted, and then invert the bits. – This results in -1 being represented as: 0001 Subtract 1: 0000 Invert: 1111 – This is called Two’s Complement 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 16
Negative Numbers Two’s Complement – This allows the “largest” negative numbers to be one “larger” than the largest positive number: 0, Positive: 1 to 2^(n-1) – 1 Negative: -1 to –(2^(n-1)) – A 16 -bit signed value can contain the following values: 0 to 32767 -1 to -32768 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 17
Binary Numbers and VB 6 Whole Number values are kept in 16 bit or 32 -bit VARIABLES. – VB 6: Integer is 16 -bit Long is 32 -bit VB 6 Byte variables are NOT SIGNED. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 18
Binary Numbers Other types of Numbers – Floating Point One group of bits hold the significant digits (Mantissa) Another group of bits holds the Exponent Floating point values are NOT ACCURATE. After simple arithmetic, a value that may look like for example 0. 45 may actually be 0. 4499… 997, so testing for equal to 0. 45 WILL FAIL! 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 19
Binary Numbers Other types of Numbers – Fixed Precision Representation can vary VB 6 has Decimal and Currency types which are fixed precision. Rounding may still be an issue, unless the full precision is used. VB 6’s Currency type has FOUR decimals, so for example $1 / 3 is $0. 3333, not $0. 33 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 20
Binary Numbers Dates – Dates are usually represented as double precision floating point values, with the integer portion representing the number of days since some predefined “start date”, and the fraction as a fraction of a 24 hr period. – VB 6 uses the start of (midnight) 12/30/1899 as its calendar “start date”. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 21
VB 6 Data Types Numeric Data Types – – – – 1/27/2004 Boolean: True or False (-1 and 0) Byte: 8 -bit unsigned whole number Integer: 16 -bit signed whole number Long: 32 -bit signed whole number Decimal: Up to 29 (decimal) digits (total), maximum 28 digits to the right of the decimal point Currency: Fixed Point (four decimals), - value range is -922, 337, 203, 685, 477. 5808 to 922, 337, 203, 685, 477. 5807 Single: Single Precision floating point Double: Double Precision floating point PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 22
VB 6 Data Types Other – Date Contains a date and time value stored as a Double with offset from midnight 12/30/1899 – String Variable or fixed length text strings – Object Reference to an instantiated object – Variant The “chameleon” variable – it can be any type of value 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 23
Instructions and Data The CPU – The CPU contains several REGISTERS and knows what to do for certain simple INSTRUCTIONS – The CPU knows how to access a memory LOCATION by an ADDRESS – Many/most CPU operations involve one or more REGISTERS and/or one memory LOCATION 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 24
Instructions and Data CPU Instructions – Typical instructions would be: Load contents of memory location X into register Y Add contents of location Z to register Y Jump to location A Jump to location B if register M is zero Call location C – Call is a JUMP where the NEXT location is pushed to a stack A Stack is a Last In First Out (LIFO) list maintained by a separate register 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 25
Instructions and Data At the lowest level, there is no real difference between instructions and code. It becomes code when the CPU is asked to execute the content of a memory location as an instruction. In high level languages, this is usually separated outside the developers’ control (*). 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 26
Instructions and Data The instructions that the CPU understands are called MACHINE INSTRUCTIONS and are simply WORDS containing certain bit patterns. Many instructions include a memory reference portion, usually a relative offset to the location of the instruction itself In order to access a large amount of memory, memory addressing is INDIRECT, i. e. the memory reference portion references a location that contains a memory reference. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 27
Compilers and Interpreters In order to make it easier to write instructions for the CPU, mnemonic codes were assigned to the various MACHINE INSTRUCTIONS, and an ASSEMBLER would translate these mnemonic codes to MACHINE instructions. An ASSEMBLY instruction example (fictitious) JNZ -7 (Jump to address of this instruction -7 if register A is not Zero). This could be used to construct a loop. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 28
Compilers and Interpreters Writing complex applications using only ASSEMBLY instructions easily becomes a tedious and major undertaking (one of very few exceptions: http: //grc. com) Higher level languages translate more complex instructions into machine instructions – On-the-fly: Interpreters – Before you can execute: Compilers 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 29
Compilers and Interpreters VB 6 uses a hybrid solution: – In the IDE, it works in an Interpretative manner, but when producing an Exe, it compiles the code. The compiled code still relies very heavily on the (relatively large) runtime system. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 30
Variables and Assignments Higher level languages introduces named Variables. Each Variable (usually) represents one or more memory locations (words). The TYPE of variable determines how the memory locations are interpreted. In order to define where the data for a variable is stored, and what type it is, we need to DECLARE the variable. Dim x As String Variables are given values by ASSIGNMENT statements x = “Welcome to PPCC Introduction to VB 6” 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 31
Variables and Assignments Forget Equations – in VB, something like x = y – 6 is not one of two equations require to solve for x and y… it means the variable x will be assigned the value of variable y minus 6. The part to the right of the equal sign in an assignment statement is called an EXPRESSION. You can use the value of the variable that you are assigning to in the expression as well: x = x - 6 means 1/27/2004 x(after) = x(before) - 6 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 32
Object Orientation Basics An OBJECT is a logical component that consists of code as well as data. Typical for an object is that it ENCAPSULATES the data, meaning you can only access its data through properties and methods that it exposes. – This allows it to control how the data is presented, and what you can do to it. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 33
What is an object Consider a Word document. If you were to open a. doc file with Note. Pad, you would see a lot of gibberish. If you made changes to it, or even just saved it in Note. Pad, chances are you would have a problem opening it back up in Word! The “Document” is like an Object – Word is the code and the file (after it is read into Word) is the data. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 34
Visual Basic built-in “Objects” Forms and Controls are Objects Database access uses ADO Objects A Collection (a VB 6 Data Type) is an Object 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 35
Windows Environment Basics Windows is the operating system that controls the execution of programs and their access to resources on the system. Windows provides an Application Programming Interface (API), allowing most applications to achieve most things that Windows itself can do. Windows is built on passing MESSAGES between programs (or objects). The visual aspects of Windows (its windows, controls, etc. ) communicate primarily through messages that generate program EVENTS. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 36
Message-based and Event-Driven Applications typically react to mouse clicks, mouse movements, keyboard input, etc. Code in a VB application gets run when the EVENT HANDLER they belong to is executed as a result of an EVENT. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 37
The different VB’s (VB, VBA, VBScript) VB 6 is a many-headed “monster”. – – – 1/27/2004 There is is is VB VBA VBScript PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 38
The different VB’s (VB, VBA, VBScript) VB – This is what you develop in the VB IDE and compile into an executable through the Microsoft Visual Basic application. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 39
The different VB’s (VB, VBA, VBScript) There is VBA – This is the language supported by many/most Microsoft applications and many third party apps. – VBA code is developed inside another application, and runs in the context of this application. – VB and VBA are quite similar – in fact VB includes the VBA engine… 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 40
The different VB’s (VB, VBA, VBScript) There is VBScript – This is a simplified VB that is interpreted (no compiler available). – VBScript can be executed directly from Windows Explorer or from the command line. – VBScript can be used to develop Active Server Pages – web pages that can access server side resources. – VBScript provides a subset of VB statements and functions, and does not include forms, etc. – VBScript can not access API functions directly. 1/27/2004 PPCC - Introduction to VB 6 - Week 0 Copyright © 2004, Tore Bostrup 41
- Slides: 41