0% found this document useful (0 votes)
50 views57 pages

Module 5 Visual Basic Variables and Formulas

This document is a module from an online Visual Basic.NET course. It discusses using variables and formulas in Visual Basic programs. It explains how to declare and use variables to store and manipulate data, as well as how to perform calculations using arithmetic operators and methods from the .NET Framework. The module provides examples of program statements and teaches programming fundamentals for working with variables, data types, and formulas in Visual Basic.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
50 views57 pages

Module 5 Visual Basic Variables and Formulas

This document is a module from an online Visual Basic.NET course. It discusses using variables and formulas in Visual Basic programs. It explains how to declare and use variables to store and manipulate data, as well as how to perform calculations using arithmetic operators and methods from the .NET Framework. The module provides examples of program statements and teaches programming fundamentals for working with variables, data types, and formulas in Visual Basic.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 57

<VISUAL BASIC.

NET
ONLINE COURSE>

VISUAL BASIC.NET Microsoft Visual Basic.NET

Programming Fundamentals

MODULE
Visual Basic Variables and
Formulas, and the .NET
5
Framework

CS – 103 / IT - 202
<VISUAL BASIC.NET
ONLINE COURSE>

W el co me b ack gu ys …. ou r session n ow w ill fo cu s on … Prog rami ng


Fundament als... Lets start with…

M odule 5: Visual Ba si c Va riabl es a nd Formula s, and the .NET


F ra mework ..
The module will cover the following..
The Anatomy of a Visual Basic Program Statement..
Using Variables to Store Information ..
Setting Aside Space for Variables: The Dim Statement ..
Implicit Variable Declaration..
Using Variables in a Program..
Using a Variable to Store Input ..
Using a Variable for Output ..
Working with Specific Data Types ..
Constants: Variables That Don’t Change..
Working with Visual Basic Operators..
Basic Math: The +, –, *, and / Operators ..
Using Advanced Operators: \, Mod, ^, and & ..
Working with Math Methods in the .NET Framework..
One Step Further: Establishing Order of Precedence..
Using Parentheses in a Formula ..
Module 5 Quick Reference..
<VISUAL BASIC.NET
ONLINE COURSE>

I n th e p re v io u s m o d u le s , “ Ge t t in g Sta r t e d wi t h V i su a l Ba si c 2 0 1 0 , ”
y o u le a rn e d h o w to c r e a te th e u se r in t e r fa c e o f a M ic r o s o f t V i su a l
B a s ic 2 0 1 0 p ro g ra m a n d h o w t o b u i ld a n d ru n a p ro g ra m i n th e
M ic ro s o f t V is u a l S t u d i o 2 0 10 d e v e lo p me n t e n v i r o n m e n t. I n th e n in e
m o d u le s , “ P ro g ra m m in g Fu n d a me n ta l s , ” y o u ’l l le a rn mo re a b o u t
V i su a l Ba s ic p ro g ra m c o d e —t h e s ta te m e n t s a n d k e y w o rd s th a t fo rm
t h e c o r e o f a V i su a l Ba s ic p ro g ra m. Y o u ’ ll le a r n h o w t o ma n a g e
i n fo rm a t io n wi th in p ro gra m s a n d c o n t ro l h o w y o u r c o d e i s e x e c u te d ,
a n d y o u ’l l le a rn h o w t o u se d e c i s io n s tr u c tu re s , lo o p s , ti me r s , a r ra y s ,
c o l le c t io n s , a n d t e x t fi le s. Yo u ’l l a l so le a rn h o w t o d e b u g y o u r
p r o g ra m s a n d h a n d le r u n - tim e e r r o rs i f t h e y o c c u r . A f te r y o u
c o m p le te th e m o d u le s , y o u ’ll b e re a d y fo r mo re a d v a n c e d to p ic s , su c h
a s c u s t o m iz in g th e u se r in te r f a c e , d a ta b a se p ro g ra mm in g , a n d W e b
p r o g ra m m i n g.
<VISUAL BASIC.NET
ONLINE COURSE>

A f te r c o m p le t in g th i s m o d u le , y o u w il l b e a b l e to :
• U s e v a r ia b l e s to st o re d a ta in y o u r p ro g ra m s.
• G e t in p u t b y u si n g t h e I n p u tB o x fu n c ti o n .
• D i sp la y m e s sa g e s b y u si n g th e M s gB o x fu n c t io n .
• W o rk w i th d i f fe r e n t d a ta ty p e s .
• U s e v a r ia b l e s a n d o p e ra to r s t o ma n ip u la te d a ta .
• U s e m e th o d s i n th e . NE T F ra m e w o rk .
• U s e a ri t h m e t ic o p e ra t o rs a n d f u n c t io n s in fo rm u la s.

.
I n th is m o d u le , y o u ’ ll le a r n h o w to u s e v a r ia b le s a n d c o n s ta n t s to
s t o re d a ta t e m p o ra ri ly in y o u r p ro gra m , a n d h o w to u se t h e I n p u tB o x
a n d M s gB o x fu n c t io n s to ga th e r a n d p re se n t in fo rma t io n b y u s in g
d ia l o g b o xe s . Y o u ’ ll a l so l e a r n h o w to u se f u n c t io n s a n d f o r mu la s to
p e rf o rm c a lc u la t io n s , a n d h o w t o u se a ri th me t ic o p e ra to r s t o p e r fo rm
t a sk s s u c h a s m u l ti p l ic a ti o n a n d s t rin g c o n c a te n a ti o n . F in a ll y , y o u ’l l
l e a rn h o w to ta p in to th e p o w e r fu l c la s s e s a n d me t h o d s o f M ic ro so f t
. N ET F ra m e w o rk 4 to p e r fo rm ma th e ma t ic a l c a l c u la t io n s a n d o th e r
u se f u l wo r k .
<VISUAL BASIC.NET
ONLINE COURSE>

N o w le t s d o so m e sc ie n c e p ra c t ic e . .
T h e A n a to m y o f a V i su a l Ba si c P ro g ra m S ta te m e n t . .

A s y o u le a r n e d in M o d u l e s 2, “ W ri t in g Yo u r F i rs t P r o g ra m , ” a l in e o f
c o d e in a M ic ro s o f t V is u a l Ba si c p ro g ra m i s c a l le d a p r o gr a m
s t a te m e n t.

A p ro g ra m s ta te m e n t i s a n y c o mb in a t io n o f V i su a l Ba sic k e y w o rd s ,
p r o p e r t ie s , o b j e c t n a m e s , v a r ia b l e s , n u mb e r s , s p e c ia l s y m b o l s , a n d
o t h e r v a lu e s th a t c o l le c t iv e ly c re a te a v a lid i n s t r u c ti o n re c o g n iz e d b y
t h e V i su a l Ba s ic c o m p i le r. A c o mp le te p ro g ra m s ta t e m e n t c a n b e a
s i m p le k e y wo rd , s u c h a s . .

End.

w h ic h h a l t s th e e xe c u ti o n o f a V i s u a l Ba s ic p r o g ra m , o r it c a n b e a
c o m b in a t io n o f e le m e n t s , s u c h a s th e fo ll o w in g s ta te m e n t , wh ic h u se s
t h e T im e S t r in g p r o p e r ty t o a s s i gn t h e c u rr e n t sy s t e m t ime to t h e Te x t
p r o p e r ty o f th e La b e l 1 o b j e c t :

L a b e l 1. Te x t = Ti m e S tr in g .

T h e r u l e s o f c o n s t ru c t io n th a t mu s t b e u s e d wh e n y o u b u il d a
p r o g ra m m i n g s ta t e m e n t a re c a ll e d sta t em en t syn ta x . V i su a l Ba sic
s h a re s m a n y o f i t s sy n ta x ru l e s wi th th e o th e r d e v e l o p me n t p ro d u c t s
i n V i su a l S tu d io , a s w e l l a s e a rl ie r v e r si o n s o f th e BA SI C p r o g ra m mi n g
l a n g u a ge . Th e t r ic k to w ri t in g g o o d p ro g ra m s ta t e me n t s i s l e a rn i n g
t h e sy n ta x o f th e m o st u se fu l e le me n t s in a p ro g ra m min g la n gu a g e
a n d th e n u s in g th o s e e le m e n t s c o r re c t ly t o p ro c e s s th e d a ta in y o u r
p r o g ra m . Fo r tu n a t e ly , V is u a l Ba sic d o e s a lo t o f th e to u gh e s t w o rk f o r
y o u , so th e ti m e y o u s p e n d w r i t in g p ro g ra m c o d e i s r e la t i v e ly sh o rt ,
a n d y o u c a n re u s e th e re su l ts in f u t u re p r o g ra m s. Th e V is u a l S tu d io
I DE a l s o p o in t s o u t p o t e n tia l sy n ta x e r ro r s a n d s u gg e s ts c o r r e c ti o n s ,
m u c h a s th e A u to Co r r e c t fe a t u re o f M ic r o so f t O f fic e Wo rd d o e s.
<VISUAL BASIC.NET
ONLINE COURSE>

I n th is m o d u le a n d th e fo l lo w in g mo d u le s , y o u ’ l l l e a rn th e m o s t
i m p o r ta n t V i su a l Ba s ic k e y wo r d s a n d p ro g ra m s ta t e me n ts , a s we ll a s
m a n y o f th e o b j e c t s , p r o p e rt i e s , a n d m e th o d s p ro v id e d b y V is u a l
S t u d io c o n t r o l s a n d th e . N ET F ra m e w o rk . Yo u ’l l fi n d t h a t t h e se
k e y w o rd s a n d o b j e c t s c o m p le me n t n i c e l y th e p ro g ra m min g sk il l s
y o u ’v e a l re a d y le a rn e d a n d w i ll h e lp y o u w r it e p o w e rf u l p ro g ra m s i n
t h e fu tu re . Th e f i r s t to p ic s —v a r ia b l e s a n d d a ta t y p e s —a re c ri t ic a l
f e a tu r e s o f n e a r ly e v e ry p r o g ra m.

U s in g V a r ia b l e s to S t o re I n fo r ma ti o n . .

A va r ia b l e is a te m p o ra ry s to ra ge lo c a tio n fo r d a ta in y o u r p ro gra m.
Y o u c a n u se o n e o r m a n y v a ri a b l e s in y o u r c o d e , a n d th e y c a n c o n ta in
w o rd s , n u m b e r s , d a t e s , p ro p e r t ie s , o r o t h e r v a lu e s. B y u s i n g
v a r ia b l e s , y o u c a n a s si gn a sh o r t a n d e a sy - to - r e me mb e r n a me t o e a c h
p ie c e o f d a ta y o u p la n to wo r k w i th . V a ria b le s c a n h o l d in f o r ma ti o n
e n te re d b y t h e u se r a t r u n t im e , th e re su l t o f a sp e c i f ic c a lc u la tio n , o r
a p i e c e o f d a ta y o u wa n t to d i sp la y o n y o u r f o r m. I n sh o r t , v a r ia b le s
a re h a n d y c o n ta i n e r s th a t y o u c a n u se to s to r e a n d t ra c k a l mo s t a n y
t y p e o f i n f o rm a t io n .

U s in g v a r ia b l e s i n a V i su a l Ba si c p ro g ra m re q u i re s s o me p la n n in g.
B e fo re y o u c a n u se a v a ria b le , y o u m u s t se t a s id e me mo r y in th e
c o m p u t e r fo r t h e v a r ia b le ’ s u se . Th i s p ro c e ss i s a l i t tl e lik e re se rv in g
a s e a t a t a t h e a te r o r a b a se b a ll ga me . I ’l l c o v e r th e p r o c e s s o f
m a k i n g r e s e rv a ti o n s f o r , o r d e c la r in g , a v a r ia b le in t h e n e x t se c t io n .

S e t ti n g A s id e Sp a c e f o r V a r ia b le s: T h e Di m S ta te m e n t . .

S i n c e th e re le a s e o f V i su a l Ba si c i n 2 0 0 2 , i t h a s b e e n n e c e s sa ry f o r
V i su a l Ba s ic p ro g ra m m e r s to e x p l ic it ly d e c la re v a r ia b le s b e f o re u sin g
t h e m . Th i s wa s a c h a n g e f ro m V is u a l Ba s ic 6 a n d e a rl ie r v e r s io n s o f
V i su a l Ba s ic , wh e r e (u n d e r c e r ta in c i rc u m s ta n c e s ) y o u c o u l d d e c la re
v a r ia b l e s im p li c i t ly —in o th e r wo rd s , s im p ly b y u si n g t h e m a n d
w i th o u t h a v i n g t o in c lu d e a D i m s ta te m e n t. Th e e a r li e r p ra c t ic e wa s
<VISUAL BASIC.NET
ONLINE COURSE>

f l e xib le b u t ra t h e r r i sk y —i t c re a t e d th e p o te n t ia l fo r v a ri a b l e
c o n fu si o n a n d m i s sp e l le d v a r ia b l e n a me s , wh ic h in t ro d u c e d p o te n t ia l
b u g s in to th e c o d e th a t m igh t o r mi gh t n o t b e d i sc o v e re d la te r .

I n V is u a l Ba s ic 2 0 0 8 , a b i t o f t h e p a s t re tu rn e d in t h e a r e a o f v a r ia b l e
d e c la ra t io n : I t b e c a m e p o s sib le o n c e a ga in to d e c la re a v a r ia b l e
i m p lic i t ly . I d o n ’ t re c o m m e n d th i s f o r mo s t u s e s , h o we v e r , s o I w o n ’ t
d i sc u s s th i s fe a t u re u n ti l y o u le a r n th e re c o mm e n d e d p ro g ra m min g
p ra c t ic e , wh ic h e xp e r ie n c e d p r o g ra m me r s fa r a n d wi d e wi l l p ra i se y o u
f o r a d o p t in g.

T o d e c la re a v a ria b le i n V i su a l B a s ic 2 0 1 0 , ty p e th e v a ria b le n a m e
a f te r t h e Di m s ta te m e n t. ( D im s ta n d s f o r d ime n si o n . ) Th is d e c l a ra t io n
r e se rv e s ro o m in m e m o ry f o r th e v a r ia b le wh e n th e p ro gr a m ru n s a n d
l e t s V i su a l B a s ic k n o w wh a t t y p e o f d a ta i t sh o u ld e x p e c t t o se e la t e r.
A l th o u gh t h i s d e c la ra ti o n c a n b e d o n e a t a n y p la c e i n th e p ro g ra m
c o d e (a s lo n g a s th e d e c la ra t i o n h a p p e n s b e fo re th e v a ria b le i s u s e d ) ,
m o s t p r o g ra m m e r s d e c la r e v a ri a b l e s i n o n e p la c e a t t h e t o p o f th e i r
e v e n t p ro c e d u re s o r c o d e mo d u le s.

F o r e xa m p le , t h e f o l lo win g st a te me n t c re a te s s p a c e f o r a v a r ia b le
n a m e d La s t Na m e th a t w il l h o ld a te x tu a l , o r s t rin g , v a lu e :

D i m La s tNa m e A s S t r in g .

N o te th a t in a d d it io n to i d e n t i fy i n g t h e v a r ia b le b y n a me , I ’v e u se d
t h e A s k e y w o rd t o giv e th e v a ri a b l e a p a r tic u la r ty p e , a n d I ’v e
i d e n t i fi e d t h e ty p e b y u s in g t h e k e y w o rd S t ri n g. ( Yo u ’ l l le a r n a b o u t
o t h e r d a ta ty p e s la t e r i n th is mo d u le . ) A s t ri n g v a r ia b le c o n ta i n s
t e x tu a l in fo r m a ti o n : wo r d s , l e t te r s , sy mb o l s —e v e n n u mb e rs . I f in d
m y s e l f u si n g s t r in g v a ria b le s a l o t; th e y h o l d n a me s , p la c e s , l in e s f r o m
a p o e m , th e c o n te n t s o f a f ile , a n d ma n y o th e r “w o rd y ” d a ta .

W h y d o y o u n e e d t o d e c la r e v a r ia b l e s ? V i su a l Ba s ic wa n ts y o u t o
i d e n t i fy t h e n a m e a n d t h e t y p e o f y o u r v a ria b le s i n a d v a n c e so th a t
t h e c o m p il e r c a n s e t a si d e th e m e m o r y th e p ro g ra m w il l n e e d to s t o re
<VISUAL BASIC.NET
ONLINE COURSE>

a n d p r o c e s s th e i n fo rm a t io n h e l d in th e v a r ia b le s. M e m o r y
m a n a g e m e n t m ig h t n o t se e m l ik e a b ig d e a l t o y o u (a f te r a l l , m o d e r n
p e rs o n a l c o m p u t e r s h a v e lo ts o f RA M a n d g iga b y te s o f f re e h a rd d i sk
s p a c e ) , b u t in so m e p r o g ra ms , me m o ry c a n b e c o n s u m e d q u i c k l y , a n d
i t ’ s a g o o d p ra c t ic e t o ta k e m e m o r y a ll o c a ti o n se r io u s ly e v e n a s y o u
t a k e y o u r fi r s t s t e p s a s a p ro g ra m me r. A s y o u ’ l l so o n se e , d i f f e r e n t
t y p e s o f v a ria b le s h a v e d i f f e r e n t sp a c e re q u i re me n t s a n d s iz e
l i m i ta t io n s.

Note In some earlier version s of Visual Basic, sp ecific variable types


(su ch as Strin g or In teger) aren’t requ ired —information is simply held b y
u sin g a generic (and memory hun gry) data type called Varian t, wh ich can
hold data of an y size or format. Variants are not su pp orted in Visu al
Basic 2010, however. Although they are handy for beginning
programmers, their design makes them slow and inefficient, and they
allow variables to be converted from one type to another too easily —
sometimes cau sin g unexpected results. As yo u ’ll learn later, however,
you can still store information in generic containers called Object, which
are likewise general -pu rpose in fun ction but rather inefficient in size.

A f te r y o u d e c la re a v a ria b l e , y o u ’ re fr e e t o a s s ign in f o rma t io n to i t in


y o u r c o d e b y u s in g th e a s si gn me n t o p e r a t o r (= ). F o r e xa m p l e , th e
f o ll o w in g p r o g ra m s ta te m e n t a s s ign s th e la s t n a me “ Sa ma n t h a ” to t h e
L a s tN a m e v a ria b le :

L a s tN a m e = "S a m a n th a " .

N o te th a t I wa s c a r e fu l to a ss i gn a te x tu a l v a lu e to t h e La s t Na me
v a r ia b l e b e c a u se i t s d a ta ty p e i s S t r in g. I c a n a l so a s s ign v a l u e s wi th
s p a c e s , sy m b o ls , o r n u m b e r s t o th e v a r ia b l e , su c h a s

L a s tN a m e = " 2 9 B Re a ga n S tr e e t" .
<VISUAL BASIC.NET
ONLINE COURSE>

b u t th e v a ria b l e i s s t i ll c o n s id e re d a s t rin g v a lu e . Th e n u m b e r p o r ti o n
c o u l d b e u se d in a m a th e m a t i c a l f o rmu la o n ly i f i t w e r e f i r s t
c o n v e r te d to a n in te ge r o r a f lo a t in g - p o in t v a lu e b y u si n g o n e o f a
h a n d fu l o f c o n v e r sio n fu n c tio n s th a t I ’l l d i sc u s s in M o d u l e 1 3 ,
“ E x p l o r in g Te x t Fi le s a n d S t ri n g P r o c e s si n g. ”

A f te r t h e La s t Na m e v a ria b l e i s a s si gn e d a v a lu e , i t c a n b e u se d i n
p la c e o f th e n a m e “ Sa m a n th a ” in y o u r c o d e . Fo r e x a mp le , th e
a s si gn m e n t s ta t e m e n t

L a b e l 1. Te x t = La s tNa m e .

d i sp la y s “Sa m a n th a ” in th e la b e l n a m e d La b e l 1 o n a fo rm .
N o w le t s lo o k o n th e .
I m p l ic i t V a r ia b l e D e c la ra ti o n . .

I f y o u r e a l ly wa n t t o d e c la r e v a r ia b le s “ th e o ld wa y ” in V i s u a l Ba s ic
2 0 1 0 — t h a t i s , wi th o u t e x p l ic i tly d e c la ri n g t h e m b y u si n g th e D im
s t a te m e n t —y o u c a n p la c e t h e Op ti o n E xp lic i t O f f s ta te me n t a t th e
v e r y to p o f y o u r f o rm ’ s o r mo d u le ’ s p r o g ra m c o d e (b e f o re a n y e v e n t
p r o c e d u r e s) , a n d i t w il l tu rn o ff th e V i s u a l Ba s ic d e fa u l t r e q u i r e me n t
t h a t v a ria b le s b e d e c la re d b e f o re th e y ’ re u se d . A s I me n t i o n e d
e a r li e r , I d o n ’ t r e c o m m e n d th i s s ta te me n t a s a p e rma n e n t a d d i ti o n t o
y o u r c o d e , b u t y o u m igh t f in d i t u se fu l te mp o ra r ily a s y o u c o n v e r t
o ld e r V is u a l Ba s ic p r o g ra m s t o V i s u a l Ba s ic 2 0 1 0.

A n o th e r p o s si b i li t y i s to u se t h e Op t io n I n fe r s ta t e m e n t , wh ic h wa s
a d d e d t o V i su a l Ba sic 20 0 8 . I f Op t io n I n f e r i s s e t to On , V i su a l Ba s ic
w i ll d e d u c e o r in fe r t h e ty p e o f a v a r i a b le b y e xa mi n in g th e i n i t ia l
a s si gn m e n t y o u m a k e . Th i s a l l o w s y o u t o d e c la r e v a r ia b le s w i th o u t
s p e c i f ic a l ly id e n ti fy in g th e ty p e u s e d , a n d a ll o w in g V is u a l B a s ic t o
m a k e t h e d e te r m in a t io n . Fo r e x a mp le , t h e e x p re s si o n

D i m a t te n d a n c e = 1 0 0 .
<VISUAL BASIC.NET
ONLINE COURSE>

w i ll d e c la re t h e v a r ia b le n a m e d a t t e n d a n c e a s a n I n t e ge r , b e c a u s e
1 0 0 i s a n i n t e g e r e xp re s s io n . I n o th e r wo r d s , w i th O p t io n I n fe r s e t to
O n , i t i s th e sa m e a s ty p in g

D i m a t te n d a n c e A s I n t e ge r = 1 0 0 .

L i k e wi se , th e e xp re s s io n .

D i m a d d r e ss = " 2 9 B Re a ga n S t r e e t " .

w i ll d e c la re t h e v a r ia b le a d d r e s s a s t y p e S t r in g , b e c a u se i t s i n i t ia l
a s si gn m e n t wa s o f ty p e S t r in g . I f y o u se t O p t ion I n f e r to O f f , h o w e v e r ,
V i su a l Ba s ic wi l l d e c la re th e v a r ia b l e a s t y p e O b j e c t —a ge n e ra l
( t h o u gh so m e w h a t b u lk y a n d in e ff ic ie n t ) c o n ta in e r f o r a n y ty p e o f
d a ta .

I f y o u p la n to u se O p ti o n I n fe r to a ll o w th i s ty p e o f in fe rr e d v a ria b l e
d e c la ra t io n ( a fl e x ib le a p p ro a c h , b u t o n e th a t c o u ld p o te n t ia l ly le a d
t o u n e x p e c te d re su l t s) , p la c e th e f o l lo wi n g t wo s ta te m e n t s a t th e to p
o f y o u r c o d e m o d u le ( a b o v e t h e Cla s s Fo r m s t a te me n t ):

O p t io n E xp l ic i t O f f .
O p t io n I n fe r O n .

O p t io n E xp l ic i t O f f a l lo ws v a r ia b l e s to b e d e c la re d a s th e y a r e u s e d ,
a n d Op t io n I n fe r O n a l lo w s V i su a l Ba s ic t o d e te rm in e th e ty p e
a u t o m a t ic a l ly . Y o u c a n a ls o s e t t h e se o p t io n s u si n g t h e O p t io n s
c o m m a n d o n t h e To o l s m e n u , a s d i sc u s s e d in M o d u l e 1 , “E x p l o rin g th e
V i su a l St u d io I n te g ra t e d De v e lo p me n t En v ir o n me n t. ”

U s in g V a r ia b l e s in a P r o g ra m . .

V a r ia b le s c a n m a in ta in t h e sa m e v a lu e th ro u gh o u t a p ro g ra m , o r th e y
c a n c h a n ge v a l u e s se v e ra l t im e s , d e p e n d i n g o n y o u r n e e d s. T h e
<VISUAL BASIC.NET
ONLINE COURSE>

f o ll o w in g e xe rc i se d e m o n s t ra te s h o w a v a r ia b le n a m e d La s t Na me c a n
c o n ta i n d i f fe re n t te x t v a lu e s a n d h o w th e v a ria b le c a n b e a ss ig n e d to
o b j e c t p ro p e r ti e s .

L e t s d o s o m e e xe rc i se o n . .
C h a n ge t h e v a lu e o f a v a r ia b l e . .

S t e p 1: S t a rt V is u a l S t u d i o . a n d th e n c l ic k th e Ne w P ro j e c t .
S t e p 2: I n th e d ia lo g b o x se le c t W in d o w s A p p lic a t io n a n d c h a n ge th e
n a m e t o Te s ti n g V a ria b le a n d th e n c l ic k OK b u t t o n . Wa i t f o r th e
f o r m 1 to l o a d . Th e n . .
S t e p 3: A d d th e 2 Bu t to n , 2 La b e l c o n t r o l in y o u r n e w fo r m 1 : .
S t e p 4: C r e a te th e in t e rfa c e wi t h th e fo l lo w in g p ro p e r ty se t t in gs : .

Object.. Property.. S e t ti n g / s. .
F o rm 1 . T e x t. “ T e s tin g V a r ia b le ” .
B u t to n 1 . T e x t. “ S h o w” .
B u t to n 2 . T e x t. “ E XI T ” .
L a b e l 1. A u t o S iz e . F a l se .
B o rd e r s ty le . F i xe d 3D .
L a b e l 2. A u t o S iz e . F a l se .
B o rd e r s ty le . F i xe d 3D .

T h e Te s t in g V a r ia b le fo r m o p e n s in th e De si gn e r . V a r ia b l e Te s t i s a
s k e le t o n p ro g ra m —i t c o n ta in s a fo rm w i th la b e l s a n d b u t t o n s fo r
d i sp la y i n g o u tp u t, b u t l i t tl e p r o g ra m c o d e . Yo u ’l l a d d c o d e i n th i s
e xe rc i se . T h e Te s t in g V a ria b l e fo r m lo o k s li k e th i s:
<VISUAL BASIC.NET
ONLINE COURSE>

T h e f o rm c o n ta in s t wo la b e ls a n d t wo b u t to n s . Y o u ’ ll u se v a ria b le s to
d i sp la y in fo r m a tio n in e a c h o f th e la b e l s.

S t e p 5: D o u b le -c l ic k t h e Sh o w b u t to n . T h e Bu t to n 1_ Cl ic k e v e n t
p r o c e d u r e a p p e a r s in t h e Co d e Ed i t o r.
S t e p 6: T y p e th e f o l lo w in g p ro g ra m s t a te me n t s t o d e c la r e a n d u s e t h e
L a s tN a m e v a ria b le :

Dim LastName As String.


LastName = "Samantha".
Label1.Text = LastName.

LastName = "Mirabel".
Label2.Text = LastName.

T h e p ro gr a m s ta te m e n t s a re a r ra n ge d i n th re e gr o u p s . Th e fi r s t
s t a te m e n t d e c la re s th e La s tN a me v a ria b l e b y u si n g t h e D i m s ta te m e n t
a n d th e S t r in g ty p e . A f te r y o u ty p e th i s l in e , V i s u a l S tu d io p la c e s a
g r e e n j a g ge d l in e u n d e r th e L a s tN a me v a ria b le , b e c a u se i t h a s b e e n
d e c la r e d b u t n o t u se d in th e p ro g ra m. Th e r e is n o t h i n g w r o n g h e r e —
V i su a l St u d io i s j u s t re m in d in g y o u th a t a n e w v a ria b le h a s b e e n
c re a t e d a n d i s wa i t in g to b e u se d .

If the variab le name still has a jagged und erline wh en you finish writin g
your p rogram, it could be a sign that you misspelled a variable name
somewhere within your code.

T h e s e c o n d a n d th i rd li n e s a s s i gn t h e n a me “ Sa ma n th a ” t o th e
L a s tN a m e v a ria b le a n d th e n d i sp la y th i s n a me in th e f i r st la b e l o n t h e
f o r m . Th is e xa m p l e d e m o n s tr a te s o n e o f t h e mo s t c o m mo n u s e s o f
v a r ia b l e s in a p ro gr a m — t ra n s f e r ri n g in f o rma ti o n t o a p ro p e r ty . A s y o u
h a v e se e n b e fo re , a l l s t ri n g v a lu e s a s s ig n e d t o v a r ia b le s a re d i s p la y e d
i n re d ty p e . Th e fo u r th l in e a s si gn s t h e n a me “ M i ra b e l ” t o th e
<VISUAL BASIC.NET
ONLINE COURSE>

L a s tN a m e v a ria b l e ( in o th e r w o rd s , i t c h a n g e s th e c o n t e n t s o f th e
v a r ia b l e ). No t ic e t h a t th e se c o n d s t r in g i s lo n g e r th a n th e fi r s t a n d
c o n ta i n s a fe w b la n k sp a c e s. Wh e n y o u a s s ig n te x t s t r in gs t o
v a r ia b l e s , o r u se t h e m i n o th e r p la c e s , y o u n e e d to e n c lo s e th e te x t
w i th in q u o ta t io n m a r k s. (Y o u d o n ’t n e e d t o d o th i s wi th n u m b e r s. )
F i n a l ly , k e e p i n m in d a n o t h e r imp o r ta n t c h a ra c te r is t ic o f t h e v a r ia b le s
b e in g d e c l a r e d in t h i s e v e n t p r o c e d u r e — th e y ma in ta in th e i r sc o p e , o r
h o l d th e i r v a lu e , o n ly w i th i n t h e e v e n t p ro c e d u re y o u ’r e u s in g th e m
i n . La t e r i n th i s m o d u le , y o u ’ l l le a r n h o w t o d e c la r e v a r ia b le s s o th a t
t h e y c a n b e u se d i n a n y o f y o u r fo r m’ s e v e n t p r o c e d u r e s.

S t e p 7: C l ic k th e F o rm 1. v b [D e si gn ] t a b to d i sp la y t h e f o r m a ga in .
S t e p 8: D o u b le -c l ic k t h e E XI T b u t t o n .
T h e Bu t t o n 2_ C lic k e v e n t p ro c e d u re a p p e a r s in th e C o d e E d it o r.
S t e p 9: T y p e th e f o l lo w in g p ro g ra m s t a te me n t t o s to p th e p ro g ra m : . En d .

Y o u r s c r e e n l o o k s l ik e th i s :

S t e p 10 : C lic k th e Sa v e A ll b u t t o n o n t h e S ta n d a r d to o l b a r to sa v e y o u r
c h a n ge s .
S t e p 11 : C l ic k th e S ta r t De b u g gi n g b u t t o n o n th e S ta n d a rd t o o lb a r t o ru n
t h e p ro g ra m . T h e p r o g ra m ru n s i n th e I DE.
S t e p 12 : C lic k th e Sh o w b u t to n . T h e p r o g ra m d e c la re s t h e v a r ia b l e ,
a s si gn s t wo v a lu e s to i t , a n d c o p ie s e a c h v a lu e to th e
a p p r o p r ia te la b e l o n t h e f o rm .
<VISUAL BASIC.NET
ONLINE COURSE>

T h e p ro gr a m p r o d u c e s th e o u t p u t sh o wn i n th e fo ll o w in g sc re e n sh o t .

S t e p 13 : C l ic k th e Qu i t b u t to n to s t o p th e p ro gra m.
T h e p ro gr a m s t o p s , a n d th e d e v e lo p me n t e n v i ro n me n t re t u r n s .

N o w le t s u s s t u d y .
V a r ia b le Na m in g Co n v e n t io n s . .
N a m in g v a ria b l e s c a n b e a l it t l e t r ic k y b e c a u s e y o u n e e d t o u se n a me s
t h a t a r e sh o r t b u t i n t u i t iv e a n d e a sy to r e me mb e r. T o a v o id
c o n fu si o n , u se th e fo ll o w in g c o n v e n ti o n s wh e n n a mi n g v a ri a b l e s:
• B e g in e a c h v a ria b le n a m e wit h a l e t te r o r u n d e r sc o r e . Th i s i s a
V i su a l Ba s ic r e q u i re m e n t. V a r ia b l e n a me s c a n c o n ta i n o n l y
l e t te r s , u n d e r sc o re s , a n d n u m b e r s.
• A l th o u gh v a ria b le n a m e s c a n b e v i r tu a l ly a n y l e n g t h , t ry t o k e e p
t h e m u n d e r 3 3 c h a ra c t e rs to ma k e th e m e a s ie r t o re a d . (V a r ia b le
n a m e s we re l im i te d to 2 5 5 c h a ra c t e rs i n V i su a l Ba s ic 6 , b u t
t h a t ’ s n o lo n ge r a c o n s t ra in t. )
• M a k e y o u r v a ria b le n a m e s d e sc r ip t iv e b y c o mb in in g o n e o r mo r e
w o rd s wh e n i t m a k e s se n se to d o so . F o r e xa m p l e , th e v a r ia b l e
n a m e Sa l e sTa x Ra te i s mu c h c le a re r t h a n Ta x o r Ra t e .
• U s e a c o m b in a t io n o f u p p e rc a se a n d lo we rc a s e c h a ra c t e rs a n d
n u m b e r s . A n a c c e p t e d c o n v e n t io n i s to c a p i ta liz e th e fi r st le t te r
o f e a c h w o rd in a v a ria b le ; fo r e xa m p le , Da t e O f Bi r t h . H o w e v e r ,
s o m e p ro g ra m m e r s p re fe r t o u s e so -c a l le d c a me l c a s in g (ma k i n g
t h e fi r s t le t te r o f a v a ria b le n a me lo we rc a se ) t o d i s ti n gu i s h
v a r ia b l e n a m e s f r o m fu n c ti o n s a n d mo d u l e n a me s , wh ic h u s u a l ly
<VISUAL BASIC.NET
ONLINE COURSE>

b e g in wi t h u p p e rc a se le t t e r s. E xa m p l e s o f c a me l c a s in g in c lu d e
d a t e O f Bi r t h , e m p l o y e e Na me , a n d c o u n t e r .
• D o n ’ t u se V i su a l Ba sic k e y wo r d s , o b j e c t s , o r p ro p e r t ie s a s
v a r ia b l e n a m e s. I f y o u d o , y o u ’l l ge t a n e r ro r wh e n y o u t r y t o ru n
y o u r p r o g ra m .
• O p t io n a ll y , y o u c a n b e g in e a c h v a ria b l e n a me wi t h a t wo -
c h a ra c t e r o r th re e -c h a ra c te r a b b re v ia ti o n c o r re sp o n d in g t o th e
t y p e o f d a ta t h a t ’ s st o re d i n t h e v a r ia b le . F o r e xa m p l e , u se
s t r Na m e t o sh o w th a t th e Na m e v a r ia b le c o n ta in s s t ri n g d a ta .
A l th o u gh y o u d o n ’ t n e e d t o w o r ry t o o mu c h a b o u t th i s d e ta il
n o w , y o u sh o u ld m a k e a n o te o f th i s c o n v e n ti o n f o r la t e r — y o u ’l l
s e e i t i n p a r t s o f th e V i s u a l S t u d io H e l p d o c u me n ta t io n a n d i n
s o m e o f th e a d v a n c e d b o o k s a b o u t V i s u a l Ba s ic p r o g ra mm in g.
( T h i s c o n v e n ti o n a n d a b b re v i a ti o n sc h e me w e re o ri gin a l ly
c re a t e d b y M ic ro s o ft D i s ti n gu i sh e d En g in e e r Ch a r le s Si mo n y i a n d
i s so m e ti m e s c a l le d t h e H u n ga r ia n Na mi n g C o n v e n tio n . )

U s in g a V a r ia b le t o S to re I n p u t .

O n e p ra c t ic a l u se f o r a v a r ia b le i s t o te mp o ra r ily h o ld i n f o r ma ti o n
t h a t wa s e n te re d b y th e u se r . A l th o u g h y o u c a n o f te n u se a n o b j e c t
s u c h a s a l is t b o x o r a te x t b o x to ga t h e r t h i s in f o rma ti o n , a t ti me s
y o u m i gh t wa n t t o d e a l d i r e c t ly wi th th e u se r a n d sa v e th e i n p u t in a
v a r ia b l e ra th e r th a n i n a p r o p e rt y . On e wa y to ga th e r in p u t i s to u s e
t h e I n p u tB o x fu n c t io n to d isp la y a d ia l o g b o x o n th e sc re e n a n d th e n
u se a v a r ia b le t o s to re th e te x t t h e u s e r ty p e s. Yo u ’ l l t ry t h i s
a p p r o a c h in th e fo ll o w in g e xa m p l e .

L e t s d o a n o th e r e xe rc i se . .
G e t in p u t b y u si n g t h e I n p u tB o x fu n c ti o n . .

S t e p 1: G o t o th e Fi le M e n u a n d th e n s e l e c t Cl o se P ro j e c t t o c lo s e t h e
T e s ti n g V a ria b le P r o g ra m .
S t e p 2: C l ic k N e w P r o j e c t a n d t h e n c h a n ge th e A p p lic a t io n W in d o w 1 to
I n p u tB o x P ro g ra m .
S t e p 3: A d d 2 b u t to n a n d 1 la b e l c o n t r o l o n t h e n e w fo rm 1 .
<VISUAL BASIC.NET
ONLINE COURSE>

S t e p 4: C r e a te a n in te r fa c e wi th fo l lo w in g p ro p e r ty se t t in gs :

O b j e c t. P r o p e r t ie s . S e t ti n g s .
F o rm 1 . T e x t. “ V a r ia b l e I n p u t u s in g
I n p u t Bo x ” .
B u t to n 1 . T e x t. “Input”.
B u t to n 2 . T e x t. “ Q UI T” .
L a b e l 1. A u t o S iz e F a l se .
B o rd e r S ty le . F i xe d 3D .
F o n t. 1 2 p o in t .

T h e in t e rfa c e m a y b e s im ila r a s s h o wn h e re :

,,.. .
S t e p 5: D o u b le -c l ic k t h e I n p u t b u t to n . T h e Bu t to n 1_ Cl ic k e v e n t
p r o c e d u r e a p p e a r s in t h e Co d e Ed i t o r.
S t e p 6: T y p e th e f o l lo w in g p ro g ra m s t a te me n t s t o d e c la r e tw o v a ri a b l e s
a n d c a ll t h e I n p u t Bo x f u n c tio n :

D i m Pr o m p t , F u l l Na me A s Str i n g .
P r o m p t = " Pl e a se e n te r y o u r c o m p le t e n a me . " .

F u ll Na m e = I n p u t Bo x ( P ro m p t ) .
L a b e l 1. Te x t = Fu ll Na m e .

T h i s ti m e , y o u ’ re d e c la ri n g tw o v a r ia b le s b y u s in g th e D im s ta te m e n t:
P r o m p t a n d Fu l l Na m e . B o th v a r ia b l e s a re d e c la re d u si n g t h e S t ri n g
t y p e . ( Yo u c a n d e c la re a s m a n y v a ria b l e s a s y o u wa n t o n t h e sa m e
<VISUAL BASIC.NET
ONLINE COURSE>

l in e , a s lo n g a s t h e y a re o f th e sa m e ty p e . ) No te th a t in V i su a l Ba s ic 6 ,
t h i s sa m e sy n ta x wo u l d h a v e p ro d u c e d d i f fe r e n t re s u l t s. D i m w o u ld
c re a t e th e Pr o m p t v a ria b l e u s in g th e V a ria n t ty p e (b e c a u se n o ty p e
w a s sp e c i fi e d ) a n d th e F u l l Na m e v a r ia b le u s in g th e S tr in g ty p e . Bu t
t h i s lo g ic a l in c o n si s te n c y h a s b e e n fi xe d i n V i su a l Ba s ic v e r s io n s 2 0 0 2
a n d l a t e r.

T h e s e c o n d li n e in t h e e v e n t p ro c e d u re a s s ig n s a te x t s t ri n g to t h e
P r o m p t v a r ia b l e . Th i s m e ssa g e i s u se d a s a t e x t a rg u me n t f o r th e
I n p u tB o x fu n c tio n . (A n a rgu m e n t i s a v a lu e o r a n e x p re ss i o n p a s se d t o
a p ro c e d u re o r a f u n c t io n . ) T h e n e x t li n e c a l ls th e I n p u tB o x fu n c ti o n
a n d a s si gn s th e re su l t o f th e c a l l ( th e t e x t s t rin g th e u s e r e n te r s ) to
t h e Fu l l Na m e v a ria b l e . I n p u tB o x i s a sp e c ia l V i su a l Ba sic f u n c t io n th a t
d i sp la y s a d ia lo g b o x o n th e s c re e n a n d p r o mp t s t h e u se r f o r i n p u t. I n
a d d i t io n to a p ro m p t s t r in g , t h e I n p u tB o x fu n c t io n su p p o r t s o t h e r
a rg u m e n t s y o u m ig h t wa n t to u se o c c a s io n a ll y . Co n su l t th e V i su a l
S t u d io H e l p d o c u m e n ta t io n fo r d e ta il s.

A f te r I n p u t Bo x h a s re tu r n e d a te x t s t ri n g t o th e p r o g ra m, t h e fo u r th
s t a te m e n t in th e p ro c e d u re p la c e s t h e u se r ’ s n a me in t h e T e x t
p r o p e r ty o f th e La b e l 1 o b j e c t , wh ic h d i sp la y s i t o n th e fo r m.

S t e p 7: S a v e y o u r c h a n ge s.
S t e p 8: C l ic k th e S ta r t De b u g gi n g b u t t o n o n th e S ta n d a rd t o o lb a r t o ru n
t h e p ro g ra m . T h e p r o g ra m ru n s i n th e I DE.
S t e p 9: C l ic k th e I n p u t B o x b u t t o n .
<VISUAL BASIC.NET
ONLINE COURSE>

V i su a l Ba s ic e x e c u te s th e Bu t t o n 1_ Cl ic k e v e n t p r o c e d u r e , a n d
t h e I n p u t Bo x d ia lo g b o x o p e n s o n y o u r sc re e n , a s sh o wn h e re :

S t e p 10 : T y p e y o u r fu ll n a m e , a n d th e n c lic k O K.
T h e I n p u t Bo x f u n c t io n re tu rn s y o u r n a m e to th e p ro gr a m a n d
p la c e s i t in t h e Fu ll Na m e v a ri a b l e . Th e p ro g r a m t h e n u se s th e
v a r ia b l e t o d i sp la y y o u r n a me o n th e f o rm , a s sh o wn h e re :

U s e th e I n p u tB o x fu n c t io n i n y o u r p ro g ra m s a n y t im e y o u w a n t to
p r o m p t th e u se r fo r i n f o rm a t i o n . Y o u c a n u s e th i s fu n c t io n in
c o m b in a t io n w i th th e o th e r i n p u t c o n t r o l s t o re gu la te t h e flo w o f d a ta
i n to a n d o u t o f a p ro g ra m . I n th e n e x t e x e rc i se , y o u ’l l le a rn h o w to
u se a s im ila r f u n c ti o n t o d i sp la y t e x t i n a d ia lo g b o x .

S t e p 11 : A d d . En d . s ta te m e n t o n th e Q u i t b u t t o n e v e n t _ c l ic k p ro c e d u re .
<VISUAL BASIC.NET
ONLINE COURSE>

S t e p 12 : C l ic k th e Qu i t b u t to n o n th e f o rm t o s to p th e p ro g ra m.

T h e p ro gr a m s t o p s , a n d th e d e v e lo p me n t e n v i ro n me n t
r e a p p e a r s.

W h a t I s a Fu n c t io n ? . .
I n p u tB o x is a sp e c ia l V i su a l B a s ic k e y wo rd k n o w n a s a f u n c t io n . A
f u n c t io n is a s ta te m e n t th a t p e rf o rm s me a n in g fu l w o rk (s u c h a s
p r o m p t in g th e u se r f o r in fo rma t io n o r c a lc u la t in g a n e q u a t io n ) a n d
t h e n re t u rn s a r e s u l t to th e p r o g ra m . Th e v a lu e re t u r n e d b y a f u n c ti o n
c a n b e a ss ig n e d to a v a ria b le , a s i t wa s in th e I n p u t B o x p r o g ra m , o r i t
c a n b e a ss ig n e d to a p ro p e r ty o r a n o th e r st a t e m e n t o r fu n c t io n .
V i su a l Ba s ic fu n c ti o n s o ft e n u se o n e o r m o re a r gu m e n t s t o d e f in e
t h e i r a c t iv i ti e s . Fo r e xa m p le , t h e I n p u t Bo x fu n c ti o n y o u j u s t e xe c u t e d
u se d th e P ro m p t v a r ia b l e to d i sp la y d ia lo g b o x in s t ru c t io n s f o r th e
u se r . Wh e n a fu n c ti o n u s e s m o re t h a n o n e a rgu me n t , c o m ma s
s e p a ra t e th e a rg u m e n ts , a n d th e wh o l e g r o u p o f a r gu me n ts i s
e n c l o se d in p a re n t h e se s. T h e fo l lo w in g st a t e m e n t sh o w s a fu n c t io n
c a ll th a t h a s tw o a rg u m e n t s :

F u ll Na m e = I n pu t Bo x (P r o mp t, Ti t le ) .

N o t ic e t h a t I ’m u si n g i ta lic in th i s s y n ta x d e sc rip t io n to in d ic a t e th a t
c e r ta in i te m s a re p la c e h o ld e r s fo r in f o rma t io n y o u sp e c if y . Th i s i s a
s t y le y o u ’ ll f in d th ro u gh o u t t h e c o u rs e a n d in th e V i su a l S t u d io H e l p
d o c u m e n ta t io n .

U s in g a V a r ia b le f o r Ou tp u t . .

Y o u c a n d i sp la y t h e c o n te n ts o f a v a r ia b le b y a s si gn in g th e v a r ia b l e to
a p ro p e r t y ( su c h a s th e T e x t p ro p e r t y o f a la b e l o b j e c t ) o r b y p a ss in g
t h e v a ri a b l e a s a n a rg u m e n t t o a d ia l o g b o x f u n c t io n . On e u se fu l
d ia l o g b o x f u n c ti o n f o r d i sp la y in g o u t p u t i s th e M s gB o x fu n c ti o n .
W h e n y o u c a ll th e M sg Bo x fu n c ti o n , i t d i sp la y s a d ia lo g b o x,
s o m e t im e s c a l le d a m e s sa ge b o x , wi th v a ri o u s o p ti o n s th a t y o u c a n
s p e c i fy . Li k e I n p u t Bo x , i t ta k e s o n e o r mo r e a rg u me n ts a s in p u t , a n d
<VISUAL BASIC.NET
ONLINE COURSE>

t h e re su l t s o f th e f u n c ti o n c a ll c a n b e a s si gn e d t o a v a r ia b le . T h e
s y n ta x f o r th e M s gB o x fu n c ti o n i s

B u t to n C lic k e d = M sg Bo x (P r o m p t , Bu t to n s , T i tl e ) .

w h e re P r o m p t i s t h e t e x t to b e d i sp l a y e d in th e me s sa ge b o x; Bu t t o n s
i s a n u m b e r th a t s p e c if ie s th e b u t t o n s , ic o n s , a n d o th e r o p ti o n s to
d i sp la y fo r th e m e s sa g e b o x; a n d Ti t le i s t h e t e x t d i s p la y e d i n th e
m e s sa ge b o x t i tl e b a r . T h e v a ri a b l e Bu t to nC li ck ed i s a s s ig n e d th e
r e su l t re t u rn e d b y th e f u n c tio n , wh ic h in d ic a te s wh ic h b u t t o n th e
u se r c l ic k e d in t h e d ia lo g b o x .

I f y o u ’ re j u s t d i sp la y in g a me s sa ge u s in g th e M sg Bo x f u n c t io n , th e
B u t to nC li ck ed v a ria b l e , th e a s si gn m e n t o p e ra t o r (= ) , th e B u t t o n s
a rg u m e n t , a n d th e T i tl e a rgu me n t a re o p ti o n a l. Yo u ’ l l b e u si n g t h e
T i t le a r gu m e n t , b u t y o u wo n ’ t b e u s in g th e o th e r s i n th e f o l lo wi n g
e xe rc i se ; fo r m o re i n f o rm a t io n a b o u t t h e m ( in c lu d in g th e d i f f e r e n t
b u tt o n s y o u c a n in c lu d e in M s gB ox a n d a fe w m o re o p t io n s) , s e a rc h
f o r t h e t o p ic “M sg Bo x M e t h o d ” i n th e V i su a l S t u d i o H e lp
d o c u m e n ta t io n . A s th e a r tic le n o t e s, th e M sg Bo x f u n c t io n i s
s o m e t im e s a l so re fe r re d to a s a m e t ho d , re f le c t in g th e in t e r n a l
o r ga n iz a ti o n o f th e M ic ro so ft . V i su al Ba s ic n a m e s p a c e .

Note Visual Stud io provid es both the MsgBox fun ction and th e
MessageBox class for disp layin g text in a message box. The MessageBox
class is part of the System.Windows.Forms namesp ace; it takes
argu men ts mu ch like Ms gBox, and it is d isplayed b y u sin g the Show
meth od. I’ll u se b oth MsgBox and MessageBox in th is course.

N o w y o u ’l l a d d a M sg Bo x fu n c t io n to th e I n p u t B o x p ro gr a m to d i sp la y
t h e n a m e th a t th e u se r e n te r s in th e I n p u t Bo x d ia lo g b o x .

D i sp la y a m e s sa ge b y u s in g th e M s gB o x fu n c t io n . .

S t e p 1: I f t h e Co d e Ed i to r i sn ’ t v i s ib l e , d o u b le -c li c k th e I n p u t Bo x
b u tt o n o n t h e I n p u t B o x fo rm . Th e Bu t t o n 1_ C lic k e v e n t
<VISUAL BASIC.NET
ONLINE COURSE>

p r o c e d u r e a p p e a r s in t h e Co d e Ed i t o r. ( Th i s i s t h e c o d e y o u
e n te re d i n th e la s t e xe rc is e . )
S t e p 2: S e le c t th e fo l lo w in g s ta te me n t in th e e v e n t p r o c e d u r e ( th e
l a s t l in e ): La b e l 1. Te x t = F u l lN a me T h i s i s th e s ta te me n t t h a t
d i sp la y s th e c o n te n t s o f th e F u ll Na m e v a ri a b l e in th e la b e l.
S t e p 3: P r e s s th e D E L ET E k e y t o d e le t e th e l in e . Th e s ta t e me n t is
r e m o v e d f ro m th e Co d e Ed i to r.
S t e p 4: T y p e th e f o l lo w in g l in e in t o t h e e v e n t p r o c e d u re a s a
r e p la c e m e n t:

M s gB o x ( Fu ll Na m e , , "I n p u t Re su l t s" ) .

T h i s n e w s ta te m e n t w il l c a ll t h e M s gB o x fu n c t io n , d is p la y t h e
c o n te n t s o f th e Fu l l Na m e v a r ia b l e in th e d ia l o g b o x , a n d p la c e
t h e w o rd s I n p u t Re su l t s in th e ti t le b a r. ( Th e o p t io n a l Bu t t o n s
a rg u m e n t a n d th e B u tt o n C lic k e d v a r ia b le a r e i rr e l e v a n t h e re a n d
h a v e b e e n o m i t te d . ) Y o u r e v e n t p ro c e d u r e lo o k s l ik e th i s in t h e
C o d e Ed i to r:

S t e p 5: C l ic k th e S ta r t De b u g gi n g b u t t o n o n th e S ta n d a rd t o o lb a r .
S t e p 6: C l ic k th e I n p u t B o x b u t t o n , ty p e y o u r n a me in th e in p u t b o x,
a n d th e n c lic k O K.
<VISUAL BASIC.NET
ONLINE COURSE>

V i su a l Ba sic st o re s t h e in p u t in t h e p ro g ra m in th e Fu ll Na m e
v a r ia b l e a n d th e n d i sp la y s i t in a m e s sa ge b o x. A f t e r ty p in g th e
n a m e in t h e in p u t b o x , I r e c e iv e d th i s m e ssa ge b o x:

S t e p 7: C l ic k OK to c l o s e th e m e ssa ge b o x. T h e n c l ic k Qu i t t o c lo s e t h e
p r o g ra m .

T h e p ro gr a m c lo s e s , a n d th e d e v e lo p me n t e n v i r o n m e n t re t u r n s.

W o rk in g wi th S p e c if ic Da ta T y p e s . .

T h e S t r in g d a ta ty p e i s u s e f u l fo r ma n a gin g te x t in y o u r p r o g ra m s , b u t
w h a t a b o u t n u m b e r s , d a t e s, a n d o t h e r ty p e s o f in f o rma t io n ? To a ll o w
f o r t h e e ff ic ie n t m e m o ry m a n a ge me n t o f a ll t y p e s o f d a ta , V i su a l
B a s ic p ro v id e s s e v e ra l a d d i t io n a l d a ta ty p e s th a t y o u c a n u s e fo r y o u r
v a r ia b l e s. M a n y o f th e s e a re fa mi lia r d a ta ty p e s f ro m e a r l ie r v e r si o n s
o f BA SI C o r V is u a l Ba s ic , a n d s o me o f t h e d a ta ty p e s w e re in t r o d u c e d
i n V i su a l S tu d io 2 0 0 5 to a l lo w fo r t h e e f f ic ie n t p ro c e s s in g o f d a ta in
n e we r 6 4 -b i t c o m p u te r s.

T a b l e 5 - 1 li s t s t h e f u n d a m e n t a l (o r e le me n ta r y ) d a ta ty p e s in V i s u a l
B a s ic . Ty p e s p r e c e d e d b y a n S a r e d e s ig n e d f o r s ign e d n u mb e r s ,
m e a n in g th a t th e y c a n h o l d b o th p o si t iv e a n d n e ga tiv e v a lu e s. Ty p e s
p r e c e d e d b y a U a re u n s ign e d d a ta ty p e s , me a n in g th a t th e y c a n n o t
h o l d n e ga ti v e v a lu e s . I f y o u r p ro g ra m n e e d s t o p e r fo rm a lo t o f
<VISUAL BASIC.NET
ONLINE COURSE>

c a lc u la ti o n s , y o u m i gh t ga i n a p e r fo r ma n c e a d v a n ta ge i n y o u r
p r o g ra m s i f y o u c h o o se t h e r i gh t d a ta ty p e fo r y o u r v a ria b le s —a s iz e
t h a t ’ s n e it h e r t o o b i g n o r to o sma ll. I n t h e n e x t e xe rc i se , y o u ’ ll s e e
h o w s e v e ra l o f th e se d a ta ty p e s w o rk .

Note Variable storage size is measu red in b its. The amoun t of space
required to store on e standard (ASCII) keyboard ch aracter in memory is 8
bits, which equ als 1 b yte.
Take time to see this list of data type and learn how to u se each on e of th em –
you can have own exercise in declarin g and u sing them as a variab le . .
T A B L E 5 - 1 Fu n d a m e n ta l Da ta Ty p e s i n V i su a l Ba s ic . .
D a ta Ty p e S iz e Range S a mp le U sa g e
Short 16-bit –32,768 through 32,767 Dim Birds As Short
Birds = 12500
UShort 16-bit 0 through 65,535 Dim Days As UShort
Days = 55000
Integer 32-bit –2,147,483,648 through Dim Insects As Integer
2,147,483,647 Insects = 37500000
UInteger 32-bit 0 through 4,294,967,295 Dim Joys As UInteger
Joys = 3000000000
Long 64-bit –9,223,372,036,854,775,808 to Dim WorldPop As Long
9,223,372,036,854,775,807 WorldPop =
4800000004
ULong 64-bit 0 through Dim Stars As ULong
18,446,744,073,709,551,615 Stars = _
1800000000000000000
Single 32-bit –3.4028235E38 through Dim Price As Single
floating point 3.4028235E38 Price = 899.99
Double 64-bit –1.79769313486231E308 through Dim Pi As Double
floating point 1.79769313486231E308 Pi = 3.1415926535
<VISUAL BASIC.NET
ONLINE COURSE>

Data Type Size Range Sample Usage


Decimal 128-bit 0 through +/–79,228,162,514,264, Dim Debt As Decimal
337,593,543,950,335 Debt = 7600300.5D
(+/–7.9 . . . E+28) with no
decimal point; 0 through +/–
7.9228162514264337593543950335
with 28 places to the right of the
decimal. Append “D” if you want
to force Visual Basic to initialize a
Decimal
Byte 8-bit 0 through 255 (no negative Dim RetKey As Byte
numbers) RetKey = 13
SByte 8-bit –128 through 127 Dim NegVal As SByte
NegVal = –20
Char 16-bit Any Unicode symbol in the range Dim UnicodeChar As
0–65,535. Append “c” when Char
initializing a Char. UnicodeChar = "Ä"c
String Usually 0 to approximately 2 billion Dim Dog As String
16-bits 16-bit Unicode characters Dog = "pointer"
per character
Boolean 16-bit True or False. (During conversions, Dim Flag as Boolean
0 is converted to False, other values Flag = True
to True.)
Date 64-bit January 1, 0001, through Dim Birthday as Date
December 31, 9999 Birthday = #3/1/1963#
Object 32-bit Any type can be stored in a variable Dim MyApp As Object
of type Object. MyApp = CreateObject _
("Word.Application")
<VISUAL BASIC.NET
ONLINE COURSE>

L e t s d o a n e x e r c i se o n - . .
U s e fu n d a m e n ta l d a ta ty p e s i n c o d e . .
R e a d y y o u r V B. N E T - .
S t e p 1: O p e n V i su a l S tu d io .
S t e p 2: S e le c t Ne w P ro j e c t .
S t e p 3: C h a n ge t h e Na m e t o Da ta Ty p e P r o g ra m th e n c lic k O K b u tt o n .
A n e w b la n k f o rm 1 w il l b e c re a t e d - .

Y o u n o w m a y b e a sk in g wh a t i s a Da ta Ty p e ? …
D a ta Ty p e s - i s a c o m p le te V i s u a l Ba s ic p r o g ra m th a t I c re a t e d t o
d e m o n s tra t e h o w th e f u n d a m e n ta l d a ta t y p e s wo r k . Yo u ’ l l r u n
t h e p ro g ra m to se e wh a t th e d a ta ty p e s l o o k l ik e , a n d th e n y o u ’l l
l o o k a t h o w th e v a ria b l e s a re d e c la re d a n d u se d i n th e p r o g ra m
c o d e . Y o u ’l l a l so le a r n wh e re to p la c e v a r ia b l e d e c la ra t io n s so
t h a t t h e y ’ re a v a ila b le to a ll t h e e v e n t p r o c e d u re s in y o u r
p r o g ra m .
Next is - .
S t e p 4: C r e a te a n in te r fa c e , a d d th e f o l lo wi n g c o n t ro l a n d th e n s e t i t s
p r o p e r t y a c c o rd in gly :

Object - . P r o p e r t ie s - . S e t ti n g s - .
F o rm 1 - . Text- . “ Da ta Ty p e s Sa m p l e ” - .
Label1- . Text- . “ S e l e c t a d a ta ty p e ” - .
Label2- . Text- . none- .
Label3- . Text- . “ Sa mp le Da ta ” - .
L i s t Bo x 1 - - . .
B u t to n 1 - . Text- . “ E XI T ” - .
<VISUAL BASIC.NET
ONLINE COURSE>

Y o u r in t e rfa c e wi n d o w s a p p li c a ti o n sh o u ld b e lo o k s im ila r t o
this- .:

N o w y o u ’l l e xa m i n e h o w th e fu n d a m e n ta l d a ta t y p e s a r e d e c la re d a t
t h e to p o f t h e f o rm a n d h o w t h e y ’ re u se d i n th e
L i s t Bo x 1_ Se le c t e d I n d e x Ch a n g e d e v e n t p ro c e d u r e .

L e t s s ta r t b y - .
S t e p 5: D o u b le -c l ic k t h e fo r m i t se l f ( n o t a n y o b j e c t s o n th e fo r m) , a n d
e n la r ge th e Co d e Ed i to r t y p e th e f o l lo wi n g c o d e :

‘ D e c la r e v a r ia b le s h e re s o th a t th e y c a n b e u s e d in .
‘ a ll o f th i s fo r m ’s e v e n t p r o c e d u re s - . .
D i m B i rd s A s S h o r t - . .
D i m I n se c ts A s I n t e ge r - . .
D i m Wo r ld P o p A s Lo n g - . .
D i m Pr ic e A s Sin gl e - . .
D i m Pi A s D o u b le - . .
D i m D e p t A s De c i ma l - . .
D i m R e t Ke y A s By t e - .
D i m U n ic o d e Ch a r A s Ch a r - . .
D i m D o g A s S t r in g - . .
D i m Fla g A s Bo o le a n - . .
D i m B i r th d a y A s Da te - . .
<VISUAL BASIC.NET
ONLINE COURSE>

B y p la c in g e a c h D im s ta t em e n t h e r e , a t t h e t o p o f th e fo r m ’ s c o d e
i n i t ia liz a t io n a re a , e n su r in g t h a t t h e v a r ia b le s w il l b e v a li d , o r w il l
h a v e sc o p e , f o r a l l o f th e fo r m ’ s e v e n t p ro c e d u r e s. Th a t wa y , y o u c a n
s e t t h e v a lu e o f a v a r ia b l e in o n e e v e n t p r o c e d u re a n d re a d it i n
a n o th e r. N o rm a lly , v a ria b l e s a r e v a lid o n ly in th e e v e n t p r o c e d u r e in
w h ic h th e y ’ re d e c la re d . T o ma k e t h e m v a l id a c ro s s th e f o r m , y o u n e e d
t o d e c la re v a ria b le s a t th e to p o f y o u r fo rm ’ s c o d e .

N o te I ’v e g iv e n e a c h v a ria b le th e sa me n a m e a s th e d a ta ty p e s ta b le
e a r li e r in th e m o d u le s o th a t y o u c a n se e th e e xa mp l e s sh o w e d y o u in
a c tu a l p r o g ra m c o d e .

Next is - .
S t e p 6: G o t o th e d e s ig n e r ta b th e n d o u b le c l ic k t h e Fo r m 1 o b j e c t t o
v ie w th e e d i to r c o d e
<VISUAL BASIC.NET
ONLINE COURSE>

S t e p 7: T y p e th e f o l lo w in g c o d e o n th e Fo rm 1 _ Lo a d e v e n t p r o c e d u r e :

' A d d i t e m s in t h e li s t b o x ( Se e M o d u le 3 ) - .
Li s tB o x 1. I te m s. A d d ( "S h o r t " ) . . .
Li s tB o x 1. I te m s. A d d ( "I n t e ge r ") . . .
Li s tB o x 1. I te m s. A d d ( " Lo n g" ) . . .
Li s tB o x 1. I te m s. A d d ( "S in gl e " ) . . .
Li s tB o x 1. I te m s. A d d ( "Do u b le " ) . . .
Li s tB o x 1. I te m s. A d d ( "De c ima l ") . . .
Li s tB o x 1. I te m s. A d d ( "By t e " ) . . .
Li s tB o x 1. I te m s. A d d ( "C h a r" ) . . .
Li s tB o x 1. I te m s. A d d ( "S tr i n g ") . . .
Li s tB o x 1. I te m s. A d d ( "B o o le a n " ) . . .
Li s tB o x 1. I te m s. A d d ( "Da t e . . .

T h e f o l lo wi n g s ta t e m e n ts , wh ic h a d d i te m s to th e l is t b o x o b j e c t i n
t h e p ro g ra m . ( Y o u m i gh t re m e mb e r th i s sy n ta x f r o m M o d u le 3 ,
“ W o rk in g w i th T o o l b o x C o n tr o l s ”

S t e p 8: R e tu rn to th e d e si gn e r ta b to v i e w th e in t e rfa c e th e n d o u b le
c lic k th e Li s t Bo x 1 t h e n e n te r t h e c o d e in th e
L i s t Bo x 1_ S e l e ct e dI nd e xC h ang e d e v e n t p ro c e d u re . .

Select Case Lis tBox1.SelectedIndex ...


Case 0...
Birds = 12500 ...
Labe l2.Text = Birds. . .

Case 1. . .

Insects = 37500000. . .

Labe l2.Text = Insects . . .

Case 2. . .

WorldPop = 4800000004. . .

Labe l2.Text = WorldPop. . .

Case 3. . .

Price = 899.99. . .

Labe l2.Text = Price. . .

Case 4. . .

Pi = 3.1415926535. . .

Labe l2.Text = Pi. . .

Case 5. . .
De pt = 7600300.5- .

...
Labe l2.Text = De pt

Case 6...
RetKey = 13 ...
Labe l2.Text = RetKey ...
Case 7...
UnicodeCha r = "A"c ...
Labe l2.Text = UnicodeChar. . .

Case 8. . .

Dog = "pointe r". . .

Labe l2.Text = Dog . . .

Case 9. . .

Flag = True. . .

Labe l2.Text = Flag . . .

Case 10. . .

Birthday = #8/22/2020#. . .

Labe l2.Text = Birthday . . .

End Select. . .
<VISUAL BASIC.NET
ONLINE COURSE>

T h e h e a r t o f th e e v e n t p r o c e d u re i s a S e le c t Ca se d e c i s io n s t ru c tu r e .
I n th e n e x t m o d u l e , w e ’l l d i sc u s s h o w th i s g r o u p o f p r o g ra m
s t a te m e n t s s e le c t s o n e c h o ic e f ro m ma n y . Fo r n o w , n o tic e h o w e a c h
s e c t io n o f t h e Se le c t Ca s e b lo c k a s s ign s a sa mp le v a l u e to o n e o f th e
f u n d a m e n ta l d a ta ty p e v a r ia b le s a n d t h e n a s s ig n s th e v a r ia b l e to th e
T e x t p r o p e r ty o f th e La b e l 2 o b j e c t o n th e f o rm. W e u s e d c o d e l ik e th i s
<VISUAL BASIC.NET
ONLINE COURSE>

i n M o d u le 3 to p ro c e ss l i s t b o x c h o ic e s , a n d y o u c a n u se t h e s e
t e c h n i q u e s to wo rk w i th l is t b o xe s a n d d a ta ty p e s in y o u r o wn
p r o g ra m s.

Note If you have more than one form in your project, you need to declare
variables in a sligh tly differen t way (and p lace) to give th em scop e
throughout your program (that is, in each form that your project
contains). The typ e of variable that you’ll declare is a public, or global,
variable, and it’s declared in a module, a special file that con tain s
declarations and procedures
not associated with a particular form.

S t e p 9: C l ic k th e S ta r t De b u g gi n g b u t t o n o n th e S ta n d a rd t o o lb a r .
T h e f o l lo wi n g a p p lic a t io n w in d o w o p e n s:

S t e p 10 : C l ic k th e I n te ge r ty p e in t h e li s t b o x.
T h e n u m b e r 3 7 5 0 0 0 0 0 a p p e a r s in t h e Sa m p le Da ta b o x.
<VISUAL BASIC.NET
ONLINE COURSE>

S t e p 11 : C l ic k th e Da te ty p e i n th e l i st b o x. Th e d a te 8/ 2 2 / 2 0 2 0
a p p e a r s in th e Sa m p le Da ta b o x.
S t e p 12 : C l ic k e a c h d a ta t y p e in th e l is t b o x to se e h o w V i su a l Ba si c
d i sp la y s it i n th e Sa mp le Da ta b o x.
S t e p 13 : C l ic k th e Qu i t b u t to n to s t o p th e p ro gra m.
S t e p 14 : I f y o u m a d e a n y c h a n ge s y o u wa n t to sa v e to d is k , c lic k t h e
S a v e A l l b u t t o n o n t h e S ta n d a rd t o o lb a r.

U s e r- De f in e d Da ta T y p e s . .

V i su a l Ba s ic a l so le t s y o u c re a t e y o u r o wn d a ta ty p e s. Th i s fe a tu r e is
m o s t u se f u l wh e n y o u ’ re d e a lin g wi t h a g ro u p o f d a ta i te m s th a t
n a t u ra l ly f it to g e th e r b u t fa ll i n to d i f fe r e n t d a ta c a te go ri e s. Yo u
c re a t e a u s er -d ef in ed t yp e ( U DT ) b y u sin g th e S t ru c tu re s t a te me n t ,
a n d y o u d e c la r e v a r ia b l e s a ss o c ia te d w i th t h e n e w ty p e b y u s in g th e
D i m s ta te m e n t. Be a wa r e th a t t h e S t ru c tu r e s ta te m e n t c a n n o t b e
l o c a te d in a n e v e n t p r o c e d u r e — i t m u st b e l o c a te d a t th e to p o f th e
f o r m a lo n g wi th o th e r v a ria b le d e c la ra ti o n s , o r i n a c o d e mo d u le .

F o r e xa m p le , t h e f o l lo win g d e c la ra ti o n c r e a te s a u se r -d e f i n e d d a ta
t y p e n a m e d E m p lo y e e th a t c a n st o re t h e n a me , d a te o f b i r t h , a n d h i re
d a t e a s so c ia te d w i th a w o rk e r:

S t r u c tu re E m p lo y e e .
D i m Na m e A s St r in g .
D i m Da t e Of B i rt h A s Da te .
D i m H ir e Da t e A s Da te .
E n d S t ru c t u re .

A f te r y o u c re a te a d a ta t y p e , y o u c a n u s e i t i n th e p r o g ra m c o d e fo r
t h e fo rm ’ s o r m o d u l e ’s e v e n t p r o c e d u re s. Th e fo ll o w in g s t a te me n t s
u se th e n e w Em p l o y e e ty p e . T h e f i r s t s ta te me n t c r e a te s a v a r ia b le
n a m e d P r o d u c t M a n a g e r , o f th e Emp lo y e e t y p e , a n d t h e se c o n d
s t a te m e n t a s s ig n s th e n a m e “ R e d A . M i ra b e l ” to th e Na me c o mp o n e n t
o f th e v a ria b le :
<VISUAL BASIC.NET
ONLINE COURSE>

D i m Pr o d u c t M a n a ge r A s Emp lo y e e .
P r o d u c tM a n a ge r. Na m e = " Re d A . M i ra b e l " .

T h i s l o o k s a l i t t le s im ila r t o s e t t in g a p r o p e r ty , d o e sn ’ t it ? V i su a l
B a s ic u se s th e sa m e n o ta t i o n f o r th e re la ti o n sh ip b e tw e e n o b j e c t s a n d
p r o p e r t ie s a s i t u s e s fo r t h e r e la ti o n sh ip b e tw e e n u se r -d e f in e d d a ta
t y p e s a n d c o m p o n e n t v a ria b l e s.
Y o u h a v e l e a rn e d h o w t o u se Da ta Ty p e s. . G o o d J o b ! . . .
N o w le t s g o t . . .
C o n s ta n ts : V a r ia b le s Th a t Do n ’ t Ch a n ge . .

I f a v a ria b le i n y o u r p r o g ra m c o n ta in s a v a lu e th a t n e v e r c h a n ge s
( s u c h a s π , a fi xe d m a t h e m a t i c a l e n t i ty ), y o u m ig h t c o n si d e r s t o rin g
t h e v a lu e a s a c o n s ta n t in s te a d o f a s a v a r ia b le . A c o n s ta n t i s a
m e a n in g fu l n a m e th a t ta k e s t h e p la c e o f a n u m b e r o r a te x t s t r in g
t h a t d o e s n ’ t c h a n ge . Co n s ta n t s a r e u s e f u l b e c a u s e th e y i n c re a se th e
r e a d a b i l it y o f p ro g ra m c o d e , t h e y c a n re d u c e p ro g ra mm in g m i s ta k e s ,
a n d th e y m a k e gl o b a l c h a n ge s e a si e r to a c c o mp l i sh la te r . C o n s ta n t s
o p e ra te a l o t li k e v a r ia b le s , b u t y o u c a n ’ t m o d i fy th e i r v a lu e s a t ru n
t i m e . T h e y a r e d e c la r e d wi th th e C o n s t k e y wo rd , a s sh o w n in t h e
f o ll o w in g e xa m p l e :

C o n s t P i A s Do u b le = 3. 1 4 1 59 2 6 5 …

T h i s st a t e m e n t c re a t e s a c o n s ta n t n a m e d P i th a t c a n b e u s e d in p la c e
o f th e v a lu e o f π in t h e p r o gr a m c o d e . To ma k e a c o n s ta n t a v a i la b le t o
a ll t h e o b j e c t s a n d e v e n t p ro c e d u r e s i n y o u r fo r m , p la c e th e
s t a te m e n t a t th e to p o f y o u r f o rm a lo n g wi t h o t h e r v a r ia b le a n d
s t r u c tu r e d e c la ra t io n s t h a t w i ll h a v e sc o p e in a ll o f th e f o r m ’s e v e n t
p r o c e d u r e s. To m a k e th e c o n s ta n t a v a i la b le t o a ll th e f o r m s a n d
m o d u le s in a p r o g ra m ( n o t j u s t F o r m 1 ), c re a te th e c o n s ta n t in a c o d e
m o d u le , w i th th e Pu b li c k e y w o rd in f r o n t o f it . Fo r e xa mp le :

P u b lic Co n s t P i A s Do u b l e = 3 . 1 4 1 5 9 2 6 5 ….
<VISUAL BASIC.NET
ONLINE COURSE>

T h e f o l lo wi n g e xe rc is e d e m o n s t ra te s h o w y o u c a n u se a c o n s ta n t in a n
e v e n t p ro c e d u re .
L e t s d o a n o th e r Ex e re c i se …
U s e a c o n s ta n t in a n e v e n t p r o c e d u r e . .

S t e p 1: O p e n th e V i su a l S t u d io a n d c lic k o n th e Ne w P r o j e c t
S t e p 2: C h a n ge t h e n a m e o f t h e p r o j e c t to
C o n s ta n ts V a ria b le P ro g ra m th e n c li c k O K.
S t e p 3: D e si gn t h e in te r fa c e w i th th e fo l lo w in g o b j e c t s a n d
s e t t in g s :

Object… P r o p e r t ie s … S e t ti n g / s …
Form1. Text. “Constant Variable”.
Button1. Text. “Show Constant”.
Button2. Text. “Quit”.
Label1. Text. None.
AutoSize. False .
BorderStyle. Fixed3D.

Y o u r w in d o w s a p p l ic a t io n i n t e r fa c e sh o u l d b e si mi la r a s s h o wn h e r e :

C o n s ta n ts a re u se fu l i n p r o gr a m c o d e , e s p e c ia l ly in in v o lv e d
m a t h e m a t ic a l fo r m u la s , s u c h a s A re a = π r 2. T h e n e x t se c t io n d e sc r ib e s
h o w y o u c a n u se o p e ra t o rs a n d v a ri a b l e s to w r i te s im ila r f o rmu la s.
<VISUAL BASIC.NET
ONLINE COURSE>

S t e p 4: T y p e th e f o l lo w in g s ta t e me n t s in t h e Bu t to n 1_ Cl ic k e v e n t
procedure:

C o n s t P i A s Do u b le = 3. 1 4 1 59 2 6 5 …
L a b e l 1. Te x t = Pi …

T h e l o c a ti o n y o u c h o o s e fo r y o u r d e c la ra t io n s sh o u ld b e b a s e d
o n h o w y o u p la n t o u se th e c o n s ta n t s o r th e v a ria b le s.
P r o g ra m m e r s ty p ic a l ly k e e p t h e sc o p e fo r d e c la ra ti o n s a s sm a l l
a s p o s si b le , w h i le s t il l ma k in g th e m a v a i la b le fo r c o d e th a t n e e d s
t o u se t h e m . Fo r e xa m p le , i f a c o n s ta n t i s n e e d e d o n ly in a si n g le
e v e n t p ro c e d u re , y o u sh o u ld p u t th e c o n s ta n t d e c l a ra t io n w i th i n
t h a t e v e n t p ro c e d u r e . H o w e v e r , y o u c o u ld a l s o p la c e th e
d e c la ra t io n a t th e to p o f t h e f o rm ’ s c o d e , wh ic h w o u l d g iv e a l l
t h e e v e n t p r o c e d u re s in y o u r f o r m a c c e ss to i t.

S t e p 5: C l ic k th e S ta r t De b u g gi n g b u t t o n o n th e S ta n d a rd t o o lb a r t o
r u n th e p ro g ra m .
S t e p 6: C l ic k th e S h o w Co n s ta n t b u tt o n .

T h e P i c o n s ta n t a p p e a r s in th e l a b e l b o x, a s sh o wn h e r e :

S t e p 7: C l ic k th e Qu i t b u t to n to s t o p th e p ro gra m.
<VISUAL BASIC.NET
ONLINE COURSE>

W o rk in g wi th V i s u a l Ba s ic Op e ra t o r s . .

A f o rm u la i s a s ta te m e n t th a t c o mb in e s n u mb e r s , v a ria b le s ,
o p e ra to r s , a n d k e y wo rd s t o c re a t e a n e w v a lu e . V i s u a l Ba si c c o n ta i n s
s e v e ra l la n gu a ge e le m e n t s d e s ign e d f o r u se i n fo r mu la s. I n th is
s e c t io n , y o u ’l l p ra c t ic e w o rk i n g w i th a r i th m e tic ( o r ma t h e ma tic a l )
o p e ra to r s , t h e sy m b o l s u s e d t o t ie t o ge t h e r th e p a r t s o f a f o rmu la .
W i t h a fe w e x c e p t io n s , t h e a r i t h me t ic sy mb o l s y o u ’ ll u se a r e th e o n e s
y o u u s e in e v e ry d a y l i fe , a n d th e i r o p e ra ti o n s a r e fa i r ly i n tu i tiv e .
Y o u ’ ll se e e a c h o p e ra to r d e m o n s t ra te d i n th e f o ll o w in g e xe rc i se s.

V i su a l Ba s ic in c lu d e s th e a rit h me ti c o p e ra to r s li s t e d in Ta b le 5 - 2 .

T A B L E 5 - 2 A r i th m e tic Op e ra to r s .
O p e ra t o r . D e sc ri p ti o n .
+. A d d i t io n .
–. S u b t ra c t io n .
*. M u l t ip lic a t io n .
/. D iv i si o n .
\. I n t e ge r ( wh o l e n u mb e r )
d iv is io n .
Mod. R e ma in d e r d iv i s io n .
^. E x p o n e n t ia ti o n ( ra i s in g to a
power).
&. S t r in g c o n c a te n a ti o n
( c o mb i n a ti o n ) .

B a s ic M a th : T h e + , – , * , a n d / Op e ra t o rs . .

T h e o p e ra t o r s fo r a d d i ti o n , s u b t ra c t io n , m u l t ip lic a t io n , a n d d iv i s io n
a re p re t ty s t ra ig h tf o rwa rd a n d c a n b e u se d in a n y f o rmu l a w h e re
n u m b e r s o r n u m e ric v a ria b le s a r e u s e d . Th e fo ll o w in g e x e rc i se
d e m o n s tra t e s h o w y o u c a n u s e t h e m i n a p ro g ra m.
<VISUAL BASIC.NET
ONLINE COURSE>

O u r e xe rc i s e n o w w il l fo c u s o n …
W o rk w i th b a sic o p e r a t o r s …
R e a d y y o u r V B. N E T …
S t e p 1: O p e n th e V i su a l S t u d io , c li c k Ne w P ro j e c t
S t e p 2: C h a n ge t h e n a m e o f t h e p r o j e c t to Ba si c M a th P ro gr a m

S t e p 3: C r e a te a n in te r fa c e , a d d th e f o l lo wi n g c o n t ro l t o th e n e w Fo rm 1
t h e n c h a n g e th e p ro p e r ti e s a c c o rd i n g ly :
L e t s s ta r t …
Object… P r o p e r t ie s … S e t ti n g s …
Form1. Text. “Basic Math Operator”.
Label1. Text. “Enter first Integer ”.
Label2. Text. “En ter second In teger” .
Label3. Text. “Result”.
GroupBox. Text. “Operator”.
RadioButton1. Text. “Addition [+]”.
RadioButton2. Text. “Subtraction [-]”.
RadioButton3. Text “Multiplication [*]” .
RadioButton4. Text. “Division [/]”.
TextBox1. Text. None.
TextAlign. Right.
TextBox2. Text. None.
TextAlign. Right.
TextBox3. Text. None.
TextAlign. Right.
Button1. Text. “CALCULATE”.
Button1. Text. “QUIT”.
N o te : a ll t e xt fo n t p ro p e r ty i s 1 2 -p o in t s iz e …
T h e p ro gr a m in te r fa c e sh o u ld b e s im i la r to t h i s: …
<VISUAL BASIC.NET
ONLINE COURSE>

S t e p 4: D o u b le -c l ic k t h e Fo r m 1 to v ie w c o d e e d i to r …

S t e p 5: D e c la r e t wo v a r ia b le s o f ty p e D o u b le a t th e to p th e f o rm ’ s c o d e :

' D e c la r e Fi r s t Nu m a n d S e c o n d Nu m v a r ia b l e s . .
D i m Fi r s t Nu m , Se c o n d N u m A s Do u b l e …

T h e D o u b le ty p e i s a la rg e a n d th e g e n e ra l -p u rp o se v a r ia b le t y p e th a t
c o u l d h a n d l e m a n y d i f fe re n t n u m b e r s —in t e ge r s , n u m b e rs wi t h
d e c i m a l p o in t s , v e ry b ig n u m b e r s , sma l l n u mb e r s , a n d so o n . Th e
v a r ia b l e s a re d e c la re d o n th e sa me l in e b y u sin g th e sh o r t c u t
n o ta ti o n . Bo t h Fi r s t Nu m a n d Se c o n d Nu m a re o f ty p e Do u b le , a n d a r e
u se d to h o ld th e v a lu e s in p u t in t h e f i r st a n d se c o n d t e xt b o xe s ,
r e sp e c tiv e l y .

S t e p 6: R e tu rn to th e d e si gn wi n d o w th e n c lic k th e Ca lc u la t e b u t t o n
e n te r th e f o ll o w in g c o d e a t t h e B u t to n 1_ C li ck e v e n t p ro c e d u re :
<VISUAL BASIC.NET
ONLINE COURSE>

T h e f i r s t t wo s ta te m e n t s in th e e v e n t p r o c e d u re t ra n s f e r d a ta e n t e re d
i n th e te x t b o x o b j e c ts i n to t h e Fi r s t Nu m a n d Se c o n d Nu m v a ria b l e s .

' A s si gn t e x t b o x v a l u e s to v a r ia b l e s .
F i r s t Nu m = Te x t Bo x 1. Te x t .
S e c o n d Nu m = Te x t Bo x 2. Te x t .

T h e Te x t Bo x c o n t ro l h a n d l e s t h e t ra n s fe r w i th t h e Te x t p r o p e r ty —a
p r o p e r ty th a t a c c e p t s te x t e n t e r e d b y th e u s e r a n d ma k e s i t a v a ila b l e
f o r u se in t h e p ro g ra m . I ’ ll ma k e f r e q u e n t u se o f t h e Te x t B o x c o n t ro l
i n th i s m o d u le . Wh e n i t ’ s s e t t o m u l t il in e a n d re si z e d , i t c a n d i sp la y
m a n y li n e s o f te x t —e v e n a w h o le f il e ! A f t e r th e te x t b o x v a lu e s a re
a s si gn e d to th e v a ria b le s , th e e v e n t p r o c e d u r e d e te rm in e s wh ic h
r a d io b u t to n h a s b e e n se le c te d , c a lc u la te s th e ma t h e ma ti c a l f o rmu la ,
a n d d i sp l a y s th e re su l t in a th i rd t e x t b o x. T h e f i rs t ra d i o b u t to n te s t
l o o k s l ik e th i s:

' D e te rm in e c h e c k e d b u tt o n a n d c a lc u la te .
I f Ra d io Bu t to n 1. Ch e c k e d = Tr u e T h e n .
T e x tB o x3 . T e x t = F i rs t Nu m + S e c o n d Nu m .
End If.

R e m e m b e r f ro m M o d u le 3 th a t o n ly o n e ra d i o b u t t o n o b j e c t i n a
g r o u p b o x o b j e c t c a n b e se le c t e d a t a n y gi v e n t im e . Yo u c a n te ll
w h e th e r a ra d io b u t t o n h a s b e e n s e l e c te d b y e v a l u a ti n g t h e Ch e c k e d
p r o p e r ty . I f i t ’s T r u e , th e b u t t o n h a s b e e n se le c te d . I f t h e C h e c k e d
p r o p e r ty i s Fa ls e , th e b u t t o n h a s n o t b e e n s e l e c te d . A f te r th i s s im p l e
t e s t , y o u ’ re re a d y to c o m p u te th e r e su l t a n d d is p la y i t in t h e th i rd
t e x t b o x o b j e c t. Th a t ’ s a ll t h e r e i s to u s in g b a s ic a r i th m e t i c o p e ra to r s.
( Y o u ’ ll le a r n m o re a b o u t t h e sy n ta x o f I f . Th e n te s t s in M o d u le 6,
“ U s in g D e c i s io n S t r u c tu re s. ”)
<VISUAL BASIC.NET
ONLINE COURSE>

T h e Ba s ic M a t h p r o g ra m ru n s in t h e I D E. Th e p ro g ra m d is p la y s t wo
t e x t b o xe s in wh ic h y o u e n te r n u me ric v a l u e s , a g ro u p o f o p e ra t o r
r a d io b u t to n s , a b o x th a t d i sp la y s re s u l t s , a n d t wo b u t to n o b j e c t s
( C a lc u la te a n d Q u i t ).

S t e p 7: O n t h e d e si gn e r w in d o w d o u b le -c lic k th e b u t t o n 2 a n d t h e n o n
t h e c o d e e d i t o r e n te r th e s ta te me n t … E n d c o d e … b e t we e n th e
B u t to n 1 _ c l ic k e v e n t p r o c e d u r e .
S t e p 8: R u n t h e p r o g ra m b y c l ic k in g t h e d e b u g g in g b u t to n c o mma n d .
S t e p 9: E n te r 2 0 2 0 i n th e fi r s t t e x t b o x, a n d th e n p re s s TA B. Th e
i n se r ti o n p o in t , o r fo c u s , mo v e s to th e se c o n d t e x t b o x.
S t e p 10 : T y p e 2 0 in th e se c o n d te x t b o x. Y o u c a n n o w a p p ly a n y o f th e
m a t h e m a t ic a l o p e ra to r s t o th e v a lu e s in th e t e x t b o x e s.
S t e p 11 : C l ic k th e A d d i ti o n ra d io b u t to n , a n d th e n c lic k th e Ca lc u la te
b u tt o n . T h e o p e ra to r i s a p p li e d t o th e t wo v a lu e s , a n d th e
n u m b e r 2 0 4 0 a p p e a r s in th e Re su l t b o x , a s sh o w n i n th e
f o ll o w in g d i sp la y .

S t e p 12 : P r a c tic e u s in g th e s u b t ra c t io n , m u l t ip lic a t io n , a n d d iv i si o n
o p e ra to r s w it h th e tw o n u mb e rs i n th e v a ria b l e b o x e s. (C l ic k
C a l c u la t e to c a lc u la t e e a c h fo r mu la . )

T h e r e su l t s a p p e a r i n th e R e s u l t b o x. Fe e l f re e to e xp e r im e n t
w i th d i f f e r e n t n u m b e r s in th e v a ria b le te x t b o xe s. ( T ry a f e w
<VISUAL BASIC.NET
ONLINE COURSE>

n u m b e r s w i th d e c im a l p o in ts i f y o u li k e . ) I u se d th e D o u b le d a ta
t y p e to d e c la re th e v a ria b le s , so y o u c a n u se v e ry la rg e n u mb e r s .

N o w t ry t h e fo l lo win g te s t to se e wh a t h a p p e n s:

S t e p 13 : T y p e 1 0 0 in th e V a r ia b le 1 te x t b o x , ty p e 0 in th e V a ria b l e 2 t e x t
b o x, c l ic k th e D iv i s io n ra d io b u tt o n , a n d t h e n c l ic k Ca lc u l a te .

D iv id in g b y z e ro i s n o t a l lo we d i n ma t h e ma t ic a l c a lc u la tio n s ,
b e c a u se i t p ro d u c e s a n i n fin i t e re s u l t. V i su a l Ba si c i s a b le to
h a n d l e th i s c a lc u la ti o n a n d d i sp la y s a v a lu e o f I n fi n i ty in th e
R e su l t te x t b o x. Be in g a b le to h a n d le so me d iv id e - b y -z e ro
c o n d i ti o n s is a fe a t u re th a t V i su a l Ba s ic 2 0 1 0 a u to ma tic a lly
p r o v i d e s.

S t e p 14 : W h e n y o u ’v e f in i sh e d c o n te m p la t in g th i s a n d o th e r te s ts , c lic k
t h e Q u i t b u t to n . Th e p ro g ra m s to p s , a n d t h e d e v e lo p me n t
e n v i r o n m e n t re t u rn s

Y o u ’ r e d o n e u sin g th e Ba sic M a t h p r o g ra m. G o o d Jo b ! . .

N o w le t s d o so m e . .
S h o r tc u t Op e ra t o rs … e xe rc ise …

A n in t e re s ti n g f e a tu re o f V i su a l Ba sic i s t h a t y o u c a n u se sh o r tc u t
o p e ra to r s f o r m a t h e m a t ic a l a n d s t rin g o p e ra t io n s th a t in v o lv e
c h a n g in g th e v a l u e o f a n e xis t i n g v a r ia b le . F o r e xa mp l e , i f y o u
c o m b in e th e + sy m b o l wi t h th e = s y mb o l , y o u c a n a d d t o a v a r ia b le
w i th o u t r e p e a t in g th e v a ria b le n a m e tw ic e i n th e fo rm u l a . Th u s , y o u
c a n w r i te t h e fo r m u la X = X + 6 b y u si n g t h e sy n ta x X + = 6 . Ta b le 5 - 3
s h o w s e xa m p le s o f th e s e sh o r tc u t o p e ra to r s.

T A B L E 5 - 3 Sh o r tc u t Op e ra to rs
O p e ra t io n . L o n g - Fo rm S y n ta x . S h o r tc u t Sy n ta x .
<VISUAL BASIC.NET
ONLINE COURSE>

Addition (+) . X=X+6. X += 6.


Subtraction (–). X = X – 6. X -= 6.
Multiplication (*) . X = X * 6. X *= 6.
Division (/) . X = X / 6. X /= 6.
Integer division (\). X=X\6. X \= 6.
Exponentiation (^) . X=X^6. X ^= 6.
String concatenation (&) . X = X & “ABC” . X &= “ABC”.

U s in g A d v a n c e d O p e ra to r s: \ , M o d , ^ , a n d & . .

I n a d d i ti o n t o th e fo u r b a s ic a r i th m e tic o p e ra to r s , V i su a l Ba s ic
i n c lu d e s f o u r a d v a n c e d o p e ra to r s , wh ic h p e r fo r m in te ge r d iv i si o n ( \ ) ,
r e m a i n d e r d iv i si o n (M o d ) , e x p o n e n t ia ti o n ( ^ ) , a n d s t r in g
c o n c a te n a t io n (& ). Th e s e o p e ra t o rs a re u s e fu l i n sp e c ia l - p u r p o se
m a t h e m a t ic a l fo r m u la s a n d t e x t p ro c e s s in g a p p li c a ti o n s. Th e
f o ll o w in g u t il i ty (a s li gh t m o d i fic a t io n o f t h e Ba si c M a th p r o g ra m )
s h o w s h o w y o u c a n u se e a c h o f th e s e o p e ra to r s in a p r o g r a m.

W o rk w i th A d v a n c e d Op e r a to r s . .

S t e p 1: O p e n th e V i su a l S t u d io , c li c k Ne w P ro j e c t
S t e p 2: C h a n ge t h e n a m e o f t h e p r o j e c t to A d v a n c e d M a th P ro g ra m

S t e p 3: C r e a te a n in te r fa c e , a d d th e f o l lo wi n g o b j e c t to th e F o rm 1 th e n
c h a n ge th e p ro p e r ti e s :
S t e p 4:
Object. P r o p e r t ie s . S e t ti n g / s .
Form1. Text. “Advanced Math Operator” .
Label1. Text. “Enter first Data” .
Label2. Text. “En ter second Data” .
Label3. Text. “Result”.
GroupBox. Text. “Operator”.
RadioButton1. Text. “Integer Division [\]”.
RadioButton2. Text. “Remainder [MOD]” .
RadioButton3. Text. “Exponentiation [^]” .
<VISUAL BASIC.NET
ONLINE COURSE>

RadioButton4. Text. “Concatenation” .


TextBox1. Text. None.
TextAlignv Right.
TextBox2. Text. None.
TextAlign. Right.
TextBox3. Text. None.
TextAlign. Right.
Button1. Text. “CALCULATE” .
Button1. Text. “EXIT” .

N o te : a ll t e xt fo n t p ro p e r ty i s 1 2 -p o in t s iz e a n d Bu t t o n C a l c u la t e
a ll c a p s 1 8 -p o i n t siz e .

T h e p ro gr a m in te r fa c e sh o u ld b e s im i la r to t h i s:

S t e p 5: D o u b le -c l ic k t h e Fo r m 1 to v ie w c o d e e d i to r

S t e p 6: D e c la r e t wo v a r ia b le s o f ty p e D o u b le a t th e to p th e f o rm ’ s c o d e :

' D e c la r e Fi r s t Nu m a n d S e c o n d Nu m v a r ia b l e s
<VISUAL BASIC.NET
ONLINE COURSE>

D i m Fi r s t Nu m , Se c o n d N u m A s Do u b l e

S t e p 7: R e tu rn to th e d e si gn wi n d o w th e n c lic k th e Ca lc u la t e b u t t o n
e n te r th e fo ll o w in g c o d e a t t h e B u t to n 1 _ C l ic k e v e n t p ro c e d u re :

' A s si gn t e x t b o x v a l u e s to v a r ia b l e s .
F i r s t Nu m = Te x t Bo x 1. Te x t .
S e c o n d Nu m = Te x t Bo x 2. Te x t .

' D e te rm in e c h e c k e d b u tt o n a n d c a lc u la te .
I f Ra d io Bu t to n 1. Ch e c k e d = Tr u e T h e n .
T e x tB o x3 . T e x t = F i rs t Nu m \ S e c o n d Nu m .
E n d I f.
I f Ra d io Bu t to n 2. Ch e c k e d = Tr u e T h e n .
T e x tB o x3 . T e x t = F i rs t Nu m M o d Se c o n d Nu m .
E n d I f.
I f Ra d io Bu t to n 3. Ch e c k e d = Tr u e T h e n .
T e x tB o x3 . T e x t = F i rs t Nu m ^ S e c o n d Nu m .
E n d I f.
I f Ra d io Bu t to n 4. Ch e c k e d = Tr u e T h e n .
T e x tB o x3 . T e x t = F i rs t Nu m & S e c o n d Nu m .
E n d I f.

S t e p 8: D o u b le -c l ic k o n t h e b u t to n 2 a n d th e n e n te r th e s ta te me n t . . En d .
o n th e c o d e e d it o r.
S t e p 9: C l ic k th e S ta r t De b u g gi n g b u t t o n o n th e S ta n d a rd t o o lb a r .

T h e p ro gr a m d i sp la y s t wo t e x t b o xe s in wh ic h y o u e n te r n u me r ic
v a lu e s , a g r o u p o f o p e ra to r r a d io b u t to n s , a te x t b o x t h a t
d i sp la y s re su l t s , a n d tw o b u t t o n s .

S t e p 10 : T y p e 9 in th e f ir s t d a ta te x t b o x, a n d th e n p re s s TA B.
S t e p 11 : T y p e 2 in th e se c o n d d a ta t e x t b o x. Yo u c a n n o w a p p ly a n y o f t h e
a d v a n c e d o p e ra to r s t o th e v a lu e s in th e t e xt b o x e s.
<VISUAL BASIC.NET
ONLINE COURSE>

S t e p 12 : C l ic k th e I n te ge r Di v i s io n ra d io b u t to n , a n d th e n c lic k th e
C a l c u la t e b u t t o n .

T h e o p e ra t o r i s a p p l ie d t o th e tw o v a lu e s , a n d th e n u mb e r 4
a p p e a r s in th e Re su l t b o x , a s sh o w n h e r e :

I n t e ge r d iv i s io n p ro d u c e s o n l y th e w h o le n u m b e r re s u l t o f th e d iv i si o n
o p e ra ti o n . A l th o u gh 9 d iv id e d b y 2 e q u a l s 4 . 5, th e i n t e g e r d iv i si o n
o p e ra ti o n r e tu rn s o n ly th e fir s t p a r t , a n in te ge r ( th e w h o l e n u mb e r
4 ) . Yo u m ig h t fi n d th i s r e su lt u s e fu l i f y o u ’ r e w o rk in g w it h q u a n t i ti e s
t h a t c a n ’ t e a s i ly b e d iv id e d in t o f ra c ti o n a l c o mp o n e n t s , s u c h a s th e
n u m b e r o f a d u l ts w h o c a n fit in a c a r.

S t e p 13 : C l ic k th e Re m a i n d e r ra d i o b u t t o n , a n d th e n c li c k th e Ca lc u la te
b u tt o n .

T h e n u m b e r 1 a p p e a rs i n th e Re su l t b o x. Re ma in d e r d iv i si o n ( mo d u l u s
a ri t h m e t ic ) re t u r n s th e re m a in d e r ( t h e p a rt l e f t o v e r) a ft e r t wo
n u m b e r s a re d iv id e d . Be c a u se 9 d iv i d e d b y 2 e q u a l s 4 w i th a
r e m a i n d e r o f 1 ( 2 * 4 + 1 = 9) , th e re su l t p r o d u c e d b y th e M o d
o p e ra to r i s 1. I n a d d i ti o n t o a d d in g a n e a r ly - 1 9 7 0 s v ib e to y o u r c o d e ,
t h e M o d o p e ra t o r c a n h e lp y o u t ra c k “ le f t o v e r s ” i n y o u r c a lc u la ti o n s ,
s u c h a s th e a m o u n t o f m o n e y le f t o v e r a ft e r a f in a n c ia l tr a n sa c t io n .

S t e p 14 : C l ic k th e E xp o n e n tia t io n ra d i o b u t to n , a n d th e n c l ic k th e
C a l c u la t e b u t t o n .
<VISUAL BASIC.NET
ONLINE COURSE>

T h e n u m b e r 8 1 a p p e a r s i n th e R e su l t b o x. T h e e x p o n e n ti a ti o n
o p e ra to r ( ^ ) ra i se s a n u mb e r t o a sp e c i f ie d p o we r. F o r e x a m p le ,
9 ^ 2 e q u a l s 9 2 , o r 8 1. I n a V i su a l Ba s ic f o rmu la , 9 2 i s wr i t t e n 9 ^
2.

S t e p 15 : C l ic k th e C o n c a t e n a ti o n ra d io b u t t o n , a n d th e n c li c k th e
C a l c u la t e b u t t o n . T h e n u mb e r 9 2 a p p e a r s i n th e R e su l t b o x. Th e
s t r i n g c o n c a t e n a ti o n o p e ra to r ( & ) c o mb i n e s t wo s t r in g s i n a
f o r m u la , b u t n o t th ro u gh a d d i ti o n . Th e re su l t i s a c o m b in a t io n o f
t h e “ 9” c h a ra c te r a n d th e “ 2” c h a ra c te r. S t ri n g c o n c a t e n a t io n
c a n b e p e r fo r m e d o n n u me ric v a r ia b le s — fo r e xa mp le , i f y o u ’ re
d i sp la y i n g t h e in n in g -b y - in n in g sc o r e o f a b a se b a l l ga me a s th e y
d o in o ld - t im e sc o r e b o x e s —b u t c o n c a te n a t io n i s mo r e c o mm o n ly
p e rf o rm e d o n s t r i n g v a lu e s o r v a ria b le s .

B e c a u s e I d e c la re d t h e F ir s tN u m a n d S e c o n d N u m v a r ia b le s a s
t y p e Do u b le , y o u c a n ’ t c o mb i n e wo r d s o r le t t e r s b y u sin g th e
p r o g ra m c o d e a s w ri t t e n . A s a n e xa mp l e , t ry t h e f o l lo win g te s t ,
w h ic h c a u s e s a n e r ro r a n d e n d s th e p ro g ra m.

S t e p 16 : T y p e R e d in t h e f i rs t d a ta 1 te x t b o x , ty p e M i ra b e l in th e s e c o n d
d a ta te x t b o x , v e r i fy th a t C o n c a t e n a t io n i s se le c t e d , a n d t h e n
c lic k Ca lc u la t e .

V i su a l Ba s ic i s u n a b le t o p ro c e s s t h e t e x t v a l u e s y o u e n te r e d , so
t h e p ro g ra m s t o p s r u n n in g , a n d a n e r ro r m e s sa g e a p p e a r s o n th e
s c re e n .
<VISUAL BASIC.NET
ONLINE COURSE>

T h i s ty p e o f e rr o r i s c a lle d a r u n - ti me e rr o r —a n e r r o r th a t su r fa c e s
n o t d u r in g th e d e s ig n a n d c o m p i la ti o n o f th e p ro g ra m b u t la te r , wh e n
t h e p ro g ra m i s r u n n in g a n d e n c o u n te r s a c o n d i t io n th a t i t d o e sn ’ t
k n o w h o w t o p r o c e s s. I f th is s e e m s o d d , y o u m ig h t i ma g in e th a t V i su a l
B a s ic i s s im p ly o f fe ri n g y o u a mo d e rn r e n d i ti o n o f th e ro b o t p l e a
“ D o e s n o t c o m p u te ! ” f r o m th e b e s t sc i e n c e - f ic ti o n f il m s o f th e 1 9 5 0 s.
T h e c o m p u te r - sp e a k m e s sa ge “ C o n v e r s io n f ro m s tr in g ‘ Re d ’ to ty p e
‘ D o u b le ’ i s n o t v a l id ” m e a n s t h a t th e w o rd s y o u e n te re d in t h e t e x t
b o xe s ( “Re d ” a n d “ M i ra b e l” ) c o u ld n o t b e c o n v e r t e d , o r c a s t , b y
V i su a l Ba s ic t o v a r ia b le s o f th e ty p e Do u b le . D o u b le ty p e s c a n c o n ta i n
o n ly n u m b e r s —p e rio d . A s we sh a l l e x p l o re in mo re d e ta i l la te r , V i su a l
S t u d io d o e s n ’ t l e a v e y o u h a n gi n g wi t h su c h a p r o b le m , b u t p ro v id e s a
d ia l o g b o x w i th d i f fe re n t ty p e s o f in f o r ma ti o n t o h e l p y o u re s o lv e th e
r u n - t im e e r r o r . Fo r n o w , y o u h a v e l e a rn e d a n o t h e r imp o r ta n t l e ss o n
a b o u t d a ta ty p e s a n d w h e n n o t to m ix th e m.

S t e p 17 : C l ic k th e S t o p De b u g gin g b u t t o n o n th e S ta n d a rd t o o lb a r t o e n d
t h e p ro g ra m . Y o u r p r o g ra m e n d s a n d re tu r n s y o u to th e
d e v e lo p m e n t e n v i ro n me n t .

N o w ta k e a lo o k a t th e p ro gra m c o d e t o se e h o w v a r ia b le s we re
d e c la r e d a n d h o w th e a d v a n c e d o p e ra to r s we r e u se d .

S t e p 18 : S c ro ll t o th e c o d e a t th e to p o f th e Co d e Ed i t o r , if i t i s n o t
c u r r e n tly v i s ib le .

Y o u se e t h e f o l lo wi n g c o m me n t a n d p ro g ra m s ta t e me n t:

' D e c la r e Fi r s t Nu m a n d S e c o n d Nu m v a r ia b l e s .
D i m Fi r s t Nu m , Se c o n d N u m A s Do u b l e .

A s y o u m i gh t r e c a l l f ro m th e p re v io u s e xe rc i s e , Fi r s t Nu m a n d
S e c o n d Nu m a re t h e v a r ia b le s th a t h o ld n u mb e r s c o m in g i n f ro m th e
T e x tB o x1 a n d T e x t Bo x 2 o b j e c t s.
<VISUAL BASIC.NET
ONLINE COURSE>

S t e p 19 : C h a n ge t h e d a ta ty p e f ro m D o u b le to S t r in g s o th a t y o u c a n
p r o p e r ly te s t h o w th e s t ri n g c o n c a t e n a ti o n (& ) o p e ra t o r w o rk s.

' D e c la r e Fi r s t Nu m a n d S e c o n d Nu m v a r ia b l e s .
D i m Fi r s t Nu m , Se c o n d N u m A s S t ri n g .

L i k e t h e Ba sic M a th p ro g ra m, t h i s p r o g ra m lo a d s d a ta f ro m th e te x t
b o xe s a n d p la c e s i t in t h e F ir s t N u m a n d Se c o n d Nu m v a r ia b le s. Th e
p r o g ra m th e n c h e c k s t o se e w h ic h ra d io b u t to n th e u se r c h e c k e d a n d
c o m p u t e s th e re q u e s te d fo rm u la . I n t h i s e v e n t p ro c e d u re , th e in t e ge r
d iv is io n ( \ ) , re m a in d e r ( M o d ) , e xp o n e n tia t io n (^ ) , a n d s tr i n g
c o n c a te n a t io n (& ) o p e ra to r s a r e u se d . No w th a t y o u ’v e c h a n ge d th e
d a ta ty p e o f t h e v a r ia b le s t o S t ri n g , r u n th e p r o g ra m a ga i n t o se e h o w
t h e & o p e ra t o r wo rk s o n t e xt . 1 6 . Cl ic k th e S ta r t De b u g gi n g b u t to n .

S t e p 20 : T y p e R e d in t h e f i rs t d a ta t e x t b o x , ty p e M i ra b e l in t h e se c o n d
d a ta te x t b o x , c l ic k

C o n c a te n a t io n , a n d th e n c l ic k C a lc u la te . T h e p ro g ra m n o w
c o n c a te n a t e s th e s t rin g v a lu e s a n d d o e s n ’ t p ro d u c e a ru n - t ime e r ro r ,
a s sh o w n h e r e :

S t e p 21 : C l ic k th e Qu i t b u t to n to c l o se th e p ro gr a m.
<VISUAL BASIC.NET
ONLINE COURSE>

A s y o u c a n se e , th e St r in g d a ta ty p e h a s fi xe d th e c o n c a t e n a ti o n
p r o b l e m . H o we v e r , i t i s n o t a t o ta l s o l u ti o n b e c a u se v a ria b le s o f
t y p e S t rin g wi ll n o t fu n c t io n c o r re c t ly i f y o u t ry t h e I n te g e r
D iv i si o n , Re m a in d e r , o r E x p o n e n t ia ti o n o p e ra t io n s w i th t h e m.
S o , i f y o u r e a l ly wa n te d to h a v e y o u r p r o g ra m p ro c e ss n u mb e r s
a n d te x t s t r in g s in t e rc h a n ge a b ly , y o u ’d n e e d t o a d d so me
a d d i t io n a l p r o g ra m lo g ic t o y o u r c o d e . F o r n o w , h o we v e r , y o u ’ re
f i n i sh e d wo r k in g wi th th e A d v a n c e d M a th p ro g ra m.

R u n - t im e e r ro r s a r e d i ff ic u l t t o a v o id c o mp le te ly —e v e n th e m o s t
s o p h i s t ic a te d a p p l ic a ti o n p ro g ra m s , s u c h a s W o rd o r M ic ro s o f t
O f f ic e Exc e l , so m e ti m e s ru n i n to e r ro r c o n d i ti o n s t h a t th e y c a n ’ t
h a n d l e , p r o d u c in g ru n - t im e e r r o r s , o r c ra sh e s. De si gn in g y o u r
p r o g ra m s to h a n d le m a n y d if f e re n t d a ta t y p e s a n d o p e ra t in g
c o n d i ti o n s h e lp s y o u p ro d u c e so lid , o r r o b u s t , a p p l ic a t io n s. I n
M o d u le 9, “T ra p p i n g E r ro r s b y U s in g S t ru c tu re d E r r o r H a n d l in g , ”
y o u ’l l le a rn a b o u t a n o th e r h e lp fu l t o o l fo r p re v e n tin g ru n - t ime
e r r o r c ra sh e s — th e s t ru c tu re d e r ro r h a n d le r.

W o rk in g wi th M a th M e t h o d s in t h e . N E T Fra me wo rk . .

N o w a n d th e n y o u ’l l wa n t t o d o a l i t tl e e x t ra n u m b e r c ru n c h in g in
y o u r p r o g ra m s . Y o u m i gh t n e e d t o ro u n d a n u mb e r , c a lc u la te a
c o m p le x m a th e m a tic a l e x p re s s i o n , o r in t ro d u c e ra n d o mn e s s in t o
y o u r p r o g ra m s . T h e m a th me t h o d s s h o wn in Ta b le 5 - 4 c a n h e lp y o u
w o rk w i th n u m b e r s i n y o u r fo r mu la s . Th e se me t h o d s a re p ro v id e d
b y th e S y s t e m . M a th c la s s o f t h e . NE T F ra m e w o rk , a c la s s li b ra ry
t h a t le t s y o u ta p in t o th e p o w e r o f th e W in d o w s o p e ra tin g sy s te m
a n d a c c o m p li s h m a n y o f th e c o mm o n p ro g ra mm in g ta sk s t h a t y o u
n e e d t o c r e a te y o u r p r o j e c ts. Th e a rg u me n t n in th e ta b le
r e p r e se n t s t h e n u m b e r , v a r ia b le , o r e x p r e s si o n th a t y o u wa n t t h e
m e th o d to e v a lu a te .
<VISUAL BASIC.NET
ONLINE COURSE>

T A B L E 5 - 4 U se fu l M a t h M e th o d s
M e th o d P u rp o s e
Abs(n) Returns the absolute value of n.
Atan(n) Returns the arctangent, in radians, of n.
Cos(n) Returns the cosine of the angle n. The angle n is expressed in radians.
Exp(n) Returns the constant e raised to the power n.
Sign(n) Returns –1 if n is less than 0, 0 if n equals 0, and +1 if n is greater than 0.
Sin(n) Returns the sine of the angle n. The angle n is expressed in radians.
Sqrt(n) Returns the square root of n.
Tan(n) Returns the tangent of the angle n. The angle n is expressed in radians.

N o te T h i s i s o n l y a p a r t ia l l is t i n g o f th e m e th o d s i n th e
S y s te m . M a th c la s s; t h e re a re ma n y m o re c la s se s in t h e . N E T
F r a m e wo r k th a t W in d o w s a p p li c a t io n s c a n u se .

T o u se o n e o r m o re o f th e se me th o d s , p u t th e s ta te m e n t

I m p o r t s Sy s te m . M a th .

a t t h e t o p o f y o u r fo rm ’ s c o d e i n th e Co d e Ed i to r. Th i s st a te me n t
r e fe r e n c e s th e Sy s te m . M a t h c la s s so th a t y o u c a n u s e i ts me th o d s
i n y o u r p r o g ra m .

W h a t i s th e p u r p o se o f t h e . N E T F ra m e w o rk , a n y wa y ? T h e . NE T
F r a m e wo r k i s a m a j o r f e a t u re o f V i su a l S tu d io th a t i s sh a r e d b y
V i su a l Ba s ic , M ic r o so f t V i su a l C+ + , M ic ro so f t V i su a l C # , M ic ro s o f t
F # , a n d o th e r t o o l s in V i su a l S tu d io . I t ’ s a n u n d e rl y i n g in t e r fa c e
t h a t b e c o m e s p a r t o f th e W in d o ws o p e ra tin g sy s te m i t se l f , a n d i t i s
i n s ta l le d o n e a c h c o m p u t e r t h a t r u n s V i su a l S tu d io p ro gr a m s. T h e
k e y c o m p o n e n ts i n th e . NE T F r a me wo r k a re t h e c o m mo n l a n g u a ge
r u n t im e ( C LR ) a n d th e . NE T F r a me wo r k c la s s l ib ra ry , wh ic h i n c lu d e s
A DO . N E T , A SP. N ET , W in d o w s Fo r m s , a n d Win d o w s Pr e se n t a ti o n
F o u n d a ti o n ( W PF ). W i th e a c h v e r si o n o f V i su a l St u d io , th e . NE T
F r a m e wo r k i s e x te n d e d t o p ro v id e a d d i t io n a l fu n c ti o n a li ty . I n
<VISUAL BASIC.NET
ONLINE COURSE>

V i su a l St u d io 2 0 1 0 , t h e . N E T F ra m e w o rk 4 l ib ra ry i s b e in g
i n t ro d u c e d , wh ic h o f fe r s a n u p d a te t o th e . N ET F ra me w o r k 3. 5
l ib ra ry a n d o f fe r s m o re d e p lo y me n t o p ti o n s , su p p o r t f o r p a ra l le l
c o m p u t in g (m u l t it h re a d e d a n d a sy n c h ro n o u s c o d e ) , im p r o v e d
s e c u r i ty , n e t wo rk in g e n h a n c e me n t s , a n d n e w We b s e rv ic e s s u p p li e d
t h r o u g h A SP. N ET .

M a n y o f th e i m p ro v e m e n t s in th e . N E T F ra me wo rk w il l c o me t o y o u
a u t o m a t ic a l ly a s y o u u se V i su a l Ba sic 2 0 1 0 , a n d so m e wil l b e c o me
u se f u l a s y o u e xp lo r e a d v a n c e d p r o g ra m mi n g t e c h n iq u e s . S ta r ti n g
n o w a n d c o n t in u in g th ro u gh o u t th i s c o u r se , I ’ ll te a c h y o u h o w t o
u se se v e ra l m e th o d s in t h e . N E T F ra m e w o rk t o e n h a n c e y o u r V i su a l
B a s ic p ro gra m s. A f te r y o u fin i sh wi t h th i s c o u r se , y o u ma y wa n t to
s e e k o u t a d d i t io n a l b o o k s a n d re so u rc e s a b o u t t h e . N E T F ra m e w o rk
b e c a u se i t o f fe r s a n i m p o r ta n t e x te n s io n to wh a t y o u c a n d o w i th
V i su a l Ba s ic a n d th e o t h e r la n gu a g e s in V i su a l S tu d io .

G iv e th e m a t h m e th o d s i n th e . NE T F ra me wo r k a t ry n o w b y
c o m p le t in g th e f o l lo wi n g e xe rc i se .

U s e th e S y st e m . M a th c la s s to c o mp u t e sq u a r e ro o ts . .

S t e p 1: O n t h e F ile m e n u , c l ic k Ne w P r o j e c t. Th e N e w P r o j e c t d ia l o g b o x
o p e n s.
S t e p 2: C r e a te a n e w V i su a l Ba si c Wi n d o w s Fo r m s A p p l ic a ti o n p ro j e c t
n a m e d M y F ra m e w o rk M a th . Th e n e w p ro j e c t i s c re a te d , a n d a
b la n k fo rm o p e n s in t h e De si gn e r.
S t e p 3: C l ic k th e Bu t t o n c o n t ro l o n th e Wi n d o w s Fo rm s ta b o f th e
T o o lb o x, a n d th e n c re a t e a b u tt o n o b j e c t a t t h e t o p o f y o u r
form.
S t e p 4: C l ic k th e T e x t Bo x c o n t ro l in t h e T o o lb o x , a n d th e n d ra w a t e x t
b o x b e lo w t h e b u t to n o b j e c t.
S t e p 5: S e t th e Te x t p r o p e r ty o f t h e b u tt o n o b j e c t to S q u a re R o o t .
S t e p 6: D o u b le -c l ic k t h e b u t to n o b j e c t t o d i sp la y th e Co d e Ed i to r.
S t e p 7: A t t h e v e ry to p o f th e C o d e E d it o r , a b o v e th e Pu b l ic C la s s Fo rm 1
s t a te m e n t , t y p e t h e f o l lo wi n g p ro g ra m s ta t e me n t:
<VISUAL BASIC.NET
ONLINE COURSE>

I m p o r t s Sy s te m . M a th

T h e Sy s t e m . M a t h c la s s i s a c o l le c ti o n o f me th o d s p r o v i d e d b y th e
. N ET F ra m e w o rk f o r a ri t h me t i c o p e ra ti o n s. Th e . N ET F ra m e w o rk i s
o r ga n iz e d i n a h ie ra rc h ic a l fa sh io n a n d c a n b e v e ry d e e p . Th e
I m p o r t s s ta te m e n t m a k e s i t e a s ie r t o re fe r e n c e c la s s e s , p r o p e r t ie s ,
a n d m e th o d s i n y o u r p r o j e c t. F o r e xa m p l e , i f y o u d id n ’ t in c lu d e t h e
p r e v i o u s I m p o r t s s ta t e m e n t , t o c a ll th e Sq r t me t h o d y o u wo u ld
h a v e to ty p e S y s te m . M a th . Sq r t in s te a d o f j u s t Sq r t. T h e I mp o r t s
s t a te m e n t m u s t b e th e f ir s t s t a te me n t in y o u r p ro g ra m — i t mu s t
c o m e e v e n b e f o re t h e v a r ia b l e s th a t y o u d e c la re fo r th e f o r m a n d
t h e Pu b lic C la s s Fo r m 1 s ta te m e n t t h a t V is u a l Ba s ic a u t o ma t ic a lly
p r o v i d e s.
S t e p 8: M o v e d o wn in t h e C o d e E d i to r , a n d t h e n a d d th e f o l lo win g c o d e
t o th e Bu t t o n 1_ C lic k e v e n t p r o c e d u r e b e tw e e n t h e P r iv a te S u b
a n d En d Su b s ta te m e n t s:

D i m R e su l t A s D o u b le .
R e su l t = Sq r t ( 6 2 5 ) .
T e x tB o x1 . T e x t = Re su l t .

T h e se th re e s ta te m e n t s d e c la re a v a r ia b l e o f th e d o u b l e t y p e n a me d
R e su l t, u se th e Sq r t m e t h o d t o c o mp u te th e sq u a re ro o t o f 6 2 5 , a n d
a s si gn t h e Re su l t v a r ia b le t o t h e Te x t p r o p e r ty o f t h e t e xt b o x o b j e c t
s o th a t th e a n s we r i s d i sp la y e d .
S t e p 9: C l ic k th e Sa v e A ll b u t to n o n t h e St a n d a rd to o lb a r t o sa v e y o u r
c h a n ge s . Sp e c i fy y o u r f o ld e r a s th e lo c a t io n .
S t e p 10 : C lic k th e S ta r t D e b u gg in g b u t t o n o n th e S ta n d a rd to o lb a r. Th e
F r a m e wo r k M a th p ro g ra m r u n s in th e I D E.
S t e p 11 : C lic k th e Sq u a re R o o t b u t to n . V is u a l Ba s ic c a lc u la te s th e sq u a re
r o o t o f 6 2 5 a n d d is p la y s th e r e s u l t ( 2 5 ) in t h e t e x t b o x. A s y o u
c a n se e h e re , th e S q r t me t h o d wo rk s!
S t e p 12 : C l ic k th e C lo s e b u t t o n o n t h e fo rm to e n d t h e p ro g ra m.
<VISUAL BASIC.NET
ONLINE COURSE>

T o m a k e i t e a si e r to re fe re n c e c la s s e s , p ro p e r ti e s, a n d m e th o d s i n
t h e . NE T F ra m e w o rk , in c lu d e th e I mp o r t s s ta te m e n t a n d sp e c i f y th e
a p p r o p r ia te n a m e sp a c e o r c la s s. Yo u c a n u se t h i s t e c h n iq u e t o u se
a n y c l a s s in th e . N E T F ra me w o rk .

C o n g ra tu la ti o n ! Y o u r d o n e w i th M o d u le 5 V i su a l Ba sic V a r ia b le s a n d
F o rm u la s , a n d t h e . N ET F ra m e w o rk . Yo u c a n c o n ti n u e o n O n e S te p
F u r th e r : Es ta b l i sh in g O r d e r o f P re c e d e n c e a s y o u r se l f - p a c e
l e a rn in g …
<VISUAL BASIC.NET
ONLINE COURSE>

O n e S t e p Fu r th e r: E s ta b li sh in g O rd e r o f P re c e d e n c e . .

I n th e p re v io u s fe w e xe rc i se s , y o u e xp e r ime n te d w i th se v e ra l
a ri t h m e t ic o p e ra t o r s a n d o n e st r in g o p e ra to r. V i s u a l Ba si c le t s y o u
m i x a s m a n y a ri th m e t ic o p e ra to r s a s y o u l ik e in a f o rmu la , s o lo n g
a s e a c h n u m e ric v a ria b le a n d e xp re s s io n i s s e p a ra te d fro m a n o th e r
b y o n e o p e ra to r . Fo r e xa mp le , th i s i s a n a c c e p ta b le V i su a l B a s ic
f o r m u la :

T o ta l = 1 0 + 1 5 * 2 / 4 ^ 2 .

T h e f o rm u la p ro c e s se s se v e ra l v a lu e s a n d a s s ign s th e r e su l t to a
v a r ia b l e n a m e d T o ta l . B u t h o w i s su c h a n e xp re s s io n e v a lu a te d b y
V i su a l Ba s ic ? I n o t h e r wo rd s, wh a t s e q u e n c e d o e s V is u a l Ba sic
f o ll o w w h e n s o lv in g th e fo rm u la ? Y o u mi gh t n o t h a v e n o t ic e d , b u t
t h e o rd e r o f e v a lu a ti o n ma t te r s a g re a t d e a l in t h i s e xa mp le .

V i su a l Ba s ic s o lv e s th i s d il e m ma b y e s ta b l is h i n g a sp e c if i c o rd e r o f
p r e c e d e n c e f o r m a t h e m a t ic a l o p e ra ti o n s. Th i s li s t o f ru le s te l ls
V i su a l Ba s ic wh ic h o p e ra t o r t o u se f i r s t , se c o n d , a n d s o o n wh e n
e v a lu a t in g a n e xp re s s io n t h a t c o n ta in s mo re t h a n o n e o p e ra t o r.
T a b l e 5 - 5 li s t s t h e o p e ra t o rs f r o m f i rs t to la s t in t h e o rd e r in w h ic h
t h e y a re e v a lu a te d . ( Op e ra to r s o n t h e sa me le v e l in th is ta b le a re
e v a lu a t e d f ro m le f t t o ri gh t a s th e y a p p e a r in a n e x p re s si o n . )

T A B L E 5 - 5 O rd e r o f P re c e d e n c e o f O p e ra to r s . .
Operator Order of Precedence
() Values within parentheses are always evaluated first.
^ Exponentiation (raising a number to a power) is second.
– Negation (creating a negative number) is third.
<VISUAL BASIC.NET
ONLINE COURSE>

*/ Multiplication and division are fourth.


\ Integer division is fifth.
Mod Remainder division is sixth.
+– Addition and subtraction are last.

G iv e n th e o r d e r o f p re c e d e n c e i n th i s ta b le , t h e e x p re s sio n
T o ta l = 1 0 + 1 5 * 2 / 4 ^ 2 .
i s e v a lu a te d b y V is u a l Ba s ic i n th e fo ll o w in g s te p s . (Sh a d i n g i s u se d t o
s h o w e a c h s t e p in t h e
o r d e r o f e v a lu a t io n . )

T o ta l = 10 + 15 * 2 / 4 ^ 2.
T o ta l = 10 + 15 * 2 / 16.
T o ta l = 10 + 30 / 16.
T o ta l = 1 0 + 1. 8 7 5 .
T o ta l = 1 1. 8 7 5 .

U s in g Pa re n th e se s in a F o rmu la . .

Y o u c a n u se o n e o r m o re p a ir s o f p a re n t h e se s in a fo r mu l a to c la r i fy
t h e o rd e r o f p re c e d e n c e o r im p o se y o u r o wn o rd e r o f p re c e d e n c e o v e r
t h e s ta n d a r d o n e . Fo r e xa m p le , V i su a l Ba sic c a l c u la t e s th e fo r mu la

N u m b e r = ( 8 – 5 * 3) ^ 2 .

b y d e t e rm in i n g t h e v a lu e wit h in t h e p a r e n th e se s ( – 7 ) b e f o r e d o in g
t h e e xp o n e n tia t io n — e v e n th o u gh e xp o n e n t ia t io n i s h i gh e r in o rd e r o f
p r e c e d e n c e th a n s u b t ra c ti o n a n d mu l t ip li c a t io n , a c c o rd in g to th e
p r e c e d in g ta b le . Y o u c a n fu rt h e r re fi n e t h e c a lc u la ti o n b y p la c in g
n e s t e d p a re n th e se s in t h e fo r mu la . Fo r e xa mp le ,

N u m b e r = (( 8 – 5 ) * 3 ) ^ 2 .

d i re c t s V i su a l Ba sic to c a lc u la te th e d i f fe r e n c e i n th e i n n e r s e t o f
p a r e n th e s e s fi r s t , p e r fo rm th e o p e ra ti o n in th e o u te r p a r e n t h e se s
n e x t , a n d th e n d e t e rm i n e t h e e xp o n e n t ia ti o n .
<VISUAL BASIC.NET
ONLINE COURSE>

T h e r e su l t p ro d u c e d b y th e tw o f o r mu la s is d if f e re n t: th e fi r s t fo r mu la
e v a lu a t e s to 4 9 a n d th e s e c o n d to 8 1. Pa re n th e se s c a n c h a n ge th e
r e su l t o f a m a th e m a tic a l o p e ra t io n ,
a s we ll a s m a k e i t e a s ie r t o r e a d .
<VISUAL BASIC.NET
ONLINE COURSE>

M o d u le 5 Q u ic k Re fe re n c e
To D o Th i s
Declare a variable Type Dim followed by the variable name, the
As keyword, and the variable data type in the
program code. To make the variable valid in all
a form’s event procedures, place this
statement at the top of the code for the form,
before an y even t p rocedures. For example:

Dim Country As String


Change the value of Assign a new value with the assignment
a variable operator (=).
For example:
Country = "Japan"
Get input b y u sin g Use the Inpu tBox fun ction and assign the
a dialog box result to a variable.
For example:
UserName = Inpu tBox("What is your n ame?")
Display ou tput in a dialog Use the MsgBox function. (The strin g to be
box disp layed in the d ialog box can be stored in a
variable.) For example:
Forecast = "Rain, mainly on the plain."
MsgBox(Forecast, , "Spain Weather Report")
Create a con stant Typ e th e Con st keyword followed b y th e
constant name, the
assign men t operator (=), the con stant data
type, and the fixed value.
For example:
Con st JackBenn ysAge As Short = 39
Create a formula Link together numeric variables or values with
on e of th e seven arithmetic op erators, and
then assign the result to a variable or a
property. For example:
Resu lt = 1 ^ 2 * 3 \ 4 'this equ als 0
Combin e text strin gs Use the string concatenation operator (&).
For example:
Msg = "Hello" & "," & " world!"
Make it easier to Place an Imports statemen t at the very top of
reference a class the form’s code that
library from the identifies the class library. For example:
<VISUAL BASIC.NET
ONLINE COURSE>

.NET Framework Imports System.Math

Make a call to a Use the method name, and in clude any


method from an necessary argu men t s so that it
included class can b e u sed in a formula or a p rogram
library statement. For example, to make a call to the
Sq rt method in the System.Math class:
Hypotenu se = Sqrt(x ^ 2 + y ^ 2)
Control the evaluation Use p aren theses in the formula. For examp le:
order in a formula Resu lt = 1 + 2 ^ 3 \ 4 'this equ als 3
Resu lt = (1 + 2) ^ ( 3 \ 4) 'this equals 1

You might also like