You are on page 1of 27

V IS U A L B A S IC V A R IA B L E S

K . N ith e e s h M u r r u g a n

V a r ia b le s
A s t o r a g e lo c a t io n in m e m o r y (R A M )
H o ld s d a p ro g ra m T h e se sto re fe rre d ta / in f o r m a tio n w h ile th e is r u n n in g . r a g e lo c a t i o n s c a n b e to b y th e ir n a m e s .

V a r ia b le s
E v e r y v a r ia b le h a s th r e e p r o p e r tie s :
N am e
r e f e r e n c e t o t h e lo c a t i o n - c a n n o t b e ch a n g e d

V a lu e
t h e in f o r m a t i o n t h a t is s t o r e d - c a n b e c h a n g e d d u r in g p ro g r a m e x e c u tio n , h e n c e th e n a m e v a r ia b le

D a ta Ty p e
t h e t y p e o f in f o r m a t i o n t h a t c a n b e sto re d - ca n n o t b e ch a n g e d .

V a r ia b le s
C r e a te a V a r ia b le V is u a l B a s ic a s s o c ia te s th a t n a m e w it h a lo c a t i o n in t h e c o m p u t e r 's R A M T h e v a lu e c u r r e n tly a s s o c ia te d w i t h t h e v a r ia b l e is s t o r e d in th a t m e m o r y lo c a t io n u s e th e n a m e w h e n y o u n e e d to a c c e s s th e v a lu e

Da t a T y p e
Da t a t y p e - S p e c i f i e s t y p e o f d a t a v a r ia b le c a n sto r e In t e g e r
L o n g , In t e g e r, S h o r t , B y t e

Fl o a t i n g - p o i n t v a r i a b l e s
S i n g l e , Do u b l e

B o o le a n v a r ia b le s
T r u e , Fa l s e

Da t a T y p e
Da t a t y p e - S p e c i f i e s t y p e o f d a t a v a r ia b le c a n sto r e Fi x e d d e c i m a l p o i n t v a r i a b l e
De c i m a l

C h a r a c te r v a r ia b le
Char

Te x t v a r ia b le
S tr in g

Da t a T y p e
Da t a t y p e - S p e c i f i e s t y p e o f d a t a v a r ia b le c a n sto r e T h e Ob je c t v a r i a b l e
De f a u l t d a t a t y p e a s s i g n e d b y V i s u a l B a s ic C a n s t o r e m a n y d if f e r e n t ty p e s o f d a ta L e s s e f f ic ie n t th a n o t h e r d a ta ty p e s

V i s u a l B a s i c Da t a T y p e s

V a r ia b le N a m e s
Fi r s t c h a r a c t e r m u s t b e a l e t t e r o r un d e rsco re M u s t c o n t a in o n ly le t t e r s , n u m b e r s, a n d u n d e r s c o r e s (n o sp a c e s, p e r io d s, e tc .) C a n h a v e u p t o 255 c h a r a c t e r s C a n n o t b e a V B la n g u a g e ke y w o r d

De c l a r i n g a V a r i a b l e
A v a r i a b l e d e c l a r a t i o n is a s t a t e m e n t t h a t c r e a t e s a v a r i a b l e in m e m o r y S y n ta x :
D im VariableName As DataType

Dim ( s h o r t f o r Di m e n s i o n ) - ke y w o r d V a r i a b l e N a m e - n a m e u s e d to r e f e r to v a r ia b le A s - ke y w o r d Da t a T y p e - o n e o f m a n y p o s s i b l e ke y w o r d s t o in d i c a t e th e ty p e o f v a l u e th e v a r i a b l e w i l l c o n ta in

E x a m p le :
D im in t Le n g t h a s In t e g e r

De c l a r i n g a n d I n i t i a l i zi n g a V a r i a b l e
A s t a r t i n g o r i n i t i a l i za t i o n v a l u e m a y b e s p e c i f i e d w i t h t h e Di m s t a t e m e n t Go o d p r a c t i c e t o s e t a n i n i t i a l v a l u e u n le s s a s s ig n in g a v a lu e p r io r to u s in g th e v a r ia b le S y n ta x :
D im VariableName As DataType = Value
Ju s t a p p e n d " = v a l u e t o t h e Di m s t a t e m e n t = 5 a s s ig n in g a b e g in n in g v a lu e to th e v a r ia b le

E x a m p le :
D im in t Le n g t h a s In t e g e r = 5

V a r i a b l e De c l a r a t i o n R u le s
V a r ia b le M U S T b e d e c la r e d p r io r to th e c o d e w h e r e th e y a re use d V a r ia b le sh o u ld b e d e c la r e d f ir s t in th e p r o c e d u r e (s t y le c o n v e n tio n ) De c l a r i n g a n i n i t i a l v a l u e o f t h e v a r ia b l e in t h e d e c la r a t io n s t a t e m e n t is o p t io n a l

L ite r a l
A c tu a l v a lu e / d a ta / in fo r m a tio n S im ila r to a v a r ia b le , b u t c a n N OT c h a n g e d u r i n g t h e e x e c u tio n o f a p r o g r a m . E x a m p le s o f L ite r a ls :
N u m e r i c : 5 ; 157 ; 195.38256 S t r i n g : Pa u l ; H e l l o !!! ; Ja c ks o n , A L 36545 C h a r : a ; 1 ; ? ; @ B o o l e a n : T r u e ; Fa l s e

S c o p e o f V a r ia b le s
In d ic a t e s t h e p a r t o f t h e p r o g r a m w h e r e th e v a r ia b le ca n b e use d Fr o m t h e v a r i a b l e d e c la r a tio n u n til th e e n d o f th e c o d e b lo c k (p r o c e d u r e , m e t h o d , e t c .) w h e r e it is d e c la r e d

S c o p e o f V a r ia b le s
V a r ia b l e c a n n o t b e u s e d b e f o r e it is d e c l a r e d V a r ia b le d e c la r e d w ith in a c o d e b l o c k is o n l y v i s ib le to s t a t e m e n t s w ith in th a t c o d e b lo c k

C a l l e d Lo c a l V a ria b l e

C a n b e d e c la r e d a t th e b e g in n in g o f t h e c l a s s c o d e w i n d o w ( Ge n e r a l De c l a r a t i o n s s e c t i o n ) a n d b e a v a i l a b l e t o a l l b l o c ks

C a l l e d Fo rm Le ve l V a ria b l e

V a r ia b le s th a t s h a r e th e s a m e sc o p e c a n n o t h a v e th e sa m e n a m e (s a m e n a m e o k if d if f e r e n t s c o p e )

L if e tim e o f V a r ia b le s
In d ic a t e s th e p a r t o f th e p r o g r a m w h e r e th e v a r ia b le e x is t s in m e m o r y Fr o m t h e b e g i n n i n g o f t h e c o d e b lo c k (p r o c e d u r e , m e th o d , e t c .) w h e r e it is d e c la r e d u n t il th e e n d o f th a t c o d e b lo c k

L if e tim e o f V a r ia b le s
Wh e n t h e c o d e b l o c k b e g i n s t h e s p a c e is c r e a t e d t o h o l d t h e lo c a l v a r ia b le s
M e m o r y is a l l o c a t e d f r o m t h e o p e r a t i n g sy ste m

Wh e n t h e c o d e b l o c k e n d s t h e l o c a l v a r ia b le s a r e d e s tr o y e d
M e m o r y is g i v e n b a c k t o t h e o p e r a t i n g sy ste m

A s s ig n m e n t S ta te m e n t
S y n ta x : variablename = expression A s s ig n s th e v a lu e o f th e e x p r e s s io n to th e v a r ia b le . (T h e v a r i a b l e m u s t b e o n t h e le f t a n d th e e x p r e s s io n o n th e r ig h t.) E x a m p le :
in t N in t N in t N In t N um um um um b b b b e e e e r1 r2 r3 r1 = = = = 4 3 * ( 2 + 2) in t N u m b e r 1 in t N u m b e r 1 + 6

Im p lic it T y p e C o n v e r s io n s
A v a lu e o f o n e d a t a ty p e c a n b e a s s ig n e d to a v a r ia b le o f a d if f e r e n t ty p e
A n i m p l i c i t t y p e c o n v e r s i o n is a n a t t e m p t t o a u to m a tic a lly c o n v e r t to th e re c e iv in g v a r i a b l e s d a t a t y p e

A w i d e n i n g c o n v e r s i o n s u f f e r s n o lo s s o f d a ta
C o n v e r t i n g a n in t e g e r t o a s i n g l e Dim s n g N u m b e r a s S i n g l e = 5

A n a r r o w i n g c o n v e r s i o n m a y lo s e d a t a
C o n v e r t i n g a d e c i m a l t o a n in t e g e r Dim in t C o u n t = 12.2 in t C o u n t b e c o m e s 12

E x p lic it T y p e C o n v e r s io n s
V B p r o v id e s a se t o f fu n c tio n s th a t p e r f o r m d a ta ty p e c o n v e r s io n s T h e s e f u n c t i o n s w i l l a c c e p t a li t e r a l , v a r ia b le n a m e , o r a r ith m e tic e x p r e s s io n T h e fo llo w in g n a r r o w in g c o n v e r s io n s r e qu i r e a n e x p l i c i t t y p e c o n v e r s i o n
Do u b l e t o S i n g l e S i n g l e t o In t e g e r L o n g t o In t e g e r

B o o l e a n , Da t e , Ob je c t , S t r i n g , a n d n u m e r ic ty p e s r e p r e s e n t d if f e r e n t s o r t s o f v a l u e s a n d r e qu i r e c o n v e r s io n fu n c tio n s a s w e ll

T h e V a l Fu n c t i o n
T h e Val function i s a m o r e f o r g i v i n g m e a n s o f p e r fo r m in g str in g to n u m e r ic c o n v e r s io n s U s e s th e fo r m V a l(s tr in g ) If t h e in i t i a l c h a r a c t e r s f o r m a n u m e r ic v a lu e , th e V a l fu n c tio n w ill re tu r n th a t Ot h e r w i s e , i t w i l l r e t u r n a v a l u e o f ze r o

T h e V a l Fu n c t i o n
R e t ur ns th e n u m b e r s c o nt ai n e d in a stri n g a s a n u m e ric va lue of a p p r o p ria t e ty p e V a l Fu n c t i o n V a lu e R e tu r n e d V a l ( "34. 90 ) 34.9 V a l ( "86a b c ) 86 V a l ( "$24. 95 ) 0 V a l ( "3, 789 ) 3 V a l ( " ) 0 V a l ( "x 29 ) 0 V a l ( "47% ) 47 V a l ( "Ge r a l d i n e ) 0

T h e To S t r in g M e t h o d
R e tu r n s a str in g re p r e s e n ta tio n o f t h e v a l u e in t h e v a r i a b l e c a l l i n g t h e m e th o d E v e r y V B d a t a t y p e h a s a ToString m e th o d U s e s th e fo r m V a r ia b le N a m e .To S tr in g Fo r e x a m p l e
Dim number as Integer = 123 lblNumber.text = number.ToString

A s s i g n s t h e s t r i n g 123 t o t h e t e x t p r o p e r t y o f t h e lb l N u m b e r c o n t r o l

Pe r f o r m i n g C a l c u l a t i o n s w ith V a r ia b le s

M o d u l u s Op e r a t o r
T his o p er ator c a n b e us e d in place of the b a cksl a s h o p erator to give the re m aind er o f a d iv is io n o p e r a tio n
intRemainder = 17 MOD 3

r e s u l t i s 2
dblRemainder = 17.5 MOD 3

r e s u l t i s 2. 5

C o n c a te n a tin g S tr in g s
C o n c a te n a te : c o n n e c t st r in g s to g e th e r C o n c a te n a tio n o p e r a to r : th e a m p e r s a n d ( &) In c lu d e a s p a c e b e f o r e a n d a f t e r th e & o p e r a to r N u m b e rs a fte r & o p e ra to r a re c o n v e r t e d to st r in g s

C o n c a te n a tin g S tr in g s
H o w to c o n c a t e n a t e c h a r a c t e r s tr in g s
s t r FN a m e = N i t h e e s h " strL N a m e = M u rr u g a n " s t r N a m e = s t r FN a m e & strN a m e = strN a m e & str L N a m e ------------ ----in t X = 1 in t Y = 2 in t R e s u l t = i n t X + i n t Y s t r Ou t p u t = in t X & + & in t Y & = & i n t R e s u l t 1 + 2 = 3

You might also like