Replies To: Need helpp Score Function in Quiz Software

Re: Need helpp Score Function in Quiz Software

Posted 11 October 2007 - 05:50 AM

To be honest with you I wouldn't use ViewState for this anyways. I would use an Array and pass it from page to page. On the first page create your 2 dimensional array

'I initialized it to one for each dimension
'You would want to initialize to how many questions and
'answers you would have for that page
Dim quizArray As Array = Array.CreateInstance(GetType(Integer), 1,1)

Then on each subsequent page, I would use ReDim Preserve to increase the size of the array, like so

Public Sub ResizeArray(size)
'Create your new array based on the
'array passed to the method
Dim quizArray As Array = _quizArray
'Resize the array to the size of the current array
plus the number of elements you want to resize by
ReDim Preserve quizeArray(array.GetUpperBounds(_quizArray)+size,array.GetUpperBounds(_quizArray)+size)
End Sub

When you resize an array, like we've done above, all data is wiped from the array, and the array is increased by the size you specify. To prevent this we use ReDim Preserve which allows you to resize the array and preserve the data currently in the array.

Now the size attribute we're passing to our method. Lets say on page 1 you have 10 questions, you would initialize your 2 dimensional array to the size of 10 (you see 9 as the size but arrays are 0 index based)

Then you add your values to it when they complete all the questions and click the next button

quizArray.Add(question1,answer1)
......
quizeArray.Add(questionN,answerN)
'set the property of the array property you've set on page 2
Page2.quizArray = quizArray
'Say you have 10 questions on the next page
'Pass the number 10 to your procedure
Page2.ResizeArray(10)

Where question1 and answer1 are your values from your quiz. Then on the 2nd page create a public array property

Dim _quizArray As Array
Public Property quizArray As Array
Get
Return _quizArray
End Get
Set(value As Array)
_quizArray = value
End Set
Ed Property

Once you call the ResizeArray method on page 2 it resizes your array while keeping the values from page 1 in it. Do this for every page in your quiz, then when you get to the last page you'll have a 2 dimensional array populated with all your questions/answers.