0

I have a class called DataStorage that stores all of the publicly declared variables that I use in my program. My main class calls a public array from DataStorage and fills it with values. The main class then calls up a windows form and populates the textboxes on it with the values in the array. However, when I run the program, the form comes up with blank textboxes. I don't understand why this would be the case. I also tried populating the textboxes from the load function of the form rather than in the main class. I debugged and stepped through my code, and the main class filled the array with values fine. But when the form was called, and I started stepping through the code to populate the textboxes, it said the array was equal to nothing. If the array is stored in a separate class that both the main class and the form call, why are the values getting set to null between the main class and the form? Here is what my code looks like:

Public Class Main
    Private ds As New DataStorage
    Public Sub Test()
        For i = 0 to 2
            ds.Info(i) = "Hello"
        Next
        Form1.ShowDialog()
    End Sub
End Class

Public Class DataStorage
    Public Info() As String
End Class

Public Class Form1
Private ds As New DataStorage
Private Sub Form1_Load(sender As Object, e As EventAgrs) Handles MyBase.Load
    Textbox1.Text = ds.Info(0)
    Textbox2.Text = ds.Info(1)
    Textbox3.Text = ds.Info(2)
End Sub

I just called it Form1; the program starts with the sub Test in the main class. I just need a way to have variables/arrays that are accessible by any class or form and don't change value or get set to null unless I tell it to.

braX
  • 11,506
  • 5
  • 20
  • 33
Buster232
  • 1
  • 3
  • Possible duplicate of [What is the use of a shared variable in VB.NET?](https://stackoverflow.com/questions/613998/what-is-the-use-of-a-shared-variable-in-vb-net) – Visual Vincent Jun 12 '18 at 16:18
  • I couldn't find anything like that when I searched; I must not be putting in the right keywords. Anyway, I tried that and my program still failed at the exact same point with the same error. Right now I get a nullreferenceexception when it tries to fill the array in the Test method. – Buster232 Jun 12 '18 at 16:25
  • Please update your question with the new code. – Visual Vincent Jun 12 '18 at 17:41

1 Answers1

0

You must give your array a size before you try to fill it.

Public Class TestArray

    Private ds As New DataStorage

    Private Sub TestArray_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FillArray()
        TextBox1.Text = ds.Info(0)
        TextBox2.Text = ds.Info(1)
        TextBox3.Text = ds.Info(2)
    End Sub

    Public Sub FillArray()
        For i = 0 To 2
            ds.Info(i) = "Hello"
        Next
    End Sub
End Class

Public Class DataStorage
    Public Info(2) As String
End Class

If you don't want to give a size then use a List(Of T)

Public Class TestArray

    Private ds As New DataStorage

    Private Sub TestArray_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FillList()
        TextBox1.Text = ds.Info(0)
        TextBox2.Text = ds.Info(1)
        TextBox3.Text = ds.Info(2)
    End Sub

    Public Sub FillList()
        For i = 0 To 2
            ds.Info.Add("Hello")
        Next
    End Sub
End Class

Public Class DataStorage
    Public Info As New List(Of String)
End Class
Mary
  • 14,926
  • 3
  • 18
  • 27