VB .
NET I
Chapter Two
Handling Data
1
Chapter 2 lesson 2
✓Arrays
✓Structures
2
Arrays
• A simple or scalar variable is one that is
unrelated to any other variable in memory
• An array is a group of variables that have the
same name and data type and are related in
some way
3
Kinds of Arrays
• The most commonly used arrays are one-
dimensional and two-dimensional
• Programmers use arrays to store related data
in the internal memory of the computer
4
One-Dimensional Arrays
• A one-dimensional array is simply a row (or
column) of variables
• Each element in an array is identified by a
subscript, which Visual Basic .NET assigns to
the variable when the array is created
Example:
Dim listofnames(4) As String
5
Syntax One-Dimensional Arrays
• Dim listofnames(4) As String
• listofnames(0) = "ali asad"
• listofnames(1) = "abdi ali"
• listofnames(2) = "kadra nor"
• listofnames(3) = "xawo cabdi"
• listofnames(4) = "qeys jamac"
•
6
Cont..
• [Link](listofnames(0))
• [Link](listofnames(1))
• [Link](listofnames(2))
• [Link](listofnames(3))
• [Link](listofnames(4))
7
Two-Dimensional Arrays
• A two-dimensional array resembles a table in
that the variables are in rows and columns
• Each variable (element) in a two-dimensional
array is identified by a unique combination of
two subscripts
8
Syntax Two-Dimensional Arrays
• Dim twod(2, 2) As Integer
• twod(0, 0) = 30
• twod(0, 1) = 28
• twod(0, 2) = 26
• twod(1, 0) = 24
• twod(1, 1) = 22
• twod(1, 2) = 20
• twod(2, 0) = 18
• twod(2, 1) = 16
• twod(2, 2) = 14
9
Cont..
• MsgBox(twod(0, 0))
• MsgBox(twod(0, 1))
• MsgBox(twod(0, 2))
• MsgBox(twod(1, 0))
• MsgBox(twod(1, 1))
• MsgBox(twod(1, 2))
• MsgBox(twod(2, 0))
• MsgBox(twod(2, 1))
• MsgBox(twod(2, 2))
10
Structures
• Structure statement: can be used to create your own
data types in Visual Basic .NET
• Data types created using the Structure statement are
referred to as user-defined data types or structures
• Members included in the structure can be variables,
constants, or procedures
• In most cases, members are variables
11
Structure
• Structure allows you to combine multiple values of
the basic data types and handle them as a whole. To
define a Structure in VB 2010, use the Structure
statement, which has the following syntax:
Structure structureName
Dim variable1 As varType
Dim variable2 As varType…
Dim variablen As varType
End Structure
12
Structure
• VarType can be any of the data types supported
by the CLR or the name of another Structure that
has been defined already. The Dim statement
can be replaced by the Private or Public access
modifiers. For Structures, Dim is equivalent to
Public. After this declaration, you have in
essence created a new data type that you can use
in your application. structureName can be used
anywhere you’d use any of the base types
(Integers, Doubles, and so on). You can declare
variables of this type and manipulate them as you
manipulate all other variables (with a little extra
typing). 13
Syntax User program
• Structure employee
• Dim empid As Integer
• Dim empname As String
• Dim empsalary As Double
• Dim emhdate As Date
End Structure
14
Syntax Procedure
• Dim empvar As employee
• [Link] = 1
• [Link] = "ali"
• [Link] [Link] = 50.5
• = #9/18/2017#
• MsgBox("employee id" & [Link] &
• "employee name" & [Link] &
• "employee sarary" & [Link] &
• "employee hire date" & [Link])
15
16