Transcript Document
Visual Basic 6 Programming. Lecture 1 : January 2005 Dr. Andrew Paul Myers 08/07/2015 1 Course Methods. 08/07/2015 Lectures. Handout notes. Supervised practical work. Weekly submission of exercises. Weekly feedback on exercises. Model Answers. Final project : 12.00 Wed. 19th May 2004. Clinics for final Project. 2 Introduction. A course in Visual Basic 6. Why Visual Basic 6 (VB6)? Objectives : • • • • General education in computing. Introduction to programming. First write a simple program, then… Write a more complex program. Course Benefits : • I.T. is a part of all our lives and getting more so! • VB6 and computing skills for other courses. • Future employment. 08/07/2015 3 Tools For The Job. Programming Environment : 08/07/2015 CFS client PC. Windows 2000. Microsoft Visual Studio 6 SP5. Computers in G73 only at present! 4 Programming Languages. You are users! How computers work. Machine code. High level languages. BASIC : Beginners All purpose Symbolic Instruction Code. Other languages : • FORTRAN, C/C++ & Java 08/07/2015 5 Programming Cycle. 08/07/2015 Analyse the task. Plan the program, a structured approach! Flowcharts & Pseudo-code (Week 2). Edit your source code. Execute and debug program (Week 2) Edit source code as necessary. 6 Setting up VB6 on CFS. VB6 is part of MS Visual Studio 6. Select : Start Menu Programs Installable Software Then navigate to : Central Software Programming Languages Double click on : Install MS Visual Studio V6 SP5 08/07/2015 7 VB6 Help : MSDN. Visual Studio 6 has an extensive, inbuilt help system call MSDN. MicroSoft Developer Network. Before this can be used select Setup Help for this session in the MS VS6 folder on the Programs menu. Only run this once per login session. 08/07/2015 8 Starting VB6. 08/07/2015 After setup a new folder is added to your programs menu. Select Microsoft Visual Basic 6 from the MS Visual Studio V6 SP5 folder. Tip : Create a short cut on your desktop. This will start the VB6 IDE. Integrated Programming Environment. Select Standard EXE as project type. 9 The VB IDE. 08/07/2015 Has Standard MicroSoft “feel”. Title Bar. Status : Design, run, debug. Menu Bar. Pull down menus. Tool Bar. Tool Box. For adding controls. Project Explorer. List project modules. Properties Window. Form Layout. 10 On we go… 08/07/2015 11 Structure of VB Program. VB programs are made up of different subroutines (or procedures) of the form: Private Sub <name>() Comment statement(s) Declaration statement(s) BASIC statement(s) End Sub 08/07/2015 12 VB Statements. Comments : Used to document programs and make them more readable. USE THEM !!! ‘ A Comment! Terminate a program. End 08/07/2015 13 Computers store and add numbers (very fast). Computer memory is divided into separate sections. Each is identified by a number (its address). Alan Bruce Claire Dan Ethel …….. A computer language allows us to identify these by names. Then to manipulate the numbers in them. Alan = Bruce + Claire An assignment statement. 08/07/2015 14 Bits, bytes and nibbles (its half a byte!). 8 bits make a byte 1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1 Each bit can be set to 1 or 0, so within one byte (8 bits) we can represent numbers in the range 0 to 255 i.e. 2^8. Combining multiple bytes, we can store numbers: 16 bits = 2 bytes stores 2^16 = 0 to 65535 32 bits = 4 bytes stores 2^32 = 0 to 4294967295 These are whole numbers (integers), no decimals allowed. We are restricted to numbers +/- 2 billion. 08/07/2015 15 Different Forms of Storage. A large number on a calculator is represented in exponential (also called scientific) format e.g. 1.23456E+23. If we take 4 bytes of memory, and store the mantissa (1.23456) in 3 bytes, and the exponent (+23) in one byte, we can store real numbers, with a greater range. Represent letters by a numerical code from 0 – 255 (ASCII) and store them in one byte. Text can then be stored as a linked set of bytes, called a String. 0 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 ASCII 107 is k 08/07/2015 ASCII 43 is + 16 Types of Variables. Integers (whole numbers) Real numbers. Single. Double (twice the number of bytes). Strings. And others coming later… Variants. All types automatically converting. DANGEROUS! – Don’t go there! 08/07/2015 17 Variable Names. Data is stored as variables, each with a different name. Variable names are: 08/07/2015 Case insensitive. Up to 255 characters long. First character must be a letter. Can use any combination of alphanumeric characters and the underscore character. 18 Examples. Value 3radius Question&Reply Density_of_water lngRadius1 08/07/2015 yes no no yes yes 19 Hungarian Notation. A convention of prefixes for variable names, depending on variable type. String : str Long Integer : lng Double : dbl Currency : cur 08/07/2015 Integer : int Single : sng Boolean : bln Variant : vnt 20 Variable Types I VB6 has 14 standard types. String : Hold characters, approx 231, can be null (empty). Can be identified by $. strText$=“Hello.” strEmptyText = “” 08/07/2015 21 Variable Types II. 08/07/2015 Integer : Range –32,768 to +32,767. Can be identified by %. intValue% = 1 intValue2 = 1000 Long Integer : Larger range (231). lngValue1& = 56000 lngValue2 = -56000 22 Variable Types III. Single Precision : Real numbers. Accuracy of 7 digits. sngReal_Number1! = 1.23 sngReal_Number2 = -0.345 Double Precision : Accuracy of 16 digits dblValue1# = 0.435827348593 dblValue2 = 23.4782947373 Reals are slower than integers in calculations! 08/07/2015 23 Variable Types IV. Boolean : True or False? blnAnswer = False 08/07/2015 Other types include : Currency, Date, Byte (range 0 to 255) and variant (can be any data type – very flexible, but dangerous!). 24 Variable Type Declarations I Declare all variables before use! Good programming practice. Also avoids having to use the suffixes $, #, ! etc. Use the Dim statement. e.g. Dim <variable> as <data type> Dim intDays as Integer Dim strText as String, dblAns as Double 08/07/2015 25 Variable Type Declarations II Declaring all variables before use is good programming practice. Use the “Option Explicit” command to make VB insist of variables being declared before you can use them. Option Explicit This also avoids having to use the suffixes $, #, ! etc, as all variable types are explicitly defined. Note : This check is carried out when the program is run, not at the editing stage! 08/07/2015 26 Assignments I. 08/07/2015 5 basic mathematical operators: Addition (+) Subtraction (-) Division(/) Multiplication (*) Exponentiation (^) Precedence follows BODMAS. Parentheses can be used to over ride precedence. 6*5+4*3 gives 42 (((6 * 5) + 4) * 3) gives 102 27 Assignments II. <variable> = <value> | <variable> | <expression> Examples : intValue = 1 strText2 = strText1 intValue = intValue + 1 sngVol = sngLength^3.0 strText = “Hello” dblVol= (4#/3#)*dblPi*dblRadius^3 08/07/2015 28 Print Statements. Print <expression> Print Print Print Print “Hello!” “Value is “; intValue “A : “; intA; “ cm.” dblRadius We shall be using “Text Boxes”! 08/07/2015 29 Good Programming Style. 08/07/2015 Comment your program! Hungarian notation for variable names. Use descriptive variable names. Blanks lines may be used to improve readability. Indent code with “tabs”. 30 References. Visual BASIC 6 from the ground up. Gary Cornell. (Osborne). MSDN On-line Help. Active subsection : Visual Basic Documentation. 08/07/2015 31