0% found this document useful (0 votes)
18 views

VB Introduction 1

Visual basic

Uploaded by

gasparshemagemb
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

VB Introduction 1

Visual basic

Uploaded by

gasparshemagemb
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

1

Introduction to Visual Basic


Programming
Outline
1 Introduction
2 Simple Program: Printing a Line of Text
3 Another Simple Program: Adding Integers
4 Memory Concepts
5 Arithmetic
6 Decision Making: Equality and Relational
Operators
7 Using a Dialog to Display a Message

 2002 Prentice Hall. All rights reserved.


2

1 Introduction

• In this chapter we introduce


– Visual Basic programming
• We present examples that illustrate several important features
of the language
– Console applications
• Applications that contain only text output
• Output is displayed in a command window

 2002 Prentice Hall. All rights reserved.


3
2 Simple Program: Printing a Line of
Text
• Simple program that displays a line of text
• When the program is run
– output appears in a command window
• It illustrates important Visual Basic features
– Comments
– Modules
– Sub procedures

 2002 Prentice Hall. All rights reserved.


4
Visual Basic console
1
2
' Fig. 3.1: Welcome1.vb
' Simple Visual Basic program. applications consist of pieces
Outline
3
4 Module modFirstWelcome
called modules
5 Single-quote character (') Welcome1.vb
6 Sub Main()
7 Console.WriteLine("Welcome to indicates
Visual that the remainder
Basic!")
8 End Sub ' Main of the line is a comment
9
10 End Module ' modFirstWelcomeThe Main procedure is the entry
The
point of the program. It is present
Console.WriteLin
in all console applications
Welcome to Visual Basic! e statement displays Program Output
text output to the
console

•A few Good Programming Practices


– Comments
• Every program should begin with one or more comments
– Modules
• Begin each module with mod to make modules easier to identify

– Procedures
• Indent the entire body of each procedure definition one “level” of
indentation
 2002 Prentice Hall.
All rights reserved.
5
2 Simple Program: Printing a Line of
Text
• Now a short step-by-step explanation of how to
create and run this program using the features of
Visual Studio .NET IDE…

 2002 Prentice Hall. All rights reserved.


6
2 Simple Program: Printing a Line of
Text
1. Create the console application
– Select File > New > Project…
– In the left pane, select Visual Basic Projects
– In the right pane, select Console Application
– Name the project Welcome1
– Specify the desired location
2. Change the name of the program file
– Click Module1.vb in the Solution Explorer
window
– In the Properties window, change the File Name
property to Welcome1.vb

 2002 Prentice Hall. All rights reserved.


7
2 Simple Program: Printing a Line of
Text
Left pane Right pane

Project
name

File
location

Fig. 2 Creating a Console Application with the New Project dialog.

 2002 Prentice Hall. All rights reserved.


8
2 Simple Program: Printing a Line of
Editor window
Text
(containing program
code)

Fig. 3.3 IDE with an open console application.

 2002 Prentice Hall. All rights reserved.


9
2 Simple Program: Printing a Line of
Text

Solution Explorer

Click Module1.vb
to display its
properties

Properties window

File
Name
property

Fig. 4 Renaming the program file in the Properties window.

 2002 Prentice Hall. All rights reserved.


10
2 Simple Program: Printing a Line of
Text
3. Change the name of the module
– Module names must be modified in the editor window
– Replace the identifier Module1 with
modFirstWelcome
4. Writing code
– Type the code contained in line 7 of Fig. 3.1 between Sub
Main() and End Sub
• Note that after typing the class name and the dot operator the
IntelliSense is displayed. It lists a class’s members.
• Note that when typing the text between the parenthesis
(parameter), the Parameter Info and Parameter List windows
are displayed

 2002 Prentice Hall. All rights reserved.


11
2 Simple Program: Printing a Line of
Text
5. Run the program
– To compile, select Build > Build Solution
• This creates a new file, named Welcome1.exe
– To run, select Debug > Start Without
Debugging

 2002 Prentice Hall. All rights reserved.


12
2 Simple Program: Printing a Line of
Text

Partially-typed member Member list

Description of
highlighted member

Fig. 5 IntelliSense feature of the Visual Studio .NET IDE.

 2002 Prentice Hall. All rights reserved.


13
2 Simple Program: Printing a Line of
Text

Up arrow Down arrow

Parameter List window

Parameter Info window

Fig. 6 Parameter Info and Parameter List windows.

 2002 Prentice Hall. All rights reserved.


14
2 Simple Program: Printing a Line of
Text

Command window prompts the user to


press a key after the program terminates

Fig. 7 Executing the program shown in Fig. 3.1.

 2002 Prentice Hall. All rights reserved.


15
2 Simple Program: Printing a Line of
Text
Omitted parenthesis
character (syntax error)

Blue underline
indicates a
syntax error

Task List window

Error description(s)

Fig. 8 IDE indicating a syntax error.

 2002 Prentice Hall. All rights reserved.


16
1 ' Fig. 3.9: Welcome2.vb Outline
2 Method Write
' Writing line of text with multiple does not
statements. position the output
3
4 Module modSecondWelcome
cursor at the beginning of the next line
5 Welcome2.vb
6 Sub Main()
7 Console.Write("Welcome to ")
8 Console.WriteLine("Visual Basic!")
9 End Sub ' Main
11 Method WriteLine positions the output
12 End Module ' modSecondWelcome
cursor at the beginning of the next line

Welcome to Visual Basic!


Program Output

 2002 Prentice Hall.


All rights reserved.
17
3 Another Simple Program: Adding
Integers
• User input two integers
– Whole numbers
• Program computes the sum
• Display result

 2002 Prentice Hall. All rights reserved.


18
1 ' Fig. 3.10: Addition.vb Outline
2 ' Addition program.
3 These variables store strings of characters
4 Module modAddition
5 Addition.vb
6 Sub Main() Declarations begin with keyword Dim
7
8 ' variables for storing user input
9 Dim firstNumber, secondNumber As String
10
11 ' variables used in addition calculation
First value entered by user is assigned
12 Dim number1, number2, sumOfNumbers As Integer
13 to variable firstNumber
14 ' read first number from user
15 Console.Write("Please enter the first integer: ")
16 firstNumber = Console.ReadLine()
17 These variables store integers values
18 ' read second number from user
Method ReadLine causes program
19 Console.Write("Please enter the second integer:to pause")and wait for user input
20 secondNumber = Console.ReadLine()
21
22 ' convert input values to Integers
23 number1 = firstNumber
24 number2 = secondNumber Implicit conversion from String to
25 Integer
26 sumOfNumbers = number1 + number2 ' add numbers
27
28 ' display results
29 Sums integers and assigns result to
Console.WriteLine("The sum is {0}", sumOfNumbers)
30 variable sumOfNumbers
31 End Sub ' Main
32
33 Format indicates that the argument
End Module ' modAddition
after the string will be evaluated and
 2002 Prentice Hall.
incorporated into the string All rights reserved.
19
Please enter the first integer: 45 Outline
Please enter the second integer: 72
The sum is 117
Addition.vb

 2002 Prentice Hall.


All rights reserved.
20
3 Another Simple Program: Adding
Integers

If the user types a non-integer value, such


as “hello,” a run-time error occurs

Fig. 11 Dialog displaying a run-time error.

 2002 Prentice Hall. All rights reserved.


21

4 Memory Concepts

• Variables
– correspond to actual locations in the computer’s memory
– Every variable has a
• Name
• Type
• Size
• value
– A value placed in a memory location replaces the value
previously stored
• The previous value is destroyed
– When value is read from a memory location, it is not
destroyed

 2002 Prentice Hall. All rights reserved.


22

4 Memory Concepts

number1 45

Fig. 12 Memory location showing name and value of variable number1.

number1 45

number2 45

Fig. 13 Memory locations after values for variables number1 and number2 have been input.

 2002 Prentice Hall. All rights reserved.


23

5 Arithmetic

• Arithmetic operators
– Visual Basic use various special symbols not used in algebra
• Asterisk (*), keyword Mod
– Binary operators
• Operates using two operands
– sum + value
– Unary operators
• Operators that take only one operand
– +9, -19

 2002 Prentice Hall. All rights reserved.


24

5 Arithmetic

45
number1
45
number2

sumOfNumbers 45

Fig. 14 Memory locations after an addition operation.

 2002 Prentice Hall. All rights reserved.


25

5 Arithmetic

• Integer division
– Uses the backslash, \
– 7 \ 4 evaluates to 1
• Floating-point division
– Uses the forward slash, /
– 7 / 4 evaluates to 1.75
• Modulus operator, Mod
– Yields the remainder after Integer division
– 7 Mod 4 yields 3

 2002 Prentice Hall. All rights reserved.


26

5 Arithmetic

Visual Basic operation Arithmetic operator Algebraic expression Visual Basic


expression
Addition + f+7 f + 7
Subtraction – p–c p - c
Multiplication * bm b * m
Division (float) / x / y or <Anchor10> or x / y

x y
Division (Integer) \ none v \ u
Modulus % r mod s r Mod s
qp
q
Exponentiation ^ ^p
Unary Negative - –e –e
Unary Positive + +g +g
Fig. 3.14 Arithmetic operators.

Fig. 14 Arithmetic Operators.

 2002 Prentice Hall. All rights reserved.


27

5 Arithmetic

• Rules of operator precedence


1. Operators in expressions contained within parentheses
2. Exponentiation
3. Unary positive and negative
4. Multiplication and floating-point division
5. Integer division
6. Modulus operations
7. Addition and subtraction operations

 2002 Prentice Hall. All rights reserved.


28

5 Arithmetic

Operator(s) Operation Order of evaluation (prec edenc e)


() Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same
level” (i.e., not nested), they are evaluated from left
to right.
^ Exponentiation Evaluated second. If there are several such operators,
they are evaluated from left to right.
+, – Sign operations Evaluated third. If there are several such operators,
they are evaluated from left to right.
*, / Multiplication and Evaluated fourth. If there are several such operators,
Division they are evaluated from left to right.
\ Integer Evaluated fifth. If there are several such operators,
division they are evaluated from left to right.

Mod Modulus Evaluated sixth. If there are several such operators,


they are evaluated from left to right.
+, – Addition and Evaluated last. If there are several such operators,
Subtraction they are evaluated from left to right.
Fig. 3.15 Precedence of arithmetic operators.

Fig. 15 Precedence of arithmetic operators.

 2002 Prentice Hall. All rights reserved.


29

5 Arithmetic

Step 1. y = 2 * 5 * 5 + 3 * 5 + 7
2 * 5 is 10 (Leftmost multiplication)

Step 2. y = 10 * 5 + 3 * 5 + 7
10 * 5 is 50 (Leftmost multiplication)

Step 3. y = 50 + 3 * 5 + 7
3 * 5 is 15 (Multiplication before addition)

Step 4. y = 50 + 15 + 7
50 + 15 is 65 (Leftmost addition)

Step 5. y = 65 + 7
65 + 7 is 72 (Last addition)

Step 6. y = 72 (Last operation—place 72 into y)

Fig. 16 Order in which a second-degree polynomial is evaluated.

 2002 Prentice Hall. All rights reserved.


30
6 Decision Making: Equality and
Relational Operators
• If/Then structure
– Allows a program to make decision based on the truth or
falsity of some expression
– Condition
• The expression in an If/Then structure
– If the condition is true, the statement in the body of the
structure executes
– Conditions can be formed by using
• Equality operators
• Relational operators

 2002 Prentice Hall. All rights reserved.


31
6 Decision Making: Equality and
Relational Operators
Standard algebraic Visual Basic Example Meaning of
equality operator or equality of Visual Basic Visual Basic c ondition
relational operator or relational condition
operator
Equality
operators
 = x = y x is equal to y
 <> x <> y x is not equal to y

Relational
operators
> > x > y x is greater than y
< < x < y x is less than y
>= x >= y x is greater than or equal to y
? <= x <= y x is less than or equal to y
Fig. 3.17 Equality and relational operators.

Fig. 17 Equality and relational operators.

 2002 Prentice Hall. All rights reserved.


32
1 ' Fig. 3.19: Comparison.vb Outline
2 ' Using equality and relational operators.
3
4 Module modComparison Variables of the same type may
5 be declared in one declaration Comparison.vb
6 Sub Main()
7
8 ' declare Integer variables for user input
9 Dim number1, number2 As Integer
10
11 The If/Then structure compares the values
' read first number from user
12 Console.Write("Please enter of number1
first and number2
integer: ") for equality
13 number1 = Console.ReadLine()
14
15 ' read second number from user
16 Console.Write("Please enter second integer: ")
17 number2 = Console.ReadLine()
18
19 If (number1 = number2) Then
20 Console.WriteLine("{0} = {1}", number1, number2)
21 End If
22
23 If (number1 <> number2) Then
24 Console.WriteLine("{0} <> {1}", number1, number2)
25 End If
26
27 If (number1 < number2) Then
28 Console.WriteLine("{0} < {1}", number1, number2)
29 End If
30
31 If (number1 > number2) Then
32 Console.WriteLine("{0} > {1}", number1, number2)
33 End If
 2002 Prentice Hall.
All rights reserved.
33
34 Outline
35 If (number1 <= number2) Then
36 Console.WriteLine("{0} <= {1}", number1, number2)
37 End If
38 Comparison.vb
39 If (number1 >= number2) Then
40 Console.WriteLine("{0} >= {1}", number1, number2)
41 End If
42
43 End Sub ' Main
44
45 End Module ' modComparison

Please enter first integer: 1000


Please enter second integer: 2000
1000 <> 2000 Program Output
1000 < 2000
1000 <= 2000

Please enter first integer: 515


Please enter second integer: 49
515 <> 49
515 > 49
515 >= 49

Please enter first integer: 333


Please enter second integer: 333
333 = 333
333 <= 333
333 >= 333
 2002 Prentice Hall.
All rights reserved.
34
6 Decision Making: Equality and
Relational Operators

Operators Associa tivity Type


() left to right parentheses
^ left to right exponentiation
* / left to right multiplicative
\ left to right integer division
Mod left to right modulus
+ - left to right additive
= <> < <= > >= left to right equality and relational
Fig. 3.19 Precedence and assoc iativity of operators introduc ed in this chapter.

Fig. 19 Precedence and associativity of operators introduced in this chapter.

 2002 Prentice Hall. All rights reserved.


35
7 Using a Dialog to Display a
Message
• Dialogs
– Windows that typically display messages to the user
– Visual Basic provides class MessageBox for creating
dialogs

 2002 Prentice Hall. All rights reserved.


36
1 ' Fig. 3.20: SquareRoot.vb Outline
2 ' Displaying square root of 2 in dialog.
3
4 Imports System.Windows.Forms ' Namespace containing MessageBox
5 SquareRoot.vb
6 Module modSquareRoot
Sqrt method of the Math class is called
7 to compute the square root of 2
8 Sub Main() Method Show of class MessageBox
9
10 ' Calculate square root of 2
11 Dim root As Double = Math.Sqrt(2)
12
13 ' Display results in dialog
14 MessageBox.Show("The square root of 2 is " & root, _
15 "The Square Root of 2")The Double data type stores floating-
16 End Sub ' Main point numbers
17
18 End Module ' modThirdWelcome Line-continuation character

Program Output

Empty command
window

 2002 Prentice Hall.


All rights reserved.
37
7 Using a Dialog to Display a
Message

Title bar

Dialog sized to
accommodate Close
contents. box

OK button allows
the user to Mouse pointer
dismiss the
dialog.

Fig. 21 Dialog displayed by calling MessageBox.Show.

 2002 Prentice Hall. All rights reserved.


38
7 Using a Dialog to Display a
Message
• Assembly
– File that contain many classes provided by Visual Basic
– These files have a .dll (or dynamic link library) extension.
– Example
• Class MessageBox is located in assembly
System.Windows.Forms.dll
• MSDN Documentation
– Information about the assembly that we need can be found in
the MSDN documentation
– Select Help > Index… to display the Index dialog

 2002 Prentice Hall. All rights reserved.


39
7 Using a Dialog to Display a
Message

Search
string

Filte
r Link to MessageBox
documentation

Fig. 22 Obtaining documentation for a class by using the Index dialog.

 2002 Prentice Hall. All rights reserved.


40
7 Using a Dialog to Display a
Message
Requirements MessageBox class
section heading documentation

Assembly
containing class
MessageBox

Fig. 23 Documentation for the MessageBox class.

 2002 Prentice Hall. All rights reserved.


41
7 Using a Dialog to Display a
Message
• Reference
– It is necessary to add a reference to the assembly if you wish
to use its classes
– Example
• To use class MessageBox it is necessary to add a reference
to System.Windows.Forms
• Imports
– Forgetting to add an Imports statement for a referenced
assembly is a syntax error

 2002 Prentice Hall. All rights reserved.


42
7 Using a Dialog to Display a
Message
Solution Explorer before
reference is added

References folder Solution Explorer after


(expanded) reference is added

System.Windows.Forms
reference

Fig. 24 Adding a reference to an assembly in the Visual Studio .NET IDE.


 2002 Prentice Hall. All rights reserved.
43
7 Using a Dialog to Display a
Message
Button
Label (displaying an Menu (e.g., Help) Text Menu bar
icon) box

Fig. 25 Internet Explorer window with GUI components.

 2002 Prentice Hall. All rights reserved.

You might also like