' Visual Basic 2008 9.0 .NET Examples - Array Datatype and Jagged Arrays - Arrays : Module Example1 Sub Main() Dim array1() As Integer = {1, 2, 3, 4, 5} Dim array2() As Integer = array1 ' (1) Console.WriteLine(array1(3)) Console.WriteLine(array2(2)) ' array1(3) = 33 Console.WriteLine(array1(3)) ' (2) Console.WriteLine(array2(3)) ' (3) ' array2(4) = 44 Console.WriteLine(array1(4)) Console.WriteLine(array2(4)) ' 'Dim array3(,) As Integer = array1 ' (4) 'Dim array4() As Short = array1 ' (5) ' Dim array5(,) As Byte = {{1, 2}, {3, 4}} ' Dim array6 As Array = array5 ' (6) Console.WriteLine(array6(1, 1)) ' array6 = array2 ' (7) Console.WriteLine(array6(4)) ' Console.ReadLine() End Sub End Module Module Example2 Sub Main() Dim names() As String = {"Brussels", "Antwerp", "Ghent"} Dim zipCodes() As Integer = {1000, 2000, 0} ' Dim cities(1) As Array cities(0) = names ' (1) cities(1) = zipCodes ' (2) ' or : Dim cities As Array() = {names, zipCodes} ' Console.WriteLine(names(1)) ' (3) Console.WriteLine(cities(0)(1)) ' (4) ' cities(1)(2) = 9000 Console.WriteLine(zipCodes(2)) ' (5) ' Console.ReadLine() End Sub End Module Module ExerciseSolution Sub Main() Console.Write("Student Count ? ") Dim studentCount As Integer = Console.ReadLine() Console.Write("Topic Count ? ") Dim topicCount As Integer = Console.ReadLine() Console.WriteLine() ' Dim studentUpperbound As Integer = studentCount - 1 Dim names(studentUpperbound) As String Dim topicUpperbound As Integer = topicCount Dim scores(studentUpperbound, topicUpperbound) As Double Dim students(1) As Array Const namesIndex As Integer = 0, scoresIndex As Integer = 1 students(namesIndex) = names students(scoresIndex) = scores ' Dim studentIndex, topicIndex As Integer For studentIndex = 0 To studentUpperbound Console.Write("Name Student " & (studentIndex + 1) & " ? ") students(namesIndex)(studentIndex) = Console.ReadLine() For topicIndex = 0 To topicUpperbound - 1 Console.Write(students(namesIndex)(studentIndex) & _ " Score Topic " & (topicIndex + 1) & " ? ") students(scoresIndex)(studentIndex, topicIndex) = _ Console.ReadLine() Next Console.WriteLine() Next ' For studentIndex = 0 To studentUpperbound For topicIndex = 0 To topicUpperbound - 1 students(scoresIndex)(studentIndex, topicUpperbound) += _ students(scoresIndex)(studentIndex, topicIndex) Next students(scoresIndex)(studentIndex, topicUpperbound) /= _ topicUpperbound Next ' Console.WriteLine() For studentIndex = 0 To studentUpperbound Console.WriteLine("Name : " & students(namesIndex)(studentIndex)) For topicIndex = 0 To topicUpperbound - 1 Console.WriteLine("Score Topic " & (topicIndex + 1) & " : " & _ students(scoresIndex)(studentIndex, topicIndex)) Next Console.WriteLine("Average Score : " & _ students(scoresIndex)(studentIndex, topicUpperbound)) Console.WriteLine() Next ' Console.ReadLine() End Sub End Module ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.