Unit 5 NET
Unit 5 NET
T-VADO.
NET
Que.
1Explai
nADO.NETAr chit
ectur
ewi thFi
gure.
Ans.
TheADO.NETarchi
tecturehastwomai nparts:
1)DataProvi
der(
Connect i
onorientedobj
ects)
2)DataSet(
Connect
ionlessobjects)
Dat
aProvider
TheDat aProv i
derisresponsibl
ef orpr ovidi
ngandmai ntai
ningtheconnect i
onto
thedat abase.
ADat aPr oviderisasetofr elat
edcomponent sthatwor ktogethertoprovidedatai
n
anef fi
cientmanner .
TheDat aProv i
derhasf ourcoreobj ects:
1)Connect ion:TheConnect i
onobj ectprov i
des aconnect i
ontothedat abase.
2)Command: TheCommandobj ecti susedt oexecuteacommand.
3)Dat aReader :TheDat aReaderobj ectpr ovi
desaf orward-onl
y,readonly,
connect edrecor dset.DataReaderi sonl yusedwi thconnectedenv i
ronment .
4)Dat aAdapt er
: TheDataAdapt erobj ectpopulatesadi sconnectedDatasetwith
data.Itprovidesabr idgebetweent heDat aSetobjectandt hedatabase.
The. NETFr amewor kcurrent
lycomeswi thfourDataPr ovi
ders:
Dat
aPr
ovi
der Descr
ipt
ion
SQLSer
ver - Pr
ovi
desdat
aaccessf
orMi
crosof
tSQLSer
ver
.
- Usest
heSy
stem.
Dat
a.Sql
Cli
entnamespace.
OLEDB - Fordat
asour
cesexposedbyusi
ngOLEDB.
- Usest
heSy
stem.
Dat
a.Ol
eDbnamespace.
3340704–.
NETPr
ogr
ammi
ng Page1of1
ODBC - Fordat
asour
cesexposedbyusi
ngODBC
- Usest
heSy
stem.
Dat
a.Odbcnamespace.
Or
acl
e - ForOr
acl
edat
asour
ces
- Usest
heSy
stem.
Dat
a.Or
acl
eCl
i
entnamespace.
Dat
aSet
Thedat aseti sadi sconnect ed,i
n-memor yrepr esent ationofdata.
Datasetr epresentei theranent i
redat abaseorsubsetofdat abase.
I tcancont aint ables,relati
onshipbet weent hoset ables.
I fdatasetisdi sconnect ed,adat asetcanbeconsi der edasl ocalcopyofthe
database.
TheDat aSethast womaj orobj
ect s:
1)TheDat aTabl eCollect i
on:
-TheDat aTabl eCollect i
oncont ainsal l t
heDat aTabl eobj ectsinaDat aSet.
-I tcont ai
nsacol lectionofr owsr epresent edbyt heDat aRowCol l
ect
ion.
-I tcont ai
nsacol lectionofcol umnsr epresent edbyt heDat aColumnCollect
ion.
2)TheDat aRel ati
onCol lecti
on:
-TheDat aRelat i
onobj ectrepr
esent sr el
ationshipt hatassoci atesrowsinone
Dat aTablewi thr owsi nanotherDat aTable.
-Rel ationshipsenabl enav i
gati
onf rom onet ablet oanot herinaDat aSet
.
-Theessent ialelement sofDat aRel ationar e:
Thenameoft her el
ati
onshi p
Thenameoft het abl
esbei ngr elat
ed
Ther elatedcol umnsi neacht able.
Que.2Expl
ainCommandobj ect.(
OR)
Expl
ainCommandobj ectwi t
hitsdi
fferentmethods?
Ans.
Commandobj ectsareusedtoexecut equeryagai
nstdatabase.
Thecommandobj ectcanexecuteSQLquer i
esandstoredprocedure.
Toret
riev
e,add,update,anddeletedatayouuseSELECT, I
NSERT, UPDATEand
DELETESQLCommands.
Dat
aAdapt
er
Sel
ectcommand
Command
I
nser
tcommand
Del
etecommand
3340704–.
NETPr
ogr
ammi
ng Page2of2
Updat
ecommand
[
Fig.Rel
ati
onshi
pbet
weenDat
aAdapt
erandCommandobj
ect
]
Commandobj ectcansuppor tSQLst at ementsandst oredprocedurest hatreturn
si
nglevalues; oneormor eset sofr owsornov aluesatall.
Commandobj ectsexposesmet hodsf orexecutingcommandsbasedont hetypeof
commandanddesi r
edret urnv alue.
Commandobj ectisdependedonconnect ionobject.Hence,Connectionshoul dbe
openforbel owmet hods:
1)ExecuteScal ar:
It’
susedwi t
haggregat ef unct ions.
Ther earef iveaggregat ef unctionsMax, Min,Avg,Sum,Count.
Itwillreturnonlyonev alueatat ime.
Itfetchest hedataf rom t hedat abase.
2)ExecuteNonQuer y:
It’
susedf orfir
einser t
,updat e,anddel etequeri
est othedatabase.
Itreturnsnumberofr owsaf fect ed.Itmeansi fwedel etef
iverecor dsthenit
returns5.
3)ExecuteReader :
Whenweexecut ethecommandobj ectusingexecutereadermet hodt hatti
me
itreturnsDat aReader .
ItreturnsDat aReaderwhi chr epr esent sforwardandr eadonlyrecor ds.
4)ExecuteXMLReader:
ItreturnsanXMLReader .
Itavailableforasql commandobj ectonl y.
Exampl
e:
I
mpor
tsSy
stem.
Dat
a.Sql
Cli
ent
Publ
i
cCl
assFor
m1
Di
m conAsSqlConnect
ion
Di
m cmdAsSqlCommand
Di
m strAsSt
ri
ng
Pr
ivat
eSubForm1_Load(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAsSy
stem.
Event
Args)
Handl
esMyBase.
Load
con=NewSqlConnecti
on(
"DataSource=C:
\ Vi
sual
Studi
o
2010\
Proj
ect
s\WindowsAppl
icat
ion4\Test
.sdf
")
EndSub
Pri
vateSubAdd_
Cli
ck(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAsSy
stem.
Event
Args)Handl
es
Add.Cl
ick
st
r="
inser
tint
oSt
udent
Inf
oval
ues(
"+Text
Box1.
Text+"
,'
"+Text
Box2.
Text
+"'
,
'"
3340704–.
NETPr
ogr
ammi
ng Page3of3
+Text
Box3.
Text
+"'
)"
con.
Open()
cmd=NewSql Command(str
,con)
cmd.Execut
eNonQuery(
)
MessageBox .
Show(
"Recor
dInsert
edSuccessf
ull
l
y")
con.
Close()
EndSub
Pri
vateSubDel
ete_
Cli
ck(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAsSy
stem.
Event
Args)Handl
es
Del
ete.Cli
ck
str="del
etefrom St
udentI
nfowhereEnroll
No="+Text
Box1.
Text
con.Open(
)
cmd=NewSql Command(str
,con)
cmd.ExecuteNonQuery(
)
MessageBox .
Show("Recor
dDeletedsuccessf
ull
y")
con.Cl
ose()
EndSub
Pri
vat
eSubUpdat
e_Cl
i
ck(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAsSy
stem.
Event
Args)Handl
es
Updat
e.Cl
ick
str="updateStudentI
nfosetName='"+TextBox2.
Text+"
'wher
eEnr
oll
No="+
TextBox1.
Text
con.Open()
cmd=NewSql Command( str
,con)
cmd.ExecuteNonQuery()
MessageBox .
Show( "
RecordUpdatedsuccessf
ull
y")
con.Cl
ose()
EndSub
Que.
3ExplainDataAdapt erwithexampl e.( OR)
Wri
tedownshor tnoteon“ DataAdapt er
”.
Ans.
I ti
swor kasbridgebet weent heDat abase(DataSour ce)andaDataSet
.
TheDat aAdapterisusedei thertofil
l aDataTableorDat aSetwi
thdataf
rom t
he
dat
abase.
Adat aadaptercanf i
ll
dat asetwi t
ht heuseoff i
l
l method.
Thedat aadapterprovidesdi f
ferentmet hodsandpr operti
estoret
ri
eveandsave
dat
abetweendat asetanddat asource.
TheDataAdapt
erpr
ovi
desf
ivepr
oper
ti
est
hatrepr
esentdat
abasecommands:
Propert
y Descri
pti
on
-Ref
erencet
oacommandt
hatr
etr
iev
esr
owsf
rom t
hedat
a
st
ore.
Sel
ect
Command
-cmd.Sel
ect
Command.CommandText=“
SELECT*FROM
Order
sORDERBYPrice”
3340704–.
NETPr
ogr
ammi
ng Page4of4
-Ref
erencet
oacommandf
ori
nser
ti
ngr
owsi
ntot
hedat
a
I
nser
tCommand
st
ore.
eCommand -Ref
Updat erencet
oacommandf
ormodi
fyi
ngr
owsi
nthedat
ast
ore.
-Ref
erencet
oacommandf
ordel
eti
ngr
owsf
rom t
hedat
a
Del
eteCommand
st
ore.
-Whent heDataAdapterr
etri
evesdatafr
om yourdatastor
e,i
t
usestheinfor
mationintheTableMappi
ngscol l
ect
ionto
Tabl
eMappi
ngs
deter
mi newherei
ny ourDatasett
ostoret
her esul
tsofyour
query.
ATheDataAdapt
eri
ncl
udesTwomai
nmet
hods:
Method Descr
ipt
ion
Fi
l
l Tof
il
lorpopul
atesaDat
asetwi
thdat
a.
Updat
e Thi
smet
hodst
oresdat
afr
om aDat
aSett
otheDat
asour
ce.
Whent heuserper
formtheSQLoper ati
onslikeSel
ect,I
nsertetc.inthedata
contai
ningint
heDatasetObject,itwon'tdi
rectl
yaff
ecttheDatabase, unt
ilt
heuser
i
nvoket heUpdat
emet hodintheDat aAdapter.
Whent heUpdatemethodiscalled,changesintheDataSetarecopiedbackt ot
he
databaseandtheappropr
iateInsert
Command, Del
eteCommand, or
UpdateCommandi sexecuted.
Exampl
e:
Pr
ivat
eSubDispl
ay_
Cli
ck(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAsSy
stem.
Event
Args)
Handl
esDispl
ay.
Cli
ck
str="sel
ect*f
rom StudentI
nfo"
con.Open(
)
cmd=NewSql Command( st
r,con)
Dim dsAsNewDat aSet
Dim daAsNewSqlDat aAdapter
da.
SelectCommand=cmd
da.
Fil
l
(ds)
Dat aGri
dVi
ew1.
Dat
aSour
ce=ds.
Tabl
es(
0)
Fori
=0Tods. Tabl
es(0)
.Rows.Count-1
TextBox1.
Text=ds.Tables(
0).
Rows(i
).
It
em(
1)
MessageBox.Show(TextBox1.
Text
)
Next
con.
Close(
)
EndSub
EndClass
3340704–.
NETPr
ogr
ammi
ng Page5of5
Que.
4Expl ai
nDat aReaderwi thExampl e.
Ans.
TheADO. NETDat aReaderi susedtor etr
ievearead-onl
y,f
orwar
d-onl
yst r
eam of
dat
af rom adat abase.
Wecannotper form anyinsert,updateanddel et
eoperati
onsint
heDat aReader
.
Whenwej ustwantt ofil
lthedat athattimeuseDat aReader.
Dat aReaderr equir
esopenconnect ion.
Dat aReadercanhol donet abl
eatat i
me.
TheExecut eReadermet hodoft heCommandobj ectret
urnsani
nstanceofthe
DataReader.
Af t
ercompl eti
ngthewor kwi ththedat areader,
cal
l t
heClosemethodt ocl
osea
dat
ar eader.
TheDat
aReaderpr
opert
ies
PROPERTIES DESCRI
PTI
ON
Fi
eldCount Ret
urnsnumberofcol
umnsi
nar
ow.
I
sCl
osed I
ndi
cat
eswhet
heradat
areaderi
scl
osed
I
tem Get
sthev
alueofacol
umn.
Recor
dsAf
fect
ed Numberofr
owaf
fect
edaf
terat
ransact
ion
TheDat
aReadermet
hod
METHOD DESCDESCRI
PTI
ONTI
ON
Cl
ose Cl
osesaDat
aReaderobj
ect
.
Read Readsnextr
ecor
dint
hedat
areader
.
Execut
eReader I
tismethodoft
heCommandobj
ect
,whi
chr
etur
nsan
i
nstanceoft
heDat
aReader
Exampl
e:W.
A.Pt
oret
ri
evedat
afr
om dat
abaseusi
ngDat
aReader
.
Pri
vat
eSubShow_
Cli
ck(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAsSy
stem.
Event
Args)Handl
es
Show.
Cli
ck
Di
m drAsSql
Dat
aReader
Di
m dt=NewDataTabl
e()
str="sel
ect*f
rom St
udent
Inf
o"
con.Open(
)
3340704–.
NETPr
ogr
ammi
ng Page6of6
cmd=NewSql Command( st
r,con)
dr=cmd.
ExecuteReader
()
dt.
Load(dr
)
DataGri
dView1.
Dat
aSour
ce=dt
con.Cl
ose(
)
EndSub
Que.
5Di
ff
erencebet
weenADOandADO.
NET.
Que.
6Di
ff
erencebet
weenDatasetandDat
aReader
.
Sr.
Dat
aSet Dat
aReader
No.
ADO ADO.
Net
ADOisbasedon
COM: Component ADO.Netisbasedon
CLR:
Common
Obj
ectModell
i
ng
based. LanguageRunti
mebased.
ADOst
oresdat
ain
binar
y f
ormat
. ADO.Netst
oresdat
ain
XML
for
mati
.e.
par
singofdata.
ADOcan’tbei
ntegr
atedwit
hXML ADO.Netcanbeintegrat
edwit
hXMLas
becauseADOhaveli
mitedaccessof hav
ingrobustsupportofXML.
XML.
InADO,
dat
aispr
ovi
dedby
Recor
dSet
.I
nADO.Netdatai
sprovi
ded
by
Dat
aSet
or
Dat
aAdapter
.
ADOi s
connect
ion
ori
entedmeansit ADO.Netis
disconnected,
doesnotneed
requi
resconti
nuousacti
veconnect
ion. cont
inuousconnection.
ADOgi vesrowsassinglet
abl
evi
ew;
it ADO.Netgiv
esrowsascoll
ecti
onssoyou
scanssequential
lyt
herows canaccessanyrecor
dandalsocango
using
Mov eNext
method. thr
oughatablevi
aloop.
InADO, Youcancr
eat
eonl
y
Cli
ent
si
de I
nADO. Net,
Youcancr
eat
ebot
h Cl
i
ent&
cursor
. Ser
ver
sidecur
sor
.
Usingasingl
econnecti
oni nst
ance, Usingasingl
econnectioninstance,
ADOcannothandlemul t
iple ADO.
Netcanhandlemul t
ipletransact
ions.
tr
ansacti
ons.
DataSetobj
ectcancont ai
nmulti
ple DataReaderprov
idesf orward-onl
yandread-
r
owsetsfr
om thesamedat asourceas onlyaccesstodata.
1 wel lasfr
om therel
ationshi
psbetween
t
hem.
2 Dat
aSeti
sadi
sconnect
edar
chi
tect
ure Dat
aReaderi
sconnect
edar
chi
tect
ure.
3340704–.
NETPr
ogr
ammi
ng Page7of7
3 Dat
aSetcanper
sistdat
a. Dat
aReadercannotper
sistdat
a.
4 Dat
sSeti
ssl
owert
hanDat
aReader
. Speedper
for
mancei
sbet
teri
nDat
aReader
.
5 I
tisdef
inedwi
thmul
ti
plet
abl
es. Youcanaccessonet
abl
eatt
ime.
6 dat
aseti
sconnect
edt
otheXML. Dat
areaderi
sconnect
edt
othedat
asour
ce.
Que.
7Di
ff
erencebet
weenDat
aAdapt
erandDat
aReader
.
Sr. Dat
aReader Dat
aAdapt
er
No.
Que.
8Gi
vedi
ff
erencebet
weendat
asetanddat
atabl
e.
Sr. Dat
aTabl
e Dat
aSet
No.
3340704–.
NETPr
ogr
ammi
ng Page9of9
Mat
hemat
ical
Funct
ions
Mathfunct
ionsareveryusef
ulfuncti
onwhenweneedt oper
form mat
hemati
caloper
ati
ons.To
usethesemathfuncti
onswithoutquali
fi
cat
ion,
impor
ttheSyst
em.Mathnamespace.
TheAbsf
unct
ion
TheAbsret
urnst
heabsol
utev
alueofagi
vennumber
.Itmeansi
twi
l
lret
urnonl
yposi
ti
ve
number
s.
Thesy
ntaxi
s
Mat
h.Abs(
number
)
Exampl
e:
ModuleModule1
SubMain()
Dim Num1,Num2asi nteger
Num1=- 10
Num2=Mat h.Abs(
Num1)
System.Consol
e.Wri
tel
i
ne( Num2)
EndSub
EndModule
Out
Put
:
10
TheExpf
unct
ion
TheExpofanumberxi
stheexponent
ial
val
ueofx,
ie.ex.
.
Thesy
ntaxi
s
Mat
h.Exp(
number
)
Exampl
e:
ModuleModule1
SubMain()
Dim Num1,Num2asi nteger
Num1=2
Num2=Mat h.Exp(Num1)
System.Console.
Writ
eli
ne(Num2)
EndSub
EndModule
Out
Put
:
7.
389056
3340704–.NETProgr
ammi
ng Page10of10
TheFi
xFunct
ion
TheFixfunct
iontr
uncat
et hedeci
malpartofaposit
ivenumberandreturnsthel
argesti
nteger
small
erthanthenumber.However
,whent henumberisnegati
ve,i
twi
llretur
nsmall
estinteger
l
argert
hanthenumber.Forexampl
e,Fi
x(9.
2)=9butFi
x(-9.
4)=-
9
Sy
ntaxi
s
Fi
x(num1)
Exampl
e:
ModuleModule1
SubMain()
Dim Num1,Num2asi nt
eger
Num1=9. 2
Num2=Fi x(
Num1)
System.Consol
e.Wr
it
eli
ne(Num2)
EndSub
EndModule
Out
Put
:
TheI
ntFunct
ion
TheIntisafunct
ionthatconvert
sanumberi nt
oanintegerbyt
runcat
ingi
tsdeci
mal
par
tandt
he
resul
ti
ngint
egeristhelar
gestintegert
hati
ssmall
erthanthenumber.
I
nt(
2.4)
=2,
Int
(6.
9)=6,
Int
(-
5.7)
=-6,
Int
(-
99.
8)=-
100
Sy
ntaxi
s:
I
nt(
num1)
Exampl
e:
ModuleModule1
SubMain()
Dim Num1, Num2asinteger
Num1=- 9.2
Num2=Fi x(Num1)
System.Console.
Wri
tel
i
ne( Num2)
EndSub
EndModule
Out
Put
:
-
10
TheLogFunct
ion
TheLogfunct
ioni
sthef
unct
iont
hatr
etur
nst
henat
ural
logar
it
hm ofanumber
.Forexampl
e,
Log(
10)=2.
302585
3340704–.
NETPr
ogr
ammi
ng Page11of11
Sy
ntaxi
s
Mat
h.Log(
num1)
Exampl
e:
ModuleModule1
SubMain()
Dim Num1,Num2asi nteger
Num1=10
Num2=Mat h.Log(Num1)
System.Console.
Writ
eli
ne(Num2)
EndSub
EndModule
Out
Put
:
TheRnd()Funct
ion
TheRndf
unct
ionr
etur
nsar
andom v
aluebet
ween0and1.
Random numbersinthei
rorigi
nalf
orm ar
enotv eryusefuli
npr ogr
ammingunti
lweconver
tthem
toi
nteger
s.Forexample,ifweneedtoobtai
nar andom outputof6int
egersr
angingf
rom 1to6,
weneedt oconv
erttherandom number
stointegersusingtheformulaI
nt(
Rnd*6)
+1.
Sy
ntaxi
s
Rnd(
)
Exampl
e:
ModuleModule1
SubMain()
Dim Num1asi nteger
Randomi ze()
Num1=I nt(Rnd()
*6)+1
System.Console.Writ
eli
ne(
Num1)
EndSub
EndModule
Out
Put
:
TheRoundFunct
ion
TheRoundf unct
ionist
hefunct
iont
hatr
oundsupanumbert
oacer
tai
nnumberofdeci
mal
pl
aces.Forexample,
Math.
Round(7.
2567,2)=7.
26
Sy
ntaxi
s
Mat
h.Round(
n,m)
3340704–.
NETPr
ogr
ammi
ng Page12of12
I
tmeanst
oroundanumbernt
om deci
mal
places.
Exampl
e:
ModuleModule1
SubMain()
Dim Num1, Num2asinteger
Num1=10. 34667
Num2=Mat h.
Round(
Num1, 2)
System.Console.
Wri
tel
i
ne( Num2)
EndSub
EndModule
Out
Put
:
10.
35
Sqr
t()Funct
ion
TheSqr
tfunct
ioni
susedt
oret
urnst
hesquar
erootoft
henumber
.
Sy
ntaxi
s:
Mat
h.Sqr
t(num)
Exampl
e:
ModuleModule1
SubMain()
Dim Num1,Num2asi nteger
Num1=4
Num2=Mat h.Sqrt
(Num1)
System.Console.
Wri
tel
i
ne( Num2)
EndSub
EndModule
Out
Put
:
St
ri
ngFunct
ions
TheMi
dfunct
ion
Themi df
unct
ioni
susedt
oret
urnasubst
ri
ngcont
aini
ngaspeci
fi
ednumberofchar
act
ersf
rom
astr
ing.
Sy
ntax
Mi
d(st
ri
ng,
star
t[
,l
engt
h])
st
ri
ng-St
ri
ngexpr
essi
onf
rom whi
chchar
act
ersar
eret
urned.
st
art-Char
act
erposi
ti
oni
nst
ri
ngatwhi
chsubst
ri
ngt
obet
aken.
3340704–.
NETPr
ogr
ammi
ng Page13of13
l
engt
h-Numberofchar
act
ers
Exampl
e
ModuleModul e1
SubMain()
Di
m St rAsStri
ng
Str=Mi d("
Hell
oWorld",2,4)
Syst
em. Consol
e.Wr
iteLine(
Str
)
EndSub
EndModul e
Out
Put
:
el
l
o
TheLef
tFunct
ion
TheLef
tfunct
ionext
ractt
hel
eftpor
ti
onofast
ri
ngasperspeci
fi
ednumberofchar
act
er.
Sy
ntax
Lef
t("
str
ing"
,n)
Her
e,ni
snumberofchar
act
er
Exampl
e
Modul
eModule1
SubMain()
Dim St
rAsString
St
r=Left("
Hell
oWor l
d",3)
Sy
stem.Consol
e.Writ
eLi
ne(St
r)
EndSub
EndModul
e
Out
Put
:
Hel
TheRi
ghtFunct
ion
TheRi
ghtf
unct
ionext
ractt
her
ightpor
ti
onofast
ri
ngasperspeci
fi
ednumberofchar
act
er.
Sy
ntax
Ri
ght
("
str
ing"
,n)
Her
e,ni
snumberofchar
act
er
Exampl
e
Modul
eModul
e1
3340704–.
NETPr
ogr
ammi
ng Page14of14
SubMai
n()
Dim StrAsStri
ng
Str=Right(
"Hell
oWorl
d",5)
System.Consol
e.Wr
it
eLi
ne(St
r)
EndSub
EndModul
e
Out
Put
:
Wor
ld
TheTr
imf
unct
ion
Thet
ri
mfunct
iont
ri
mst
heempt
yspacesonbot
hsi
deoft
heSt
ri
ng.
Sy
ntax
Tr
im(
"St
ri
ng"
)
Exampl
e
Modul
eModule1
SubMain(
)
Dim StrAsSt
ring
St
r=Tr i
m("Hell
oWor ld")
Sy
stem.Consol
e.Wri
teLi
ne(St
r)
EndSub
EndModul
e
Out
Put
:
Hel
l
oWor
ld
TheLt
ri
m Funct
ion
TheLt
ri
mfunct
iont
ri
mst
heempt
yspacesoft
hel
eftpor
ti
onoft
hest
ri
ng.
Sy
ntax
Lt
ri
m("st
ri
ng"
)
Exampl
e
Modul
eModule1
SubMain(
)
Di
m StrAsStri
ng
St
r=LTrim("HelloWorld")
Sy
stem.Consol
e.Wri
teLi
ne(
Str
)
EndSub
EndModul
e
Out
Put
:
Hel
l
oWor
ld
TheRt
ri
m Funct
ion
3340704–.
NETPr
ogr
ammi
ng Page15of15
TheRt
ri
mfunct
iont
ri
mst
heempt
yspacesoft
heRi
ghtpor
ti
onoft
hest
ri
ng.
Sy
ntax
Rt
ri
m("st
ri
ng"
)
Exampl
e
Modul
eModule1
SubMain(
)
Di
m StrAsStri
ng
St
r=RTr i
m(" Hell
oWor l
d " )
Sy
stem.Consol
e.Wri
teLi
ne(St
r)
EndSub
EndModul
e
Out
Put
:
Hel
l
oWor
ld
TheUcaseFunct
ion
TheUcasef
unct
ionconv
ert
sal
lthechar
act
ersofast
ri
ngt
ocapi
tal
let
ter
s.
Sy
ntax
Ucase(
"st
ri
ng"
)
Exampl
e
Modul
eModule1
SubMain(
)
Di
m StrAsStri
ng
St
r=UCase("Hell
oWor l
d")
Sy
stem.Consol
e.Wri
teLi
ne(St
r)
EndSub
EndModul
e
Out
Put
:
HELLOWORLD
TheLcaseFunct
ions
TheLcasef
unct
ionconv
ert
sal
lthechar
act
ersofast
ri
ngt
osmal
ll
ett
ers.
Sy
ntax
Lcase(
"st
ri
ng"
)
Exampl
e
Modul
eModul
e1
SubMai
n()
Dim StrAsStri
ng
Str=LCase("Hell
oWor l
d")
System.Consol
e.Wri
teLi
ne(
Str
)
3340704–.
NETPr
ogr
ammi
ng Page16of16
EndSub
EndModul
e
Out
Put
:
hel
l
owor
ld
TheChrfuncti
on
ChrconvertAsci
iv aluetocharacter
.
Synt
ax
Chr
( ascii
)
Example:
ModuleModul e1
SubMai n()
Dim hAsChar
h=Chr (
65)
Consol e.Wr
it
eLine(h)
EndSub
EndModul e
Out
Put
:
TheASCf unct i
on
ASCconv ertchar actertoAsciiv
alue
Syntax
ASC( ‘
char’)
Example:
Modul eModul e1
SubMai n()
Dim cAsChar
c=Asc( “A”)
Consol e.
Writ
eLine(
c)
EndSub
EndModul e
OutPut:
65
3340704–.
NETPr
ogr
ammi
ng Page17of17
Thefoll
owingt
abl
eident
if
iest
hepr
edef
ineddat
eandt
imef
ormatnames.Thesemaybeusedbynameast
hest
ylear
gumentf
ort
heFor
mat
funct
ion:
For
matName Descr
ipt
ion
Gener
alDat
e, Di
spl
aysadateand/
ort
ime.Forexample,3/
12/200811:
07:
31AM.Dat
e
or
G di
spl
ayisdet
ermi
nedbyyourappl
icat
ion'
scurr
entcul
tur
eval
ue.
Long Displ
aysadat
eaccordingtoyourcur
rentcul
tur
e'sl
ongdat
efor
mat
.For
Date,
Medi
um example,
Wednesday
, March12,2008.
Date,
orD
Shor
tDat
e,ord Displaysadat eusingyourcur
rentcul
ture'
sshortdatef
ormat
.For
exampl e,3/12/2008.
Thedchar acterdi
splay
sthedayi nauser-
defi
neddatefor
mat.For
mor einformat i
on,see(
User-
DefinedDate/
TimeFor mat
s(For
mat
Funct i
on).
Long Di
spl
aysatimeusi
ngy ourcur
rentcul
ture'
slongti
meformat
;ty
pical
l
y
Time,
Medium i
ncl
udeshours,
minut
es,seconds.Forexample,11:
07:
31AM.
Time,or
T
Shor
tTi
meort Display
sat imeusi ngyourcurr
entcul
ture'
sshortt
imeformat.For
exampl e,
11:07AM.
Thetchar acterdispl
aysAM orPM valuesforl
ocal
esthatusea12- hour
cl
ocki nauser -defi
nedti
mef or
mat.Formor ei
nfor
mation,see(User-
DefinedDate/ TimeFormats(FormatFuncti
on)
.
f Displ
aysthelongdateandshortt
imeaccor
dingt
oyourcur
rentcul
tur
e's
format.Forexampl
e,Wednesday,Mar
ch12,200811:
07AM.
F Displ
aysthelongdateandl
ongtimeaccor
dingtoyourcurr
entcul
tur
e's
format.Forexampl
e,Wednesday
,March12,200811:07:
31AM.
g Displ
aystheshor
tdateandshor
tti
meaccordi
ngt
oyourcur
rent
cult
ure'
sfor
mat.Forexampl
e,3/
12/200811:
07AM.
M,
m Displaysthemont handthedayofadate.Forexample,March12.
TheM char act
erdisplay
sthemont hinauser-
defi
neddateformat.The
m char act
erdi
splaystheminutesinauser
-defi
nedtimeformat.For
mor einformat
ion,see(User
-Defi
nedDate/
TimeFor mats(For
mat
Funct i
on).
R,
r Formatsthedateaccor
dingt
othe(RFC1123Patt
ernpropert
y.For
example,
Wed, 12Mar200811:07:
31GMT.Thef ormatteddatedoes
notadj
ustthevalueoft
hedateandtime.Youmustadjustthe
Date/
Timev al
uet oGMTbefor
ecall
ingtheFor
matf unct
ion.
s Formatsthedateandti
measasor tabl
eindex.Forexample,
2008-
03-
12T11:07:
31.
Theschar act
erdi
splay
st hesecondsinauser-def
inedti
mef or
mat.For
moreinformati
on,see(User-
Defi
nedDate/TimeFormats(Format
Funct
ion).
3340704–.
NETPr
ogr
ammi
ng Page18of18
u For
matst
hedateandt
imeasaGMTsor
tabl
eindex.Forexampl
e,2008-
03-
1211:
07:
31Z.
U Format
sthedateandti
mewitht
helongdat
eandl
ongt
imeasGMT.For
exampl
e,Wednesday,
March12,
20086:07:
31PM.
Y,
y Formatst
hedateastheyearandmont h.Forexample,Mar
ch,2008.
TheYandycharacter
sdisplayt
heyearinauser -
def
ineddatefor
mat.
Formoreinf
ormat
ion,see(User
-Def
inedDate/TimeFormats(For
mat
Funct
ion)
.
Char
act
er Descr
ipt
ion
(
:) Timeseparator.Insomelocales,othercharact
ersmaybeusedt orepresent
theti
mesepar ator.Thet
imesepar atorseparat
eshour s,
mi nut
es,andseconds
whent i
mev al
uesar efor
mat t
ed.Theact ualcharact
erthatisusedasthet i
me
separat
ori
nf ormat t
edoutputisdeterminedbyy ourappli
cati
on'scur
rent
cult
ureval
ue.
(
/) Datesepar
ator.Insomel ocal
es,otherchar acter
smaybeusedt orepr
esentt
he
datesepar
ator.Thedateseparatorsepar at
est heday,month,andyearwhen
dateval
uesar efor
mat t
ed.Theactual characterthati
susedast hedate
separat
ori
nf ormattedoutputi
sdet er
mi nedbyy ourappl
icati
on'scurr
ent
cul
ture.
(
%) Usedt oi
ndicatethatthefol
l
owi ngchar
actershoul
dbereadasasi ngl
e-l
etter
for
matwi thoutregardtoanytrail
i
nglet
ter
s.Alsousedtoindi
cat
ethatasi ngl
e-
l
etterfor
mati sreadasauser -
definedf
ormat.Seewhatfoll
owsforaddit
ional
det
ails.
d Displ
aysthedayasanumberwit
houtaleadingzer
o( f
orexampl
e,1)
.Use%di
f
thi
sistheonl
ycharact
eri
nyouruser
-def
inednumericfor
mat.
dd Di
spl
ayst
hedayasanumberwi
thal
eadi
ngzer
o(f
orexampl
e,01)
.
ddd Di
spl
ayst
hedayasanabbr
evi
ati
on(
forexampl
e,Sun)
.
dddd Di
spl
ayst
hedayasaf
ull
name(
forexampl
e,Sunday
).
M Displaysthemonthasanumberwithoutal
eadi
ngzer
o( f
orexample,
Januar
y
i
sr epresent
edas1).Use%M i
fthi
sistheonl
ychar
acteri
nyouruser-
def
ined
numer icfor
mat.
MM Di
spl
ayst
hemont
hasanumberwi
thal
eadi
ngzer
o(f
orexampl
e,01/
12/
01)
.
MMM Di
spl
ayst
hemont
hasanabbr
evi
ati
on(
forexampl
e,Jan)
.
MMMM Di
spl
ayst
hemont
hasaf
ull
mont
hname(
forexampl
e,Januar
y).
gg Di
spl
ayst
heper
iod/
erast
ri
ng(
forexampl
e,A.
D.)
.
h Displ
ayst
hehourasanumberwi t
houtleadi
ngzer
osusi
ngt
he12-
hourclock
(f
orexample,
1:15:15PM).Use%hift
hisist
heonl
ychar
act
eri
nyouruser-
defi
nednumericformat
.
3340704–.
NETPr
ogr
ammi
ng Page19of19
hh Displ
aysthehourasanumberwi
thl
eadi
ngzer
osusi
ngt
he12-
hourcl
ock(
for
example,
01:15:
15PM).
H Displ
ayst
hehourasanumberwit
houtl
eadi
ngzer
osusi
ngthe24-
hourclock
(f
orexample,
1:15:
15)
.Use%Hift
hisi
stheonl
ychar
act
eri
nyouruser
-defi
ned
numericf
ormat.
HH Displ
aysthehourasanumberwi
thl
eadi
ngzer
osusi
ngt
he24-
hourcl
ock(
for
example,
01:15:
15).
m Di
spl
aystheminuteasanumberwit
houtl
eadingzeros(
forexampl
e,12:
1:15)
.
Use%m i
fthi
sistheonl
ychar
acteri
nyouruser-
defi
nednumericf
ormat.
mm Di
spl
ayst
hemi
nut
easanumberwi
thl
eadi
ngz
eros(
forexampl
e,12:
01:
15)
.
s Di
spl
aysthesecondasanumberwit
houtl
eadi
ngzer os(
forexample,
12:
15:
5).
Use%sifthi
sistheonl
ychar
act
eri
nyouruser
-def
inednumericfor
mat.
ss Di
spl
ayst
hesecondasanumberwi
thl
eadi
ngzer
os(
forexampl
e,12:
15:
05)
.
f Displ
aysfracti
onsofseconds.Forexampleffdi
splayshundr
edt
hsofseconds,
whereasffffdispl
ayst
en-t
housandthsofseconds.Youmayuseuptoseven
fsymbolsi
ny ouruser-
defi
nedformat.Use%fifthi
sistheonl
ychar
acteri
n
youruser-
definednumeri
cformat.
t Usesthe12-hourcl
ockanddispl
aysanuppercaseAforanyhourbefor
enoon;
displ
aysanuppercasePforanyhourbet
weennoonand11: 59P.M.Use%tif
thisi
stheonlycharact
eri
nyouruser
-def
inednumeri
cformat.
t
t Forlocal
esthatusea12-
hourcl
ock,
displ
aysanuppercaseAM withanyhour
beforenoon;di
splay
sanupper
casePM wit
hanyhourbet weennoonand11:59
P.M.
Forlocal
esthatusea24-
hourcl
ock,
displ
aysnot
hing.
y Displ
aystheyearnumber(
0-9)wi
thoutl
eadi
ngzer
os.Use%yi
fthi
sist
heonl
y
charact
eriny
ouruser-
defi
nednumericf
ormat.
y
y Di
spl
ayst
hey
eari
ntwo-
digi
tnumer
icf
ormatwi
thal
eadi
ngzer
o,i
fappl
i
cabl
e.
y
yy Di
spl
ayst
hey
eari
nfour
-di
gitnumer
icf
ormat
.
y
yyy Di
spl
ayst
hey
eari
nfour
-di
gitnumer
icf
ormat
.
z Displ
aysthet
imezoneoff
setwi
thoutaleadi
ngzer
o(forexample,
-8)
.Use%zi
f
thi
sistheonl
ycharact
eri
nyouruser-
defi
nednumeri
cformat.
zz Di
spl
ayst
het
imezoneof
fsetwi
thal
eadi
ngzer
o(f
orexampl
e,-
08)
zzz Di
spl
ayst
hef
ull
timezoneof
fset(
forexampl
e,-
08:
00)
Formor
einf
ormat
ionaboutt
heappl
i
cat
ion'
scur
rentcul
tur
einf
ormat
i see(
on, HowCul
tur
eAf
fect
sSt
ri
ngsi
nVi
sual
Basi
c.
Exampl
( e
Thef
oll
owi
ngar
eexampl
esofuser
-def
ineddat
eandt
imef
ormat
sforDecember7,
1958,
8:50PM,
35seconds:
For
mat Di
spl
ays
M/
d/y
y 12/
7/58
3340704–.
NETPr
ogr
ammi
ng Page20of20
d-
MMM 7-
Dec
d-
MMMM-
yy 7-
December
-58
dMMMM 7December
MMMM y
y December58
hh:
mm t
t 08:
50PM
h:
mm:
sst 8:
50:
35P
H:
mm 20:
50
H:
mm:
ss 20:
50:
35
M/
d/y
yyyH:
mm 12/
7/195820:
50
For
matst
ri
ng
Tost
art
,weuseaspeci
fi
cfor
mat
ti
ngst
ri
ngwi
thDat
eTi
meand
ToSt
ri
ngt
oobt
ainaspeci
alDat
eTi
mest
ri
ng.Thi
sisusef
ulwhen
i
nter
act
ingwi
thot
hersy
stems,
orwheny
our
equi
reapr
eci
se
f
ormat
.
Not
e:Thel
ett
ersi
nthef
ormatst
ri
ngabov
especi
fyt
heout
put
.
Thef
inal
commentshowswhatef
fectt
hef
ormatpat
ter
ns
hav
e.
Basedon:
.
NET4.
5
C#pr
ogr
am t
hatusesDat
eTi
mef
ormat
usi
ngSy
stem;
cl
assProgram
{
st
ati
cvoidMain()
{
DateTi
met i
me=Dat eTi
me.Now; //Usecurr
entt
ime
str
ingf
ormat="MMM ddddHH: mm yy
yy"
;/ /Usethi
sformat
Consol
e.Wri
teLi
ne(
ti
me.ToStr
ing(
for
mat))
;//Writ
etoconsol
e
}
}
Out
put
FebFr
i2711:
412009
3340704–.
NETPr
ogr
ammi
ng Page21of21
For
matst
ri
ngpat
ter
n
MMM di splaythree-l
ettermont
h
ddd di
splaythree-l
etterdayoft
heWEEK
d displaydayoft heMONTH
HH displaytwo-digithourson24-
hourscal
e
mm di splaytwo-digitminut
es
yyy
y di
splayfour-di
gityear
Modi
fyf
ormat
Cont
inui
ngon,
wemodi
fyt
heDat
eTi
mef
ormatst
ri
ngi
ntheabov
e
exampl
etogetdi
ff
erentout
putwi
thToSt
ri
ng.Wechangesome
oft
hef
iel
dssot
her
esul
ti
ngst
ri
ngv
aluei
sshor
ter
.
For
matst
ri
ngusages.Youwi
l
lal
soneedt
ospeci
fyaf
ormat
st
ri
ngwhenusi
ngDat
eTi
me.
Par
seExactand
Dat
eTi
me.
Par
seExact
.Thi
sisbecauset
hosemet
hodsr
equi
rea
cust
om pat
ter
ntopar
se.
C#pr
ogr
am t
hatusesdi
ff
erentf
ormat
usi
ngSy
stem;
cl
assProgram
{
st
ati
cvoidMain()
{
DateTi
met i
me=Dat eTi
me.Now; //Usecurrentt
ime
str
ingf
ormat="M dh:mm yy"; //Usethi
sformat
Consol
e.Wri
teLi
ne(
ti
me.ToStr
ing(
for
mat )
);/
/Wr i
tetoconsol
e
}
}
Out
put
22711:
4809
For
matst
ri
ngpat
ter
n
M displayone-digitmonthnumber [
changed]
d displayone-digitdayoftheMONTH [ changed]
h displayone-digithouron12-hourscal
e[changed]
mm displaytwo-digitminut
es
yy displaytwo-digityear [
changed]
Si
ngl
e-charf
ormat
Nexty
oucanuseasi
ngl
echarwi
thToSt
ri
ngor
Dat
eTi
me.
Par
seExactt
ospeci
fyapr
esetf
ormat
.Thesear
e
3340704–.
NETPr
ogr
ammi
ng Page22of22
st
andar
dfor
mat
sandusef
uli
nmanypr
ogr
ams.Theycan
el
i
minat
ety
posi
nthecust
om f
ormatst
ri
ngs.
Dat
eTi
me.
Par
se
C#pr
ogr
am t
hatt
est
sfor
mat
s
usi
ngSy
stem;
cl
assProgram
{
st
ati
cvoidMain()
{
DateTi
menow=Dat eTi
me.Now;
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"d")
);
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"D"))
;
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"f"
));
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"F")
);
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"g")
);
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"G"))
;
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"m"));
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"M"));
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"o")
);
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"O"))
;
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"s")
);
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"t"
));
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"T")
);
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"u")
);
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"U")
);
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"y"
));
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"Y")
);
}
}
Out
put
d 2/ 27/2009
D Fri
day , February27, 2009
f Fri
day , February27, 200912: 11PM
F Friday ,Febr uar
y27, 200912: 12:22PM
g 2/ 27/200912: 12PM
G 2/27/ 200912: 12: 22PM
m Febr uar y27
M Febr uar y27
o 2009- 02- 27T12: 12:22.1020000- 08:
00
O 2009- 02-27T12: 12:22. 1020000-08:
00
s 2009- 02- 27T12: 12:22
t 12:12PM
T 12: 12:22PM
u 2009- 02- 2712: 12:22Z
U Fri
day , February27, 20098:12:22PM
y Febr uar y,2009
Y Febr uar y,2009
Dat
est
ri
ngs
3340704–.
NETPr
ogr
ammi
ng Page23of23
Her
eweseet
heToLongDat
eSt
ri
ng,
ToLongTi
meSt
ri
ng,
ToShor
tDat
eSt
ri
ng,
andToShor
tTi
meSt
ri
ngmet
hodsonDat
eTi
me.
Thesemet
hodsar
eequi
val
entt
othel
ower
caseandupper
caseD
andTmet
hodsshowni
ntheex
ampl
eabov
e.
Not
e:Thedef
aul
tToSt
ri
ngonDat
eTi
mei
sequi
val
entt
othe
"
G"f
ormat
ti
ngst
ri
ng.ToSt
ri
ng(
"G"
)andToSt
ri
ng(
)dot
he
samet
hing.
st
ri
ng.
For
mat
C#pr
ogr
am t
hatusesToSt
ri
ngmet
hods
usi
ngSy
stem;
cl
assProgram
{
st
ati
cvoidMain()
{
DateTi
menow=Dat eTi
me.Now;
Consol
e.Wri
teLi
ne(
now.
ToLongDateSt
ri
ng())
;/ /Equi
valenttoD
Consol
e.Wri
teLi
ne(
now.
ToLongTimeStr
ing(
));//Equi
v alentt
oT
Consol
e.Wri
teLi
ne(
now.
ToShortDat
eSt
ring(
));/
/Equivalenttod
Consol
e.Wri
teLi
ne(
now.
ToShortTi
meStri
ng()
);//Equi
valenttot
Consol
e.Wri
teLi
ne(
now.
ToStr
ing()
);
}
}
Out
put
ToLongDateSt
ri
ng Fr iday ,
February27,
2009
ToLongTimeStr
ing 12: 16:59PM
ToShort
DateSt
ring 2/27/ 2009
ToShort
TimeStri
ng 12: 16PM
ToStr
ing 2/27/200912: 16:59PM
Secondsf
ormat
Secondsar
efor
mat
tedwi
tht
hel
ower
casef
,theupper
caseF,
and
t
hel
ower
cases.Theupper
caseFchangeshowt
rai
l
ingzer
osar
e
di
spl
ayed.Andt
hel
ower
caseschangeshowl
eadi
ngzer
osar
e
di
spl
ayed.
Uset
hel
ower
caseft
oindi
cat
ethesecondst
oonedi
gitl
engt
h.
3340704–.
NETPr
ogr
ammi
ng Page24of24
Uset
wol
ower
casef
s(f
f)t
oindi
cat
ethesecondst
otwodi
git
s.
Theupper
caseFpat
ter
nsdot
hesamebutwor
kdi
ff
erent
lyon
t
rai
l
ingzer
os.
Thel
ower
casesdi
spl
aysseconds.Asi
ngl
elower
casesmeans
t
haty
oudonotr
equi
real
eadi
ngzer
o.Twol
ower
cases
char
act
ers(
ss)meansy
oual
way
swantt
wodi
git
s,suchas00-
59.
Mi
nut
esf
ormat
Next
,Dat
eTi
mef
ormatst
ri
ngsuset
hel
ower
casem ormm f
or
mi
nut
es.Youcanuseoneort
wol
ower
casems.Twol
ower
case
mshasal
eadi
ngzer
oift
henumberi
sonl
yonedi
gitl
ong.Thi
sis
t
heonl
ydi
ff
erence.
Ti
p:Thet
womsmeanst
hatt
her
ear
eal
way
stwodi
git
s
di
spl
ayed,
wit
hal
eadi
ngzer
oifnecessar
y.
Hour
sfor
mat
I
nDat
eTi
mef
ormat
s,hour
sar
erepr
esent
edwi
thl
ower
casehand
upper
caseH.Youcanuseoneort
wochar
act
ers.Thel
ower
case
hi
susedf
ora12-
hourcl
ock.Theupper
caseHi
sa24-
hourcl
ock.
Val
ueh:
Displ
ayt
hehour
sinonedi
giti
fpossi
ble.I
fthehour
s
i
sgr
eat
ert
han9,
itwi
l
ldi
spl
ayt
wodi
git
s.Rangei
s1-
12.
Val
uehh:
Displ
ayt
hehour
sint
wodi
git
sal
way
s,ev
eni
fthe
houri
sonedi
git
.Ther
angeher
ewi
l
lbe01-
12.
Val
ueH:
Thi
srepr
esent
sthehour
sinar
angeof0-
23,
whi
chi
s
cal
l
edmi
l
itar
yti
mei
nsomepar
tsoft
hewor
ld.
Val
ueHH:
Thi
sret
urnst
hehour
sinar
angeof00-
23.Wi
tha
3340704–.
NETPr
ogr
ammi
ng Page25of25
si
ngl
eH,
ther
eisal
way
sal
eadi
ngzer
oift
henumberi
sone
di
git
.
Dayf
ormat
Thef
ormat
s"d"and"
dd"(
oneandt
wods)meansomet
hing
di
ff
erentf
rom "
ddd"and"
dddd"(
thr
eeandf
ourds)
.One"
d"and
"
dd"i
ndi
cat
ethedayoft
hemont
h,whi
l
e"ddd"and"
dddd"i
ndi
cat
e
t
hedayoft
heweek,
inawor
d.
Val
ued:
Uset
hist
ospeci
fyt
henumer
icv
aluef
ort
hedayof
t
hemont
h.I
twi
l
lbeoneort
wodi
git
slong.
Val
uedd:
Thi
sist
hesameasasi
ngl
ed,
exceptt
her
ear
e
al
way
stwodi
git
s,wi
thal
eadi
ng0pr
ependedi
fnecessar
y.
Val
ueddd:
Thi
sdi
spl
aysat
hree-
let
terst
ri
ngt
hati
ndi
cat
est
he
cur
rentdayoft
heweek.
Val
uedddd:
Thi
sdi
spl
ayst
hef
ull
str
ingf
ort
hedayoft
he
week.Anexampl
ewoul
dbe"
Sat
urday
".
Mont
hfor
mat
I
nDat
eTi
mef
ormatst
ri
ngs,
themont
hisf
ormat
tedwi
than
upper
caseM.Youcanuseonet
ofourMs.Oneandt
woMs
di
spl
aynumer
icr
epr
esent
ati
ons.Thr
eeandf
ourMsdi
spl
ayst
ri
ng
r
epr
esent
ati
ons.
M,MM:
Thesedi
splayt
hemont
hsi
nnumer
icf
orm.OneM
3340704–.
NETProgr
ammi ng Page26of26
doesnothav
eal
eadi
ngzer
ooni
t.TwoMshav
eal
eadi
ng
zer
o.
MMM:
Thi
sdi
spl
ayst
heabbr
evi
atedt
hree-
let
terf
orm oft
he
mont
hrepr
esent
edi
ntheDat
eTi
me.
MMMM:
Thi
sdi
spl
ayst
hef
ull
mont
hst
ri
ng,
proper
ly
capi
tal
i
zed.Anexampl
eis"
Januar
y".
Yearf
ormat
They
earf
ormatusest
hel
ower
caseyl
ett
er.Youcanputonet
o
f
ivechar
act
ersf
ort
hey
ear
.Iti
sunl
i
kel
yyouwi
l
lneedf
ive
char
act
ersanyt
imesoon.Fory
earst
ri
ngs,
weuset
hev
alues"
y"
t
hrough"
yyy
yy"
.
Ti
p:Thesedi
spl
ayt
hey
eart
odi
ff
erentdi
git
s.I
nyourpr
ogr
ams,
y
ouwon'
tneedt
hreedi
git
sfort
hey
ear(
yyy
),orf
ive.
Mi
sc.
Ev
enmor
eDat
eTi
mef
ormat
ti
ngchar
act
ersar
eav
ail
abl
e.Youcan
changehowAM andPM ar
edi
spl
ayed.Youcandi
spl
ayADand
BC.Youcanshowt
heUTCof
fsetandev
endi
spl
ayt
imezones.
Val
uet
:Uset
hel
ower
casett
oindi
cat
eA,
whent
het
imei
sin
t
heAM,
andP,
forwhent
het
imei
sinPM.
Val
uet
t:
Uset
wol
ower
caset
tst
odi
spl
ayt
hef
ull
AM orPM
st
ri
ng.Youwi
l
lnor
mal
l
ywantt
hisf
ordi
spl
ayi
ngt
hest
ri
ngt
o
auser
.
Val
uegg:
Uset
hist
odi
spl
ayADony
ourdat
e.I
tisunl
i
kel
ythat
t
hiswi
l
lbeBCi
nmostpr
ogr
ams.
Val
uek:
Uset
hist
odi
spl
ayt
imezonei
nfor
mat
ion.Thi
sis
of
tennotneededi
npr
ogr
ams.
Val
uesz,
zz,
zzz:
Theser
epr
esentt
heof
fsetf
rom t
heUTC
t
imeont
hel
ocal
oper
ati
ngsy
stem.
Col
onandsl
ash:
Thecol
oni
sthet
imesepar
ator"
:"
.Thesl
ash
t
hedat
esepar
ator"
/".
Thr
ee-
let
terday
s
3340704–.
NETPr
ogr
ammi
ng Page27of27
I
tmaybeusef
ult
odi
spl
ayt
hedayoft
heweeki
nat
hree-
let
ter
f
orm.Her
eweseeasi
mpl
epr
ogr
am t
hatpr
int
soutt
heday
sof
t
heweeki
nthr
ee-
let
terf
ormat
.Thi
swi
l
lvar
ybasedont
he
l
anguagei
nst
all
edont
hecomput
er.
C#pr
ogr
am t
hatt
est
sday
s
usi
ngSy
stem;
cl
assProgram
{
st
ati
cvoidMai n(
)
{
DateTimenow=Dat eTime.Today;
for(i
nti=0;i<7;i
++)
{
Console.
Wri
teLi
ne(now.ToStr
ing(
"ddd"
));
now=now. AddDays(1)
;
}
}
}
Out
put
Thu
Fri
Sat
Sun
Mon
Tue
Wed
Compl
eteday
Of
teny
ouneedt
odi
spl
ayt
hecompl
etedayoft
heweeki
nyour
C#code,
andt
hef
ourdst
oget
herwi
l
ldot
his.Wi
th
Consol
e.Wr
it
eLi
ne,
wepr
intal
lsev
endi
ff
erentdayst
ri
ngsy
oucan
getf
rom t
hedddd.
Consol
e.Wr
it
eLi
ne
C#pr
ogr
am t
hatshowsdayst
ri
ngs
usi
ngSy
stem;
cl
assProgram
{
st
ati
cvoidMai n(
)
{
DateTimenow=Dat eTime.Today;
for(i
nti=0;i<7;i
++)
{
Console.
Wri
teLi
ne(now.ToStr
ing(
"dddd"
));
now=now. AddDays(1)
;
}
}
}
Out
put
Thursday
Fri
day
Saturday
3340704–.
NETPr
ogr
ammi
ng Page28of28
Sunday
Monday
Tuesday
Wednesday
Er
a
The.
NETFr
amewor
kal
l
owsy
out
odi
spl
ayt
hedat
ewi
tht
heer
a
orper
iod—usual
l
yADorBC.I
tisunl
i
kel
ythaty
ouwi
l
lneedt
ouse
BC,
excepti
nar
aret
heor
eti
cal
appl
i
cat
ion.Her
eiswhatt
het
wo
gswi
l
lpr
int
.
St
atementt
hatpr
int
ser
a:C#
Consol
e.Wr
it
eLi
ne(
Dat
eTi
me.
Now.
ToSt
ri
ng(
"gg"
));
Out
put
A.
D.
Mont
h
Pr
ogr
amshav
edi
ff
erentf
ormat
ti
ngr
equi
rement
sformont
h
names.Somet
imes,
thef
ir
stt
hreel
ett
ersoft
henamear
eneeded.
Thi
shel
psi
ntabul
arorspr
eadsheetdi
spl
ays.Mor
edet
ail
sand
exampl
esar
eav
ail
abl
e.
Dat
eTi
me.
Mont
h
AM,
PM
Thi
sisn'
tsomet
hingy
ouar
eli
kel
ytoneed,
buti
nter
est
ingt
ofi
nd
out
.Wheny
ouspeci
fyonet
,youcangett
hef
ir
stl
ett
eroft
heAM
orPM st
ri
ng.Thi
sisequi
val
entt
ousi
ngSubst
ri
ngorget
ti
ngt
he
f
ir
stcharoft
het
tst
ri
ng.
Ti
p:Ther
eisaspaceatt
heendoft
hef
ormatst
ri
ng—t
hev
alue
"
t"canmeansomet
hingel
sei
nthef
ormatst
ri
ng.
Next
:Weshowapr
ogr
am t
hatget
sthest
ri
ngAM orPM i
n
Dat
eTi
meToSt
ri
ngcode.
Not
e:Ther
ear
enoper
iodsi
ntheout
putoft
t.I
fyour
equi
re
per
iodsi
nyourAM orPM,
youwoul
dhav
etomani
pul
ate
t
hest
ri
ng.
C#t
hatdi
spl
aysAM andPM
3340704–.
NETPr
ogr
ammi
ng Page29of29
usi
ngSy
stem;
cl
assProgram
{
st
ati
cvoidMai n(
)
{
DateTimenow=Dat eTime.Now;
for(i
nti=0;i<2;i
++)
{
Console.
Wri
teLi
ne(now.ToSt
ri
ng(
"t
t")
);
now=now. AddHours(12)
;
}
}
}
Out
put
PM
AM
Year
Youcanv
aryt
henumberofdi
git
sdi
spl
ayedi
nthey
earst
ri
ng.You
wi
l
lal
way
swantt
ousey
,yy
,ory
yyyf
ory
ourpr
ogr
ams.The.
NET
Fr
amewor
kaccept
sdi
ff
erentnumber
s,butt
heyar
eimpr
act
ical
in
t
her
eal
wor
ld.
Not
e:Occasi
onal
l
ytwoy
sisusef
ulf
orauser
-or
ient
ed
pr
ogr
am,
butf
ory
ourbackendcode,
youwi
l
lwantt
ousef
our
y
s.
C#t
hatdi
spl
aysy
ear
s
usi
ngSy
stem;
cl
assProgram
{
st
ati
cvoidMain()
{
DateTi
menow=Dat eTi
me.Now;
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"y")
);
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"yy
"))
;
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"yy
y"))
;/ /<--Don'tusethis
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"yy
yy")
);
Consol
e.Wri
teLi
ne(
now.
ToStr
ing(
"yy
yyy")
);//<--Don'tusethi
s
}
}
Out
put
9
09
2009
2009
02009
3340704–.
NETPr
ogr
ammi
ng Page30of30