ER231519
ER231519

Reputation: 3

VB.net Splitting a 1D array down into a 2D array

I am currently writing a (multiple choice) quiz program using VB.net I have read the answer options in from a .txt file into a single dimension temporary array however I would like to split this down into each question and the answer options for each questions into a 2D array so that (0,0) would be option A for question one and then (0,1) would be Option B for question 1 etc and then (1,0) would be option A for question 2. Below is the method I have tried however I get the error: "Object reference not set to an instance of an object" when adding 1 to a variable when I try and run this section of the code optnum = optnum + 1 Any help would be greatly appreciated, either fixing my code below or suggesting another method.

    Dim optnum As Integer
    Dim tempq As Integer = 0
    Gameload(Phase_3.sounds)
    Questionnum = 0
    L_start.Hide()
    optnum = 0
    'splits the temp array down into options for each question
    For i = 0 To 39

        questions(tempq, optnum) = temparray(i)
        optnum = optnum + 1
        'there are 4 options for each question
        'moves on to the next question when there is 4 options in the question
        If optnum = 3 Then
            tempq = tempq + 1
            optnum = 0
        End If
    Next

    For i = 0 To 3
        L_option1.Text = questions(0, i)
    Next
    question_set()

Edit: Here is the new full code I am still getting the error: Object reference not set to an instance of an object but now at the next in this section of code. '''
For optnum = 0 To 3 questions(i, optnum) = temparray(i * 4 + optnum) Next ''' Thank you for all the help so far

''' Public Class Game

Dim submission As Integer
Dim Correct_ans As Integer
Dim temparray() As String
Dim questions(,) As String
Dim Questionnum As Integer
Dim rs As New Resizer
Private Sub Game_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'finds all components on the screen in preperation if the screen resizes
    rs.FindAllControls(Me)
    L_start.Show()
End Sub
Private Sub Game_Resize(sender As Object, e As EventArgs) Handles Me.Resize
    'resizes all components on the screen to same proportions
    rs.ResizeAllControls(Me)
End Sub

Sub Gameload(ByVal sounds As String)

    Dim pack As String

    'reads in the sound pack
    pack = My.Resources.ResourceManager.GetString(sounds)
    Phase_3.Close()
    'splits the pack into an array so that it can be broken down into questions
    temparray = pack.Split(","c)


End Sub

Sub L_start_Click(sender As Object, e As EventArgs) Handles L_start.Click
    Dim optnum As Integer
    Dim tempq As Integer = 0
    Gameload(Phase_3.sounds)
    Questionnum = 0
    L_start.Hide()
    optnum = 0
    'splits the temp array down into options for each question
    For i = 0 To temparray.Count / 4
        For optnum = 0 To 3
            questions(i, optnum) = temparray(i * 4 + optnum)
        Next
    Next

    For i = 0 To 3
        L_option1.Text = questions(0, i)
    Next

End Sub

'''

Upvotes: 0

Views: 89

Answers (2)

Phoenix Stoneham
Phoenix Stoneham

Reputation: 147

Where's your declaration for temparray and questions. Will the quiz always have 4 choices and 10 questions?

It might be easier for you to have a nested loop so that i is temparray.count/4 and optnum is 0-3, then you can populate questions(i, optnum) = temparray(i*4+optnum)

Looking at your new code, you haven't initialised the questions variable. Only declared it.

Personally I would change questions to a list(of question), then use Mary's answer.

Upvotes: 0

Mary
Mary

Reputation: 15091

This is just my opinion but I think your aproach would be hard to maintain. I never saw the value of a class until I actually tried one. Maybe this is your chance.

Public Class Quiz
    Public Property QuestionNumber As Integer
    Public Property Question As String
    Public Property AnswerA As String
    Public Property AnswerB As String
    Public Property AnswerC As String
    Public Property AnswerD As String
    Public Property CorrectAnswer As String

    Public Overrides Function ToString() As String
        Return $"Question: {QuestionNumber}.{Question} Answer Choices: {AnswerA}, {AnswerB}, {AnswerC}, {AnswerD} Correct Answer - {CorrectAnswer}"
    End Function
End Class

To use your class...

Private QuestionList As New List(Of Quiz)

Private Sub OPCode2()
    Dim temparray = File.ReadAllLines("answers.txt")
    Dim temparraylocation As Integer
    Dim questions = File.ReadAllLines("questions.txt")
    Dim correctAnswers = File.ReadAllLines("correct.txt")
    For i = 0 To questions.Length - 1
        Dim qu As New Quiz
        qu.QuestionNumber = i + 1
        qu.Question = questions(i)
        qu.CorrectAnswer = correctAnswers(i)
        qu.AnswerA = temparray(temparraylocation)
        temparraylocation += 1
        qu.AnswerB = temparray(temparraylocation)
        temparraylocation += 1
        qu.AnswerC = temparray(temparraylocation)
        temparraylocation += 1
        qu.AnswerD = temparray(temparraylocation)
        temparraylocation += 1
        QuestionList.Add(qu)
    Next
    For Each q In QuestionList
        Debug.Print(q.ToString)
    Next
End Sub

Upvotes: 1

Related Questions