Question Global array problem

ucp8

Active member
Joined
Feb 9, 2009
Messages
28
Programming Experience
3-5
Hi,

I am developing a piece of software using VB.NET which allows teachers to test and monitor their students progress. The problem I am facing is that I am using a Module to hold the questions array variable:

Friend Questions() As String

When I attempt to apply a value to each of the array variables, I get the followng error:

Object reference not set to an instance of an object.

I am using the following code to do this:

For k = 0 To 9
Questions(k)=questionDs.Tables("TestQuestions").Rows(k).Item("Question").ToString
Next


I do not understand why it wont apply the values given to it for each of the array variables. If I make Questions() into a standard String using Friend Questions As String , it will apply the value.

I understand that I could create 10 seperate Questions String variables and apply a value to each one but I am trying to cut down on my LOC. Can anyone tell me why I cannot set each array value or how to fix my code to enable this?
 

ucp8

Active member
Joined
Feb 9, 2009
Messages
28
Programming Experience
3-5
After reading my post I found a solution. I noticed that no array size had been set in the variable declaraton. I changed the code in the Module to Friend Questions(10) As String and it worked.
 

ucp8

Active member
Joined
Feb 9, 2009
Messages
28
Programming Experience
3-5
I have never come across Lists, what advantages would they provide over the String array? Thanks for your help
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
Collections/lists are better used when you need to dynamically add/remove/insert items.
 

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
VB.NET:
Friend questions As New List(Of String)

VB.NET:
For k As Integer = 0 To 9
     questions.Add(questionDs.Tables("TestQuestions").Rows(k).Item("Question").ToString())
Next
 
Top Bottom