user2308700
user2308700

Reputation: 67

Can you create an array of other arrays?

If I have an array of student test scores for multiple students(example: 5 students with 5 grades for each)

Dim aStudent1Grades() As New String = {Me.tboStudent1Grade1.Text, Me.tboStudent1Grade2.Text, Me.Student1Grade3.Text, Me.Student1Grade4.Text, Me.Student1Grade5.Text}

(create 4 other arrays for the other 4 students in the same fashion)

THEN I would like to create an array and store these 5 student arrays into that so I can loop through it and do all my data validation testing.

Something like:

Dim aAllGrades() As New Array = {aStudent1Grades(), aStudent2Grades(),        aStudent3Grades(), aStudent4Grades(), aStudent5Grades()}

I would use a For loop to loop through the array of arrays which will have another For loop inside that to loop through each aStudentGrade array to test the data.

Is storing arrays within another array possible? Thanks

Upvotes: 0

Views: 85

Answers (3)

D Stanley
D Stanley

Reputation: 152501

Sure - just make it a jagged array:

Dim aAllGrades()() As String = {aStudent1Grades, aStudent2Grades, aStudent3Grades}

Then you can loop through in a strongly-typed manner:

For Each a As String() in aAllGrades
    For Each aa As String in a
        Console.WriteLine(aa)
    Next
Next

Upvotes: 1

Index
Index

Reputation: 225

I'd like to suggest to sort your Students in a class:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Create an array of 5 for the Students
        Dim Students(5) As Student  'could be a List(Of Student) so you can add a new one whenever you like without any effort
        Dim i As Integer = 0 'Just an example for useage


        For j = 0 To 5
            Students(j) = New Student
        Next

        'Add the Grades
        Students(0).Grades.Add(Me.tboStudent1Grade1.Text)
        'etc

        'An example for a loop
        For Each s In Students
            For Each g As Integer In s.Grades
                i += g 
            Next
        Next


    End Sub
End Class


Public Class Student
    Public Name As String
    Public Grades As New List(Of Integer)

    Shared Sub New()

    End Sub
End Class

Upvotes: 0

William Xifaras
William Xifaras

Reputation: 5312

This is a c# example, but you should get the idea.

int[] array1= new int[4] { 44, 2, 3, 4};
int[] array2 = new int[4] { 55, 6, 33, 3};
int[] array3 = new int[4] { 77, 22, 4, 1 };
int[] array4 = new int[4] { 77, 4, 3, 3};

int[][] arrays= new int[][] {  array1,  array2,  array3,  array4 };

Upvotes: 2

Related Questions