Sei sulla pagina 1di 6

Visual Basic6.

0 Unit 3
Data Type Conversion Functions
A complete listing of all the conversion functions offered by Visual Basic is elucidated
below.
Function Conversion To
Cbool Boolean
Cbyte Byte
Ccur Currency
Cdate Date
Cdec Decimals
CDbl Double
Cint Integer
CLng Long
CSng Single
CStr String
Cvar Variant
CVrr rror
Visual Basic Built-in Functions
!any built"in functions are offered by Visual Basic fall under various categories. #hese
functions are procedures that return a value. #he functions fall into the following basic
categories that will be discussed in the following sections at length.
Date and Time Functions
Format Function
String Functions
Date and #ime are internally stored as numbers in Visual Basic. #he decimal points
represents the time between $%$$%$$ and &'%()%() hours inclusive.
Now, Date and Time functions * +etrives the System Date , #ime.
Page 1 of 6
Visual Basic6.0 Unit 3
Now () Date () Time ()
+etrieves the date and time retrieves only date retrieves only #ime
#o display both the date and time together a message bo- is displayed use the
statement given below.
MsgBox "The current date and time of the sstem is" ! Now
.ere , is used as a concatenation operator to concentrate the string and the /ow
function. Selective portions of the date and time value can be e-tracted using the
below listed functions.
0ther 1sed 2unction
"# $andom No %enerator (Function $nd)
Synta-%
Dim /um
/um3Int 441Bound"LBound5678+nd5LBound7
For eg to fnd a random no b/w 1 to 10
Num = int ((10-1+1)*Rnd+1)
2.
Page of 6
Visual Basic6.0 Unit 3
Function &xtracted 'ortion
9ear 4 7 9ear 4/ow7
!onth 4 7 !onth 4/ow7
Day 4 7 Day 4/ow7
:ee;Day 4 7 :ee;Day 4/ow7
.our 4 7 .our 4/ow7
!inute 4 7 !inute 4/ow7
Second 4 7 Second 4/ow7
Conversion functions related to date and time functions are listed below.
Function Descri(tion
Date)dd ( ) +eturns a date to which a specific interval has been added
DateDiff ( ) +eturns a Long data type value specifying the interval
between the two values
Date'art ( ) +eturns an Integer containing the specified part of a given
date
Date*a+ue ( ) Converts a string to a Date
Time*a+ue ( ) Converts a string to a time
DateSeria+ ( ) +eturns a date for specified year< month and day
2ollowing e-ample illustrates the use of the control array.
, 0pen a Standard = pro>ect and save the 2orm as Calculator.frm and save the ?ro>ect as Calculater.vbp.
, Design the form as shown below.
Object Property Setting
Form
Caption
!a"e
Calculator
fr"Calculator
Page 3 of 6
Visual Basic6.0 Unit 3
Co""an#Button
Caption
!a"e
$n#e%
1
c"#
0
Co""an#Button
Caption
!a"e
$n#e%

c"#
1
Co""an#Button
Caption
!a"e
$n#e%
3
c"#

Co""an#Button
Caption
!a"e
$n#e%
&
c"#
3
Co""an#Button
Caption
!a"e
$n#e%
'
c"#
&
Co""an#Button
Caption
!a"e
$n#e%
6
c"#
'
Co""an#Button
Caption
!a"e
$n#e%
(
c"#
6
Co""an#Button
Caption
!a"e
$n#e%
)
c"#
(
Co""an#Button
Caption
!a"e
$n#e%
*
c"#
)
Co""an#Button
Caption
!a"e
$n#e%
0
c"#
10
Co""an#Button
Caption
!a"e
$n#e%
.
c"#
11
Co""an#Button
Caption
!a"e
+C
c"#+C
Co""an#Button
Caption
!a"e
,
c"#Plus
Co""an#Button
Caption
!a"e
-
c"#.inus
Co""an#Button
Caption
!a"e
/
c"#.ultiply
Co""an#Button
Caption
!a"e
0
c"#Divi#e
Page & of 6
Visual Basic6.0 Unit 3
Co""an#Button
Caption
!a"e
,0-
c"#!eg
Te%tBo%
!a"e
Te%t
t%tDisplay
1 e"pty 2
Co""an#Button
Caption
!a"e
3
c"#45ual
#he following variables are declared inside the general declaration
Dim Current As Double
Dim Previous As Double
Dim Choice As String
Dim Result As Double
#he following code is entered in the cmd@Clic;4 7 4Control Array7 event procedure
Private Sub cmd_Click(Index As Integer)
txtDispla!"ext # txtDispla!"ext $ cmd(Index)!Caption
%$is the concatenation operator
Current # &al(txtDispla!"ext)
'nd Sub
#he following code is entered in the cmdAC@Clic; 4 7 event procedure
Private Sub cmdAC_Click()
Current # Previous # (
txtDispla!"ext # ))
'nd Sub
#he below code is entered in the cmd/eg@Clic;4 7 procedure
Private Sub cmd*eg_Click()
Current # +Current
txtDispla!"ext # Current
'nd Sub
#he following code is entered in the clic; events of the cmd?lus< cmd!inus< cmd!ultiply< cmdDevide controls
respectively.
Private Sub cmdDevide_Click()
txtDispla!"ext # ))
Previous # Current
Page ' of 6
Visual Basic6.0 Unit 3
Current # (
Choice # ),)
'nd Sub
Private Sub cmd-inus_Click()
txtDispla!"ext # ))
Previous # Current
Current # (
Choice # )+)
'nd Sub
Private Sub cmd-ultipl_Click()
txtDispla!"ext # ))
Previous # Current
Current # (
Choice # ).)
'nd Sub
Private Sub cmdPlus_Click()
txtDispla!"ext # ))
Previous # Current
Current # (
Choice # )/)
'nd Sub
#o print the result on the te-t bo-< the following code is entered in the cmdAual@Clic; 4 7 event procedure.
Private Sub cmd'0ual_Click()
Select Case Choice
Case )/)
Result # Previous / Current
txtDispla!"ext # Result
Case )+)
Result # Previous + Current
txtDispla!"ext # Result
Case ).)
Result # Previous . Current
txtDispla!"ext # Result
Case ),)
Result # Previous , Current
txtDispla!"ext # Result
'nd Select
Current # Result
'nd Sub
Save and run the pro>ect. 0n clic;ing digits of userBs choice and an operator button< the output appears.
Page 6 of 6

Potrebbero piacerti anche