17
www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies. 1 VB PROGRAMMERS GUIDE LESSON 1 File: VbGuideL1.doc Date Started: May 24, 2002 Last Update: Dec 27, 2002 ISBN: 0-9730824-9-6 Version: 0.0 INTRODUCTION TO VB PROGRAMMING VB stands for Visual Basic. Visual Basic is a programming language that lets you program window GUI programs very fast. GUI stands for Graphic U ser I nterface. All windows programs are GUI's. These guides will teach you VB programming with step by step instructions. If you do all the exercises in these lessons you will become a very good VB programmer. In each lesson we use bold to introduce new programming concepts, underline for important programming concepts, violet for program definitions and blue for programming code examples. VB Data Types and Variables A program lists instructions as words to instruct a computer to do something. The words form a programming statement, just like we use words in a sentence. Which words are used defines the programming language. Each programming language as their own set of words. The words chosen for Visual Basic make it easy for some one to learn programming. All programs need data to represent values. Programming languages use numeric data like 10.5 or string data like "hello". These different types of data are known as data types. Here is a chart of all data types used in Visual Basic. Data Type Sample value Range of values String "hello there" Alphanumeric characters, digits and other characters Integer -5 Whole numbers ranging from -32,768 to 32,767 Long 18459038 Large whole numbers having range:-2147483648 to 2147483647 Boolean True True or False Byte 10 small numbers range 0 to 255 Single 5.678 Single-precision floating point numbers with six digits of accuracy. Double -2.4585E30 Double-precision floating point numbers with 14 digits of accuracy Currency 10.50 Decimal fractions, such as dollars and cents. Date #5/23/2002# month, day, year, various date formats available Object frm as Form Objects represent a group of many values Variant v as variant A variant can represent any data type. The variable's data type will depend on the value that was last assigned to it. Decimal 1.123453 Double-precision floating point number with 14 digits of accuracy. Data is stored in computer memory. The location and value where the data is stored is represented by a variable. A variable gets an identification name like: x. The identification name must start with a letter. Additional letters or digits may follow the first Letter, like: x1. VB Program variables programming statements

INTRODUCTION TO VB PROGRAMMING VB

  • Upload
    others

  • View
    20

  • Download
    0

Embed Size (px)

Citation preview

Page 1: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

1

VB PROGRAMMERS GUIDE LESSON 1

File: VbGuideL1.docDate Started: May 24, 2002Last Update: Dec 27, 2002ISBN: 0-9730824-9-6Version: 0.0

INTRODUCTION TO VB PROGRAMMING

VB stands for Visual Basic. Visual Basic is a programming language that lets you program windowGUI programs very fast. GUI stands for Graphic User Interface. All windows programs are GUI's.These guides will teach you VB programming with step by step instructions. If you do all theexercises in these lessons you will become a very good VB programmer. In each lesson we use boldto introduce new programming concepts, underline for important programming concepts, violet forprogram definitions and blue for programming code examples.

VB Data Types and Variables

A program lists instructions as words to instruct a computer to do something. The words form aprogramming statement, just like we use words in a sentence. Which words are used defines theprogramming language. Each programming language as their own set of words. The wordschosen for Visual Basic make it easy for some one to learn programming. All programs need data torepresent values. Programming languages use numeric data like 10.5 or string data like "hello".These different types of data are known as data types. Here is a chart of all data types used inVisual Basic.

Data Type Sample value Range of valuesString "hello there" Alphanumeric characters, digits and other charactersInteger -5 Whole numbers ranging from -32,768 to 32,767Long 18459038 Large whole numbers having range:-2147483648 to 2147483647Boolean True True or FalseByte 10 small numbers range 0 to 255Single 5.678 Single-precision floating point numbers with six digits of accuracy.Double -2.4585E30 Double-precision floating point numbers with 14 digits of accuracyCurrency 10.50 Decimal fractions, such as dollars and cents.Date #5/23/2002# month, day, year, various date formats availableObject frm as Form Objects represent a group of many values

Variant v as variantA variant can represent any data type. The variable's data type willdepend on the value that was last assigned to it.

Decimal 1.123453 Double-precision floating point number with 14 digits of accuracy.

Data is stored in computer memory. The location and value where the data is stored is representedby a variable. A variable gets an identification name like: x. The identification name must start witha letter. Additional letters or digits may follow the first Letter, like: x1.

VB Program

variables

programming statements

Page 2: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

2

Declaring Variables

Before you can use a variable it has to be declared. To declare a variable you use the Dim keywordand specify the data type.

Dim x as Integer Code (blue)

Dim identifier As datatype Definition (violet)

In the above example x is declared as a variable of Integer data type. Keywords are used inprogramming languages as commands. Dim means declare variable, as means specify data type.

Using Variables

Think of a variable as a box that holds a value represented by an identifier. If you know the name ofthe identifier then you can get its value. The value is stored in computer memory represented by thevariable.

x 5

After you declare a variable, you can give it a value. When you give a value to a variable it is knownas assigning or initializing. Here is an example declaring an Integer variable x and initializing toan integer value.

Dim x As Integer

x = 5 x 5

Here is an example of declaring a string variable and initializing it with a string value:

Dim name As String

name = "Ed" name "Ed"

Later in your program you can assign different values to your variables:

name = "Tom" name "Tom"

In this situation the old value is replaced with the new value. The old value disappears, gone forever

You can even assign a variable to another variable:

Dim x As IntegerDim y As Integer

x = 10 x 10 y = x y 10

In this situation the value of variable x is assigned to variable y. Remember, it is the valuerepresented by the variable that is assigned not the name of the variable. The value is obtainedfrom the name. As you use your variables the values will be always changing.

Page 3: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

3

VB Sample Program

We are now ready to write our first VB program. In this sample program we will initialize somevariables and print their values to the screen using the debug.print statement. Programminglanguages use programming statements to do things. A statement includes a command word calleda keyword and values. The debug.print statement allows you to print out messages on thecomputer screen. Messages are string constants enclosed by " double quotes ". Here we use theDebug.Print statement to print "Hi, my name is Tom" message on the computer screen.

Debug.Print "Hi, my name is Tom"

You can also use a variable to print out values to the screen.

Debug.Print name

Here is our example program:

Before you can write and run this program you first need to create a new Project. Make sure youhave Visual Basic installed on your computer. We are using Visual Basic version 6.0. Open up VisualBasic, the following window pops up, select Standard EXE then click on the Open button.

programming statement

keywords string message

Dim name as StringDim age as Integername = "Tom"age = 20Debug.Print "Hi, my name is "Debug.Print nameDebug.Print "I am now "Debug.Print ageDebug.Print "years old."

Page 4: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

4

The VB IDE Integreated Development Environment now appears as shown below. You write yourVB program using the IDE. Just follow these step by step Instructions:

[1] Right-click on Form1 in the Project Explorer window and select Remove Form1 from the pop upwindow. (this is an optional step)

[2] From the Project menu click Add Module.

[3] Click on the Open button to create Module1, then double-click the Module1 icon on the ProjectExplorer window to open it.

[4] Enter the following program in the Module1 window as shown below. Notice, the programstarts with Sub Main() and ends with End Sub

Sub Main()

Dim name as StringDim age as Integername = "Tom"age = 20Debug.Print "Hi, my name is "Debug.Print nameDebug.Print "I am now "Debug.Print ageDebug.Print "years old."

End Sub

Page 5: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

5

[5] Click on the View menu and select Immediate Window.

[6] Click the Run menu and then click Start (or press F5 to run the program). The output of theprogram is shown in the Immediate window.

Page 6: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

6

Understanding the example program

Lets go through each line of the example program one by one for understanding. We will put acomment on each line. Comments are used to explain program code, the comments are neverexecuted. Comments start with a ' single quotation.

' This is a Comment

Our comments will be in color green to be easily recognized. Comments may start at the beginningof a line or at the end of a programming statement.

age = 20 ' assign the number 20 to the variable age

Programs are made up of programming statements. Programming statements are groupedtogether in a Sub. Subs are put into modules. When the VB program starts to run it executes thestatements one by one contained in the Sub. When the last statement is executed the programends. All Subs need a name for identification. The name of our Sub is Main. The Main is the firstsub to be executed when the program runs. A VB program can only have 1 main sub. The roundbrackets () following Main means Main is a sub. Round brackets () distinguish a sub name from avariable name. Names of subs and variables are known as identifiers.

Sub main() ' contains program

Dim name as String ' declare a string variable to represent a persons nameDim age as Integer ' declare an integer variable to represent a persons agename = "Tom" ' assign the string "Tom" to the variable nameage = 20 ' assign the number 20 to the variable ageDebug.Print "Hi, my name is " ' print a message to the screenDebug.Print name ' print to the screen the persons nameDebug.Print "I am now " ' print a message to the screenDebug.Print age ' print to the screen the persons ageDebug.Print "years old." ' print a message to the screen

End Sub ' ends program

The program output:

Page 7: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

7

RENAMING PROJECTS AND MODULES

Every time you make a project it gets a default name Project 1. Every time you add a module, itgets a default name like Module 1. You should give projects and modules meaningful names. It'seasy to do. Just go to the project window click on the module and in the Module properties boxchange the name.

To change a project name right click on theproject, select project properties from thepop up menu and the following dialog boxappears. Dialog boxes allow you to enterinformation. In VB programming you will beusing dialog boxes all the time. You will alsobe writing your own dialog boxes. Dialogboxes are also called forms.

set startup to Main

projectname

projecttype

Page 8: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

8

Saving your projects and modules

You need to save all the modules of your project separately from the file menu. From the file menuselect Save Project As and then Save Module As. You only need to use "Save As" once. Nexttime just use Save Project and save Module. Be careful, save project only saves the projects but notthe modules. You always need to save everything.

VB LESSON1 EXERCISE 1

Write a program that has 3 variables to display a person's name, address and phone number.Declare and assign values to these variables. Print out to the screen using Debug.Print. Call your VBproject Project1 and call your module Module1

VARIANTS

You may need a variable to represent any kind of data type. In this situation you may declare thedata type as Variant. This means that the data type of the variable will depend on the valueassigned to it.

Dim s As Variants = "Hello"

The String constant "Hello" is assigned to the variable s. Later in the program, you may assign anumber to s.

s = 10

Variants may be confusing to some but to others it may be more convenient. Variants are also thedefault data type.

Dim s

s is now a variant. In VB, you can declare variables on the same line separated by commas.

Dim x, y As Integer

You must be very careful here! x is a variant where y is an Integer. To make both Integer you needto write the statement as

Dim x As Integer, y As Integer

CONSTANTS

There are situations in a program where you need to assign a value to variable but you never everwant that variable to change. These variables are called constants and are also known as "readonly". This means you can read the value again but you can never write a new value to it. You usethe keyword const to specify that a variable will be a constant and initialize it with a value.

Const Max = 10

Page 9: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

9

Once you initialize the constant variable with a value it can never change. The constant is onlyavailable for use in your module. To let other modules use your constant you proceed the constantdeclaration with the Public keyword.

Public Const Max = 10

Once you declare and initialize your constant you use it just like any variable.

Dim x As Integer

x = Max

Now x has the value 10. Remember you can never assign new values to your constant variable.

Max = 20

EXPLICIT OPTION

It is not always necessary to use the Dim statement to declare variables. You can use variables rightaway.

x = 10

It is advised that you declare variables before you use them. To make sure you declare everyvariable before you use it, you put the

Option Explicit

directive at the top of your program. Here's the last program example using Option Explicit

Option Explicit

Sub main()

Dim name as StringDim age as Integername = "Tom"age = 20Debug.Print "Hi, my name is "Debug.Print nameDebug.Print "I am now "Debug.Print ageDebug.Print "years old."

End Sub

VB LESSON1 EXERCISE 2

Change your Exercise 1 to use Variants, Constants and the Explicit Option. Call your moduleModule2. You can put a second module in your project and in the Project Properties you can set thestartup to this module. You will have to rename Main to Main1 in Module 1. If you run your programmany times you need to clear the Immediate window. It retains all previous results. Just highlightall the entries and press the delete button, or right click on it using the mouse and select cut fromthe pop up menu.

Page 10: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

10

INPUT BOX

Your program will need to get data values from the keyboard. A good way to get data from thekeyboard is to use an Input Box.

These look very professionals and impressive. Each Input Box gets a prompt message to tell theuser what to do. Once they enter the data they can click on the OK button or Cancel button.

InputBox is a built in VB function. Built in functions are pre-defined code that you can use rightaway. Functions receive values and return values. Using built in functions let you program very fast.You need to know how to use built in functions. Every built in function has a syntax that specifieshow to use it. The syntax for the Input Box function is a little overwhelming.

InputBox (prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])

Every function gets arguments. arguments are used to pass values to the function. The InputBoxfunction needs the prompt message. The other arguments enclosed in square brackets [ ] areoptional meaning you do not need to supply values for them. Here are the arguments and theirdescriptions. Don't be too concerned right now about all the arguments right now.

Argument Description

prompt

Required. String expression displayed as the message in the dialog box. Themaximum length of prompt is approximately 1024 characters, depending onthe width of the characters used. If prompt consists of more than one line, youcan separate the lines using a carriage return character (Chr(13)), a linefeedcharacter (Chr(10)), or carriage return–linefeed character combination(Chr(13) & Chr(10)) between each line.

title Optional. String expression displayed in the title bar of the dialog box. If youomit title, the application name is placed in the title bar.

Page 11: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

11

default Optional. String expression displayed in the text box as the default response ifno other input is provided. If you omit default, the text box is displayed empty.

xposOptional. Numeric expression that specifies, in twips, the horizontal distance ofthe left edge of the dialog box from the left edge of the screen. If xpos isomitted, the dialog box is horizontally centered.

ypos

Optional. Numeric expression that specifies, in twips, the vertical distance of theupper edge of the dialog box from the top of the screen. If ypos is omitted, thedialog box is vertically positioned approximately one-third of the way down thescreen.

helpfileOptional. String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box. If helpfile is provided, context must also beprovided.

contextOptional. Numeric expression that is the Help context number assigned to theappropriate Help topic by the Help author. If context is provided, helpfile mustalso be provided.

The following string constants can be used anywhere in your code in place of actual values:Constant Value Description

vbCr Chr(13) Carriage return

vbCrLf Chr(13) &Chr(10)

Carriage return–linefeed combination

vbFormFeed Chr(12) Form feed; not useful in Microsoft WindowsvbLf Chr(10) Line feed

vbNewLineChr(13) &Chr(10) orChr(10)

Platform-specific newline character; whatever is appropriate forthe platform

vbNullChar Chr(0) Character having the value 0

vbNullString String havingvalue 0

Not the same as a zero-length string (""); used for callingexternal procedures

vbTab Chr(9) Horizontal tabvbVerticalTab Chr(11) Vertical tab; not useful in Microsoft Windows

Functions receive values do a calculation and return the calculated value. It's easy to use theInputBox function, all you have to do is assign the function to a variable. The variable gets the valuethat the user has entered into the input box

Dim name As String

name = InputBox("What is your name?")

Here's a sample program that asks someone for their name and then prints their name to thescreen.

Sub main()

Dim name As Stringname = InputBox("What is your name?")Debug.Print "Your name is " & name

End Sub

Page 12: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

12

VB LESSON1 EXERCISE 3

Change your exercise 2 to use an Input Box. Ask someone for their name, address and age. Callyour module Module3. Rename the Main sub in Exercise 2 to Main2

MSG BOX

A message box lets you display information in a window. The message box is displayed until the OKbutton is pressed.

A Message Box function has the following syntax:

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

Again the optional supplied argument values are in square brackets[ ]. We just use prompt andtitle. Here are the descriptions of the named arguments:

Part Description

prompt

Required. String expression displayed as the message in the dialog box. The maximum lengthof prompt is approximately 1024 characters, depending on the width of the charactersused. If prompt consists of more than one line, you can separate the lines using acarriage return character (Chr(13)), a linefeed character (Chr(10)), or carriage return –linefeed character combination (Chr(13) & Chr(10)) between each line.

buttonsOptional. Numeric expression that is the sum of values specifying the number and type ofbuttons to display, the icon style to use, the identity of the default button, and themodality of the message box. If omitted, the default value for buttons is 0.

title Optional. String expression displayed in the title bar of the dialog box. If you omit title,the application name is placed in the title bar.

helpfile Optional. String expression that identifies the Help file to use to provide context-sensitiveHelp for the dialog box. If helpfile is provided, context must also be provided.

context Optional. Numeric expression that is the Help context number assigned to the appropriateHelp topic by the Help author. If context is provided, helpfile must also be provided.

Here's the program output:Here's the input box

Page 13: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

13

Settings

The buttons argument settings are:Constant Value DescriptionvbOKOnly 0 Display OK button only.vbOKCancel 1 Display OK and Cancel buttons.vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.vbYesNoCancel 3 Display Yes, No, and Cancel buttons.vbYesNo 4 Display Yes and No buttons.vbRetryCancel 5 Display Retry and Cancel buttons.vbCritical 16 Display Critical Message icon.vbQuestion 32 Display Warning Query icon.vbExclamation 48 Display Warning Message icon.vbInformation 64 Display Information Message icon.vbDefaultButton1 0 First button is default.vbDefaultButton2 256 Second button is default.vbDefaultButton3 512 Third button is default.vbDefaultButton4 768 Fourth button is default.

vbApplicationModal 0Application modal; the user must respond to themessage box before continuing work in thecurrent application.

vbSystemModal 4096 System modal; all applications are suspendeduntil the user responds to the message box.

vbMsgBoxHelpButton 16384 Adds Help button to the message boxVbMsgBoxSetForeground

65536 Specifies the message box window as theforeground window

vbMsgBoxRight 524288 Text is right aligned

vbMsgBoxRtlReading 1048576 Specifies text should appear as right-to-leftreading on Hebrew and Arabic systems

The first group of values (0–5) describes the number and type of buttons displayed in the dialogbox; the second group (16, 32, 48, 64) describes the icon style; the third group (0, 256, 512)determines which button is the default; and the fourth group (0, 4096) determines the modality ofthe message box. When adding numbers to create a final value for the buttons argument, use onlyone number from each group. Note these constants are specified by Visual Basic for Applications. Asa result, the names can be used anywhere in your code in place of the actual values.

Return Values

Constant Value DescriptionvbOK 1 OKvbCancel 2 CancelvbAbort 3 AbortvbRetry 4 RetryvbIgnore 5 IgnorevbYes 6 YesvbNo 7 No

Page 14: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

14

Remarks

When both helpfile and context are provided, the user can press F1 to view the Help topiccorresponding to the context. Some host applications, for example, Microsoft Excel, alsoautomatically add a Help button to the dialog box. If the dialog box displays a Cancel button,pressing the ESC key has the same effect as clicking Cancel. If the dialog box contains a Helpbutton, context-sensitive Help is provided for the dialog box. However, no value is returned until oneof the other buttons is clicked. Note To specify more than the first named argument, you must useMsgBox in an expression. To omit some positional arguments, you must include the correspondingcomma delimiter

Here's our example program using a MsgBox

Sub Main()

Dim name As Stringname = InputBox("What is your name?")MsgBox "Your name is " & name

End Sub

Notice we use the '&' operator to join a message and a variable value. Operators are used to dooperations with variables. The '&' can also be used to join strings together but not two numericvalues.

VB LESSON1 EXERCISE 4

Change your exercise 1 to use include a MsgBox. Ask the user to enter their name, address and ageusing the Input Box and then display them in a MsgBox. Use a line feed character Chr(10) or Vbcrlfto separate lines in the MsgBox. Call your module Module4

ARRAYS

Arrays represent many variables of the same data type all accessed using a common name.Each variable of the array is known as a cell or element.

x x0 x1 x2 x3 x4

You may want to use an array to record temperature of every hour for a certain day.

temps 45 89 56 34 23

It's easy to make an array you just declare a variable stating a lower bound and an upper bound.

Dim a(0 to 4) as Integer

Dim ( lowerbound to upperbound )

The lowerbound starts at 0 and can be omitted.

Dim a(4)

Page 15: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

15

Once you declare your array you can store values in it, this is known as accessing. You access thearray cell elements by an index. Each cell element in the array is located using an index.

a(2) = 5

Our declared array has 5 elements with indexes from 0 to 4. The value 5 will be located at index 2

0 1 2 3 45

You can get the value from an array by specifying the array element index and assign the value athe specified index to another variable,

Dim x as Integerx = a(2)

Now variable x has the value 5. Arrays of 1 row are known as 1 dimensional arrays.

VB LESSON1 EXERCISE 5

Declare a 1 dimensional array of 5 elements to store string values. Get 5 different names using anInput Box and store in the array. Using another Input Box ask the user to type in a number between1 to 5. Display the name corresponding to the number in a MsgBox. Call your module Module5.Warning to not declare an array as name(4) .

Some people like their arrays to start at 1 rather than 0. Use the following statement at the top ofyour program to make all arrays to start at lower bound 1.

Option base 1

2 dimensional arrays

An array may have many rows and columns these arrays are known as 2 dimensional arrays,there are now two dimension rows and columns. To declare a two dimensional array you mustdeclare the number of rows and columns.

dim b ( 2, 3) As Integer

array name index value

rows columns

columns

rows

Page 16: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

16

You assign values to the array cells by specifying row and column indexes

columns0 1 2

rows 01 5

VB LESSON1 EXERCISE 6

Declare a 2 dimensional array of 4 rows and two columns. Using an Input Box ask the user for 4names. Store each name in the first column of each row. For each name ask the user their favoritehobby, using an InputBox. Store the hobby in the second column of each row. Ask the user to typein a number between 1 and 4. using an Input Box. Display the person's name and hobby in aMessage Box. Call your module Module6

b(1, 2) = 5

row column

Page 17: INTRODUCTION TO VB PROGRAMMING VB

www.cscourses.com E-Learning, E-Books, Computer Programming Lessons and Tutoring copyright © www.cscourses.com For use of student or purchaser only. Do not make copies.

17

IMPORTANT

You should use all the material in all the lessons to do the questions and exercises. If you do not know how to do something orhave to use additional books or references to do the questions or exercises, please let us know immediately. We want to haveall the required information in our lessons. By letting us know we can add the required information to the lessons. The lessonsare updated on a daily bases. We call our lessons the "living lessons". Please let us keep our lessons alive.

E-Mail all typos, unclear test, and additional information required to:

[email protected]

E-Mail all attached files of your completed exercises to:

[email protected]

Order your next lesson from:

http://www.cscourses.com/vb.htm

This lesson is copyright (C) 2002 by The Computer Science Tutoring Center "www.cstutoring.com"This document is not to be copied or reproduced in any form. For use of student only