Visual Programming

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

VISULA BASIC PROGRAMMING &

TOOLS
• VB stands for Visual Basic, and is a High-Level Programming
Language.
• A programming language basically allows you to create programs or
applications, such as Microsoft Word.
• Visual Basic one of the most popular programming languages
around. it is also one of the easiest, and is ideal for beginners.
VISUAL BASIC (VB) DATA TYPES,
MODULES & OPERATORS
• Visual Basic uses building blocks such as Variables, Data Types,
Procedures, and Control Structures in its programming environment.

MODULES:
 Code in Visual Basic is stored in the form of modules.
 The three kind of modules are Form Modules, Standard Modules and Class
Modules.
Form Modules: A simple application may contain a single Form, and the code
resides in that Form module.
standard Module: As the application grows, additional Forms are added and
there may be a common code to be executed in several Forms. To avoid the
duplication of code, a separate module containing a procedure is created that
implements the common code and it called standard module.
Class module: are the foundation of the object oriented programming in Visual
Basic. New objects can be created by writing code in class modules.
VISUAL BASIC (VB) DATA TYPES,
MODULES & OPERATORS(cont…)
Each module can contain:
• Declarations: May include constant, type, variable and DLL procedure
declarations.
• Procedures: A sub function, or property procedure that contain pieces of
code that can be executed as a unit.

These are the rules to follow when naming elements in VB - variables,


constants, controls, procedures, and so on:
• A name must begin with a letter.
• May be as much as 255 characters long
• Must not contain a space or an embedded period or type-declaration
characters used to specify a data type; these are ! # % $ & @
• Must not be a reserved word .
• The dash, although legal, should be avoided because it may be confused
with the minus sign. Instead of First-name use First_name or FirstName
DATA TYPES IN VISUAL BASIC
1. Numeric

Byte Store integer values in the range of 0 - 255


Integer Store integer values in the range of (-32,768) - (+ 32,767
Long Store integer values in the range of (- 2,147,483,468) - (+
2,147,483,468)
Single Store floating point value in the range of (-3.4x10-38) - (+ 3.4x1038)
Currency store monetary values. It supports 4 digits to the right of decimal
point and 15 digits to the left
Double Store large floating value which exceeding the single data type
value

2. String: Use to store alphanumeric values. A variable length string can


store approximately 4 billion characters.

3. Date: Use to store date and time values. A variable declared as date
type can store both date and time values.
DATA TYPES IN VISUAL BASIC(Cont…)
4. Boolean: Boolean data types hold either a true or false value. These
are not stored as numeric values and cannot be used as such. Values are
internally stored as -1 (True) and 0 (False) and any non-zero value is
considered as true.

5. Variant: Stores any type of data and is the default Visual Basic data
type. In Visual Basic if we declare a variable without any data type by
default the data type is assigned as default.
VARIABLES
• VARIABLES: Variables are the memory locations which are used to
store values temporarily. A defined naming strategy has to be
followed while naming a variable. It must be unique within the same
scope. It should not contain any special character like %, &, !, #, @ or
$.

The different ways of declaring variables in Visual Basic:


1. Explicit Declaration
2. Using Option Explicit statement
VARIABLES(cont…)
EXPLICIT DECLARATION: Automatically whenever Visual Basic
encounters a new variable, it assigns the default variable type and value.
This is called implicit declaration.
• This type of declaration is easier for the user, to have more control
over the variables, it is advisable to declare them explicitly.
• The variables are declared with a Dim statement to name the variable
and its type.

Syntax: Dim VariableNamen As DataType


For example:
Dim strName As String
Dim intCounter As Integer
Syntax:Dim VariableName1 As DataType1, VariableName2 As
DataType2, VariableName3 As DataType3
For example:
Dim password As String, yourName As String, firstnum As
Integer
VARIABLES(cont…)
USING OPTION EXPLICIT STATEMENT: The Option Explicit
statement checks in the module for usage of any undeclared variables
and reports an error to the user. The user can thus rectify the error on
seeing this error message.

• The Option Explicit statement can be explicitly placed in the general


declaration section of each module using the following steps.
1. Click Options item in the Tools menu
2. Click the Editor tab in the Options dialog box
3. Check Require Variable Declaration option and then click the OK
button
SCOPE OF VARIABLES
• A variable is scoped to a procedure-level (local) or module-level variable
depending on how it is declared.
• The scope of a variable, procedure or object determines which part of the
code in our application are aware of the variable's existence.
• A variable is declared in general declaration section of Form, and hence is
available to all the procedures.
• If we want a variable to be available to all of the procedures within the same
module, or to all the procedures in an application, a variable is declared with
broader scope.

LOCAL VARIABLES:
• A local variable is one that is declared inside a procedure.
• This variable is only available to the code inside the procedure and can be
declared using the Dim statements.
• Variables that are declared with keyword Dim exist only as long as the
procedure is being executed.
• Example: Dim sum As Integer
SCOPE OF VARIABLES(cont…)
STATIC VARIABLES:
• Static variables are not reinitialized each time Visual Invokes a procedure
and therefore retains or preserves value even when a procedure ends.
• These static variables are also ideal for making controls alternately visible or
invisible.
• The values in a module-level and public variables are preserved for the
lifetime of an application.
• The value of a local variable can be preserved using the Static keyword.
Example: Static intPermanent As Integer
Function RunningTotal ( )
Static Accumulate
Accumulate = Accumulate + num
RunningTotal = Accumulate
End Function
SCOPE OF VARIABLES(cont…)

MODULE LEVEL VARIABLES:


• A module level variable is available to all the procedures in the module.
• They are declared using the Public or the Private keyword.
• declare a variable using a Private or a Dim statement in the declaration
section of a module—a standard BAS module, a form module, a class
module, and so on—you're creating a private module-level variable.
• We can access a module property as a regular variable from inside the
module and as a custom property from the outside.

PUBLIC VS LOCAL VARIABLES:


• A variable can have the same name and different scope.
• For example, we can have a public variable named R and within a procedure
we can declare a local variable R.
• local variable and references to R outside the procedure would access the
public variable.
CONSTANTS
• Constant also store values, but as the name implies, those values remains
constant throughout the execution of an application.
• Using constants can make your code more readable by providing meaningful
names instead of numbers.

There are two sources for constants:


• System-defined constants are provided by applications and controls.
• User-defined constants are declared using the Const statement. It is a
space in memory filled with fixed value that will not be changed.

Syntax: Const constant_name = value


Example: Const X=3.14156 Constant for procedure
Private Const X=3.14156 Constant for form and all
procedure
Public Const X=3.14156 Constant for all forms
CONSTANTS(cont…)

A Const statement's scope is the same as that of a variable declared in the


same location. You can specify scope in any of the following ways:

• To create a constant that exists only within a procedure, declare it


within that procedure.
• To create a constant available to all procedures within a class, but not
to any code outside that module, declare it in the declarations section
of the class.
• • To create a constant that is available to all members of an assembly,
but not to outside clients of the assembly, declare it using the Friend
keyword in the declarations section of the class.
• • To create a constant available throughout the application, declare it
using the Public keyword in the declarations section the class
OPERATORS IN VISUAL BASIC
• An operator is a special symbol which indicates a certain process is
carried out. The operators are used to process data.

1. ARITHMETICAL OPERATORS: are used to perform many of


the familiar arithmetic operations that involve the calculation of
numeric values represented by literals, variables, other expressions,
function and property calls, and constants.
Operators Description Example Result
+ Add 5+5 10

- Subtract 10-5 5
/ Divide 25/5 5
\ Integer Division 20\3 6
* Multiply 5*4 20
^ Exponent (power of) 3^3 27
Mod Remainder of division 20 Mod 6 2
& String concatenation "George"&" "&"Bush" "George Bush"
OPERATORS IN VISUAL BASIC(cont…)
2. COMPARISON/CONDITIONAL/RELATIONAL OPERATORS
• Comparison operators compare two expressions and return a Boolean value
that represents the relationship of their values.
• There are operators for comparing numeric values, operators for comparing
strings, and operators for comparing objects.

Operators Description Example Result


> Greater than 10>8 True
< Less than 10<8 False
>= Greater than or 20>=10 True
equal to
<= Less than or equal 10<=20 True
to
<> Not Equal to 5<>4 True
= Equal to 5=7 False
OPERATORS IN VISUAL BASIC(cont…)
3. LOGICAL OPERATORS
• Logical operators compare Boolean expressions and return a Boolean result.
• The And, Or, AndAlso, OrElse, and Xor operators are binary because they
take two operands, while the Notoperator is unary because it takes a single
operand.
• Some of these operators can also perform bitwise logical operations on
integral values.

Operators Description
OR Operation will be true if either of the operands is true
AND Operation will be true only if both the operands are true
Xor One side or other must be true but not both
Not Negates true
DATA TYPE CONVERSION
• Visual Basic functions either to convert a string into an integer or vice
versa and many more conversion functions.
Conversion To Function Meaning
Boolean Cbool The function Cbool converts any data type to
Boolean 0 or 1.
Byte Cbyte The function Cbyte converts any data type to
Byte.
Currency Ccur The function Ccur converts any data type to
currency.
Date Cdate The function Cdate converts any data type to
date.
Decimals Cdec The function Cdec converts any data type to
decimal.
Double CDbl The function CDbl converts, integer, long integer,
and single- precision numbers to double-
precision numbers.
Value val The CVal function is used to convert string to
double-precision numbers
CONTROL STRUCTURES IN VISUAL
PROGRAMMING
• Control Statements are used to control the flow of program's execution.
• Visual Basic supports control structures such as if... Then, if...Then ...Else,
Select...Case.
• Loop structures such as Do While...Loop, While...Wend, For...Next etc
method.
• Decision making process is an important part of programming because it can
help to solve practical problems intelligently.

1. If...Then selection structure: The If...Then selection structure performs


an indicated action only when the condition is True; otherwise the action is
skipped.
Syntax of the If...Then selection:
If <condition>Then Example:
statement If average>75 Then
End If txtGrade.Text = "A"
End If
CONTROL STRUCTURES IN VISUAL
PROGRAMMING(cont…)

2. If...Then...Else selection structure: The If...Then...Else selection


structure allows the programmer to specify that a different action is to be
performed when the condition is True than when the condition is False.
Syntax of the If...Then...Else selection: Example : If average>50 Then
If <condition > Then txtGrade.Text = "Pass"
statements Else
Else txtGrade.Text = "Fail"
End If
statements
End If

3. Nested If...Then...Else selection structure: Nested If...Then...Else


selection structures test for multiple cases by placing If...Then...Else
selection structures inside If...Then...Else structures.
CONTROL STRUCTURES IN VISUAL
PROGRAMMING(cont…)
Syntax of the Nested If...Then...Else selection structure:
Method 1: Method 2:
If < condition 1 > Then If < condition 1 > Then
statements statements
ElseIf < condition 2 > Then Else If < condition 2 >Then
statements statements
ElseIf < condition 3 > Then Else If < condition 3 >Then
statements statements
Else Statements Else Statements
End If End If
If average > 75 Then End If
txtGrade.Text = "A" End If
ElseIf average > 65 Then
txtGrade.Text = "B"
ElseIf average > 55 Then
txtGrade.text = "C"
ElseIf average > 45 Then
txtGrade.Text = "S" Else t
xtGrade.Text = "F"
End If
CONTROL STRUCTURES IN VISUAL
PROGRAMMING(cont…)
4. Select...Case selection structure: The Select Case structure compares
one expression to different values. The advantage of the Select Case
statement over multiple If...Then...Else statements is that it makes the
code easier to read and maintain.
Syntax of the Select...Case selection structure
Select Case expression Dim average as Integer
average = txtAverage.Text
Case value1 Select Case average
Statements Case 100 To 75
Case value2 txtGrade.Text ="A“
Case 74 To 65
Statements . . . . txtGrade.Text ="B"
Case valuen Case 64 To 55
Statements txtGrade.Text ="C"
Case 54 To 45
Case else txtGrade.Text ="S"
statements Case 44 To 0
txtGrade.Text ="F"
End Select Case Else
MsgBox "Invalid average marks“
End Select
LOOPING STATEMENTS
• Visual Basic procedure that allows the program to run repeatedly until a
condition or a set of conditions is true.
• Looping is a very useful feature of Visual Basic because it makes repetitive
works easier.
• There are three kinds of loops in Visual Basic, the Do...Loop ,the For
.......Next loop and the While.....Wend Loop.

1. Do...Loop:
• The Do...Loop executes a block of statements for as long as a condition is
True.
• Visual Basic evaluates an expression, and if it’s True, the statements are
executed.
• The Do Loop statements have four different forms, as shown below:

a) The Do While...Loop: is used to execute statements until a certain


condition is met.
Syntax : Do While condition
Block of one or more VB statements
Loop
LOOPING STATEMENTS(cont…)
Example
The following Do Loop counts from 1 to 100.
Dim number As Integer
number = 1
Do While number <= 100
number = number + 1
Loop

b) The Do...Loop While : statement first executes the statements and then test
the condition after each execution.
Syntax :
Do
Block of one or more VB statements
Loop While condition
Example
Dim number As Long
number = 0
Do number = number + 1
Loop While number < 201
LOOPING STATEMENTS(cont…)
C) The Do Until... Loop : structure tests a condition for falsity. Statements in the body of
a Do Until...Loop are executed repeatedly as long as the loop-continuation test evaluates
to False.
Syntax:
Do Until condition
Block of one or more VB statements
Loop
Example:
Dim number As Long
number=0
Do Until number > 1000
number = number + 1
Print number
Loop
For Example:
2) For....Next Loop:
1)For I=0 To 10 step 5
syntax is:
Statements
For counter = Start To End Step [Increment]
Next I
One or more VB statements
2) For counter = 100 To 0 Step -5
Next [counter]
Statements
Next counter
LOOPING STATEMENTS(cont…)
3. EXISTING LOOP:
• The exit statement allows you to exit directly from For Loop and Do Loop.
• Exit For can appear as many times as needed inside a For loop, and Exit Do can
appear as many times as needed inside a Do loop.
• Sometimes the user might want to get out from the loop before the whole repetitive
process is executed.

Exit For Exit Do


The syntax: The syntax:
For counter= start To end step (increment) Do While condition
Statements Statements
Exit for Exit do
Statement Statements
Next counter Loop
4. While... Wend Statement
• A While...Wend statement behaves like the Do While...Loop statement.
Syntax: Example The following While...Wend counts from 1 to 100
While condition Dim number As Integer
Statements number = 1
Wend While number <=100
number = number + 1
Wend
VISUAL BASIC FUNCTIONS:
• Visual Basic offers a rich assortment of built-in functions.
Numerical/Mathematical Function

Function Description
X= RND Create random number value between 0 and 1
Y=ABS(X) Absolute of X, |X|
Y=SQR(X) Square root of X
Y=SGN(X) -(-1 or 0 or 1) for (X<0 or X=0 or X>0) Y=EXP(X) 𝒆𝒆𝑿𝑿
Y=LOG(X) Natural logarithms, ln𝑋𝑋 Y=LOG(X) / LOG(10) log𝑋𝑋
Y=sin (𝑋𝑋) Trigonometric functions
Y=cos (𝑋𝑋)
Y=tan (𝑋𝑋)
Y=INT(X) Integer of X Y= FIX(X) Take the integer part
Y=ATN(X) Is arc= tan−1(𝑋𝑋) (Where X angle in radian).
VISUAL BASIC FUNCTIONS:
Function of String Variable

Function Description
Y=Len(x) Number of characters of Variable
Y=UCase (x) Change to capital letters
Y=LCase (x) Change to small letters
Y=Left (X,L) Take L character from left
Y=Right (X,L) Take L character from right
Y=Mid (X,S,L) Take only characters between S and R
VISUAL BASIC FUNCTIONS(cont…)
• Visual Basic offers different types of procedures to execute small sections of coding
in applications.
• Visual Basic programs can be broken into smaller logical components called
Procedures.
• Procedures are useful for condensing repeated operations such as the frequently used
calculations, text and control manipulation.

The benefits of using procedures in programming are:


• It is easier to debug a program a program with procedures, which breaks a program
into discrete logical limits.
• Procedures used in one program can act as building blocks for other programs with
slight modifications.

Sub Procedures:
• A sub procedure can be placed in standard, class and form modules. Each time the
procedure is called, the statements between Sub and End Sub are executed.

The syntax for a sub procedure is as follows:


[Private | Public] [Static] Sub Procedurename [( arglist)]
[ statements]
End Sub
VISUAL BASIC FUNCTIONS(cont…)
Event Procedures:
• An event procedure is a procedure block that contains the control's actual
name, an underscore (_), and the event name.

The syntax for event procedure for a Form_Load event follows:


Private Sub Form_Load()
....statement block..
End Sub
• Event Procedures acquire the declarations as Private by default.

General Procedures:
• A general procedure is declared when several event procedures perform the
same actions.
• It is a good programming practice to write common statements in a separate
procedure (general procedure) and then call them in the event procedure.
VISUAL BASIC FUNCTIONS(cont…)
. In order to add General procedure:
• The Code window is opened for the module to which the procedure is to be
added.
• The Add Procedure option is chosen from the Tools menu, which opens an
Add Procedure dialog box as shown in the figure given below.
• The name of the procedure is typed in the Name textbox.
• Under Type, Sub is selected to create a Sub procedure, Function to create a
Function procedure or Property to create a Property procedure.
• Under Scope, Public is selected to create a procedure that can be invoked
outside the module, or Private to create a procedure that can be invoked only
from within the module.
VISUAL BASIC FUNCTIONS(cont…)
. Note: We can also create a new procedure in the current module by
typing Sub ProcedureName, Function ProcedureName, or Property
ProcedureName in the Code window. A Function procedure returns a
value and a Sub Procedure does not return a value.

Function Procedures
• Functions are like sub procedures, except they return a value to the
calling procedure.
• They are especially useful for taking one or more pieces of data,
called arguments and performing some tasks with them.
• functions returns a value that indicates the results of the tasks
complete within the function.
VISUAL BASIC FUNCTIONS(cont…)
. Examples: function procedure calculates the third side or hypotenuse
of a right triangle, where A and B are the other two sides. It takes two
arguments A and B (of data type Double) and finally returns the results.

Function Hypotenuse (A As Double, B As Double) As Double


Hypotenuse = sqr (A^2 + B^2)
End Function

Note: he above function procedure is written in the general declarations


section of the Code window. A function can also be written by selecting
the Add Procedure dialog box from the Tools menu and by choosing
the required scope and type.
VISUAL BASIC FUNCTIONS(cont…)
.
Property Procedures
• A property procedure is used to create and manipulate custom
properties.
• It is used to create read only properties for Forms, Standard modules
and Class modules.
• Visual Basic provides three kind of property procedures:
1. Property Let procedure that sets the value of a property
2. Property Get procedure that returns the value of a property
3. Property Set procedure that sets the references to an object.
ARRAYS
•. An array is a variable with a single name that represents many
different items.
• When we work with a single item, we only need to use one variable.
• However, if we have a list of items which are of similar type to deal
with, we need to declare an array of variables instead of using a
variable for each item.

There are two types of arrays in Visual Basic namely:


1. Fixed-size/static array : The size of array always remains the same-
size doesn't change during the program execution.
2. Dynamic array : The size of the array can be changed at the run
time- size changes during the program execution.

Declaring one dimensional Array:


Dim arrayName(subscript) as dataType
EXAMPLE
Dim numbers(5) As Integer
ARRAYS
•. An array is a variable with a single name that represents many
different items.
• When we work with a single item, we only need to use one variable.
• However, if we have a list of items which are of similar type to deal
with, we need to declare an array of variables instead of using a
variable for each item.

There are two types of arrays in Visual Basic namely:


1. Fixed-size/static array : The size of array always remains the same-
size doesn't change during the program execution.
2. Dynamic array : The size of the array can be changed at the run
time- size changes during the program execution.

Declaring one dimensional Array:


Dim arrayName(subscript) as dataType
EXAMPLE
Dim numbers(5) As Integer
Multidimensional Arrays
•. A common use of multidimensional arrays is to represent tables of
values consisting of information arranged in rows and columns.
• To identify a particular table element, we must specify two indexes:
1. The first (by convention) identifies the element's row
2. The second (by convention) identifies the element's column.

Declaring two dimensional Array:


Dim ArrayName(Sub1,Sub2) as dataType

Example
Dim AvgMarks ( 50, 50)

DYNAMIC ARRAY: So far we have learned how to define the number of


elements in an array during design time. This type of array is known as static
array.
• dynamic array where the number of elements will be decided during run
time.
• In VB6, the dynamic array can be resized when the program is executing.
Single-quote character (')
• 1 ' Fig. 3.1: Welcome1.vb indicates that the remainder
• 2 ' Simple Visual Basic program. of the line is a comment
• 3
• Visual Basic console
4 Module modFirstWelcome
applications consist of pieces
• 5 called modules
• 6 Sub Main()
• 7 Console.WriteLine("Welcome to Visual Basic!")
• 8 End Sub ' Main
• 9 The Main procedure is the entry
point of the program. It is present
• 10 End Module ' modFirstWelcome in all console applications
The Console.WriteLine
statement displays 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
• 1 ' Fig. 3.9: Welcome2.vb
• 2 ' Writing line of text with multiple statements.
•3
• 4 Module modSecondWelcome Method Write does not position the
output cursor at the beginning of the next
•5
line
• 6 Sub Main()
•7 Console.Write("Welcome to ")
•8 Console.WriteLine("Visual Basic!")
• 9 End Sub ' Main Method WriteLine positions the
• 11 output cursor at the beginning of the next
line
• 12 End Module ' modSecondWelcome

Welcome to Visual Basic!


Program
Output
•1 ' Fig. 3.10: Addition.vb
•2 ' Addition program. These variables store strings of characters
•3 Module modAddition
•5
Declarations begin with keyword Dim
•6 Sub Main()
•7
•8 ' variables for storing user input
•9 Dim firstNumber, secondNumber As String
•10
•11 ' variables used in addition calculation
•12 Dim number1, number2, sumOfNumbers As Integer
•13
•14 ' read first number from user
•15 Console.Write("Please enter the first integer: ")
•16 firstNumber = Console.ReadLine()
•17 Method ReadLine causes program
•18 ' read second number from user to pause and wait for user input
•19 Console.Write("Please enter the second integer: ")
•20 secondNumber = Console.ReadLine()
•21
•22 ' convert input values to Integers
•23 number1 = firstNumber Implicit conversion from String to
•24 number2 = secondNumber
•25 Integer
•26 sumOfNumbers = number1 + number2 ' add numbers
•27
•28 ' display results
•29 Console.WriteLine("The sum is {0}", sumOfNumbers)
•30
•31 End Sub ' Main Format indicates that the argument
•32
•33
after the string will be evaluated and
End Module ' modAddition
incorporated into the string
40
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
6 Sub Main()
7
8 ' declare Integer variables for user input
9 Dim number1, number2 As Integer
10
11 ' read first number from user
12 Console.Write("Please enter first integer: ") The If/Then structure compares the
13 number1 = Console.ReadLine() values of number1 and number2 for
14
15 ' read second number from user
equality
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.
41
34 Outline
35 If (number1 <= number2) Then
36 Console.WriteLine("{0} <= {1}", number1, number2)
37 End If
38
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
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.
42
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
6 Module modSquareRoot
Sqrt method of the Math class is called
7 to compute the square root of 2
8 Sub Main()
9 Method Show of class MessageBox
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, The_ Double data type stores floating-
15 "The Square Root of 2") point numbers
16 End Sub ' Main
17
18 End Module ' modThirdWelcome

Line-continuation character

Empty command
window

 2002 Prentice Hall.


All rights reserved.
43

Using a Dialog to Display a Message

Title bar

Dialog sized to
accommodate
Close box
contents.

OK button allows the


user to dismiss the Mouse pointer
dialog.

Fig. Dialog displayed by calling MessageBox.Show.

 2002 Prentice Hall. All rights reserved.

You might also like