Membuat array di vb.net
untuk mendeklarasikan sebuah array di vb.net silahkan ligat contoh berikut :Module Module1
Sub Main()
Dim Array1(10) ' array ini akan memiliki element sebanyak 11
Dim Array2(15) As String 'Array yang memiliki16 element string
Dim Array3(10, 20) As Integer 'Array 2 dimensi
Dim Array4(10, 100) 'Array 2 dimensi
End Sub
End Module
Contoh penggunaan Array dalam aplikasi console
Module Module1
Sub Main()
Dim Array1() As Integer = {12, 16, 20, 24, 28,
32} ' array ini akan memiliki element sebanyak 11
Dim Array2() As String = {"Harison", "Sector Code", "Ganteng",
"Banget"} 'Array yang memiliki16 element string
End Sub
End Module
Module Module1Untuk referensi silahkan lita tutorial berikut http://www.tutorialspoint.com/vb.net/vb.net_arrays.htm
Sub Main()
Dim x(10) As Integer ' n is an array of 11 integers '
Dim y, z As Integer
' initialize elements of array n '
For y = 0 To 10
x(y) = y + 100 ' set element at location i to i + 100
Next y
' output each array element's value '
For z = 0 To 10
Console.WriteLine("Element({0}) = {1}", z, x(z))
Next z
Console.ReadKey()
End Sub
End Module
dan dalam aplikasi console diatas akan menghasilkan pesan :
Element(0) = 100
Element(1) = 101
Element(2) = 102
Element(3) = 103
Element(4) = 104
Element(5) = 105
Element(6) = 106
Element(7) = 107
Element(8) = 108
Element(9) = 109
Element(10) = 110
Declare Dynamic Arrays Vb.NET
Mendeklarasikan Arrays Dynamic vb.net bisa dengan menggunakan pernyataan ReDim. lihat contoh sytax ReDim berikut:ReDim [Preserve] arrayname(subscripts)
Module Module1Aplikasi diatas akan menghasilkan pesan :
Sub Main()
Dim SC() As Integer
ReDim SC(2)
SC(0) = 85
SC(1) = 75
SC(2) = 90
ReDim Preserve SC(10)
SC(3) = 80
SC(4) = 76
SC(5) = 92
SC(6) = 99
SC(7) = 79
SC(8) = 75
For i = 0 To 10
Console.WriteLine(i & vbTab & SC(i))
Next i
Console.ReadKey()
End Sub
End Module
0 85
1 75
2 90
3 80
4 76
5 92
6 99
7 79
8 75
9 0
10 0
Multi-Dimensional Arrays
Berikut penggunaan Multi-Dimensional Arrays dalam vb.netModule Module1Maka akan menghasilkan pesan :
Sub Main()
' an array with 5 rows and 2 columns
Dim a(,) As Integer = {{0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}}
Dim i, j As Integer
' output each array element's value '
For i = 0 To 4
For j = 0 To 1
Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i, j))
Next j
Next i
Console.ReadKey()
End Sub
End Module
a[0,0] = 0
a[0,1] = 0
a[1,0] = 1
a[1,1] = 2
a[2,0] = 2
a[2,1] = 4
a[3,0] = 3
a[3,1] = 6
a[4,0] = 4
a[4,1] = 8
Jagged Array dalam Vb.Net
Syntaxnya :Dim scores As Integer()() = New Integer(5)(){}
Module Module1Maka akan menghasilkan pesan
Sub Main()
'a jagged array of 5 array of integers
Dim x As Integer()() = New Integer(4)() {}
x(0) = New Integer() {0, 0}
x(1) = New Integer() {1, 2}
x(2) = New Integer() {2, 4}
x(3) = New Integer() {3, 6}
x(4) = New Integer() {4, 8}
Dim y, z As Integer
' output each array element's value
For y = 0 To 4
For z = 0 To 1
Console.WriteLine("a[{0},{1}] = {2}", y, z, x(y)(z))
Next z
Next y
Console.ReadKey()
End Sub
End Module
a[0,0] = 0Buat referensi : http://www.tutorialspoint.com/vb.net/vb.net_arrays.htm
a[0,1] = 0
a[1,0] = 1
a[1,1] = 2
a[2,0] = 2
a[2,1] = 4
a[3,0] = 3
a[3,1] = 6
a[4,0] = 4
a[4,1] = 8
Jangan sungkan-sungkan untuk bertanya pada kotak komentar dibawah ini ya. terima kasih :)