Академический Документы
Профессиональный Документы
Культура Документы
VisualBasicTutorialLesson16:Arrays
Lesson16:Arrays
<PreviousLesson><<Home>><NextLesson>
AdsbyGoogle
VisualBasicCode
ArrayArrays
PythonArray
NetArray
16.1IntroductiontoArrays
Bydefinition,anarrayisalistofvariableswiththesamedatatypeandname.Whenweworkwithasingleitem,weonlyneedtouseone
variable.However,ifwehavealistofitemswhichareofsimilartypetodealwith,weneedtodeclareanarrayofvariablesinsteadofusinga
variableforeachitem
Forexample,ifweneedtoenteronehundrednames,itisdifficultytodeclare100differentnames,thisisawasteoftimeandefforts.So,
insteadofdeclaringonehundreddifferentvariables,weneedtodeclareonlyonearray.Wedifferentiateeachiteminthearraybyusing
subscript,theindexvalueofeachitem,forexamplename(1),name(2),name(3).......etc.,makesdeclaringvariablesmorestreamline.
AdsbyGoogle NetArray
ArrayC#
VBNet JavaLesson
16.2DimensionofanArray
Anarraycanbeonedimensionalormultidimensional.Onedimensionalarrayislikealistofitemsoratablethatconsistsofonerowofitemsor
onecolumnofitems.
Atwodimensionalarrayisatableofitemsthatmakeupofrowsandcolumns.TheformatforaonedimensionalarrayisArrayName(x),the
formatforatwodimensionalarrayisArrayName(x,y)andathreedimensionalarrayisArrayName(x,y,z).Normallyitissufficienttouseone
dimensionalandtwodimensionalarray,youonlyneedtousehigherdimensionalarraysifyouneedtodealwithmorecomplexproblems.Let
meillustratethethearrayswithtables.
Table16.1.OnedimensionalArray
StudentName
Name(1)
Name(2)
Name(3)
Name(4)
Table16.2TwoDimensionalArray
Name(1,1)
Name(1,2)
Name(1,3)
Name(1,4)
Name(2,1)
Name(2,2)
Name(2,3)
Name(2,4)
Name(3,1)
Name(3,2)
Name(3,3)
Name(3,4)
http://www.vbtutor.net/lesson16.html
1/3
03/09/2015
VisualBasicTutorialLesson16:Arrays
16.2DeclaringArrays
WecanusePublicorDimstatementtodeclareanarrayjustasthewaywedeclareasinglevariable.ThePublicstatement
declaresanarraythatcanbeusedthroughoutanapplicationwhiletheDimstatementdeclareanarraythatcouldbeusedonly
inalocalprocedure.
Thegeneralformattodeclareaonedimensionalarrayisasfollow:
DimarrayName(subs)asdataType
wheresubsindicatesthelastsubscriptinthearray.
Example16.1
DimCusName(10)asString
willdeclareanarraythatconsistsof10elementsifthestatementOptionBase1appearinthedeclarationarea,startingfrom
CusName(1)toCusName(10).Otherwise,therewillbe11elementsinthearraystartingfromCusName(0)throughto
CusName(10)
CusName(1)
CusName(2)
CusName(3)
CusName(4)
CusName(5)
CusName(6)
CusName(7)
CusName(8)
CusName(9)
CusName(10)
Example16.2
DimCount(100to500)asInteger
declaresanarraythatconsistsofthefirstelementstartingfromCount(100)andendsatCount(500)
Thegeneralformattodeclareatwodimensionalarrayisasfollow:
DimArrayName(Sub1,Sub2)asdataType
Example16.3
DimStudentName(10,10)willdeclarea10x10tablemakeupof100students'Names,startingwithStudentName(1,1)andendwith
StudentName(10,10).
16.3SamplePrograms
(i)DimstudentName(10)AsString
DimnumAsInteger
PrivateSubaddName()
Fornum=1To10
studentName(num)=InputBox("Enterthestudentname","EnterName","",1500,4500)
IfstudentName(num)<>""Then
Form1.PrintstudentName(num)
http://www.vbtutor.net/lesson16.html
2/3
03/09/2015
VisualBasicTutorialLesson16:Arrays
Else
End
EndIf
Next
EndSub
Theprogramacceptsdataentrythroughaninputboxanddisplaystheentriesintheformitself.Asyoucansee,thisprogramwillonlyallowsa
usertoenter10nameseachtimeheclickonthestartbutton.
ii)DimstudentName(10)AsString
DimnumAsInteger
PrivateSubaddName()
Fornum=1To10
studentName(num)=InputBox("Enterthestudentname")
List1.AddItemstudentName(num)
Next
EndSub
PrivateSubStart_Click()
addName
EndSub
TheprogramacceptsdataentriesthroughanInputBoxanddisplaystheitemsinalistbox.
<PreviousLesson><<Home>><NextLesson>
Copyright2008Dr.LiewVoonKiong.Allrightsreserved|Contact:admin@vbtutor.net
[PrivacyPolicy]
http://www.vbtutor.net/lesson16.html
3/3