Resolved Using one array in multiple forms project

Danielss

Member
Joined
Mar 22, 2010
Messages
20
Programming Experience
1-3
I declare an array on one form that i'd like to use on another form.
I declare it public in the declarations area and edit it later within a Private sub. However, when i reference it from another form it's as if i haven't editted it at all and it is only grabbing the initial declaration. Since i'm certain that i'm not being very clear, i've included an example of what i mean below:


VB.NET:
Public Class form1

Public Array() As Integer

    Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button.click
        Redim Array(1)
        Array(0) = 5
        Array(1) = 2
       
        End Sub
End Class

Now let's say on another form that is a child of this one I try to use this array. I'll do something like:

VB.NET:
Form1.array(0)

OR

VB.NET:
UBound(Form1.array,1)

In both cases i get an error that basically complains because it is only looking at the line 'Public Array() As Integer'

If i change it to something like 'Public Array(1) As Integer' isn't helpful either.

What can i do to make my changes stick?
 
Last edited:
Because the way that you are referencing the the array is not correct for what you want to do.

VB.NET:
Form1.array(0)

will reference the uninitialized variable for the Form1 class. What you want to reference is the instance of that class.

VB.NET:
Public Class frm1
     Public aryOfInts() As Integer

     Public Sub frm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          aryOfInts = new Integer() { 1,2,3,4}
     End Sub

     Public Sub btnOpenNewForm_Click(Byval sender As Object, Byval As System.EventArgs) Handles btnOpenNewForm.Click
         Dim NewFormToOpen As frm2
         NewFormToOpen.Show(Me)
     End Sub
End Class

Now for form 2

VB.NET:
Public Class frm2

     Public Sub frm2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          'need to get the instance of frm1 
          'if we try to do frm1.aryOfInts(1) there is no instance of this variable
          Dim thisFormTempArray() As Integer = DirectCast(Me.Owner, frm1).aryOfInts
     End Sub

End Class

When we did NewFormToOpen.Show(Me), this says that NewFormToOpen is the instance of frm2 that we are going to show. By saying .Show(Me) we are saying that when NewFormToOpen instance opens, it's owner will be the instance of frm1. This helps because in the frm2 code we can get the instance of frm1 by accessing frm2 Owner property.
 
ok, thanks. Unfortunately, i don't see how i can adapt this to my real scenario as my code for adding information to the array is quite complex and i wasn't anticipating a solution like this.

Instead, I'll change the situation a little and i hope someone can help me find a simpler solution. Let's say i have a 3 dimensional array that is declared within a private sub, it's redim'd later and then values get added to it. At the end of the sub i have all the information i need. How can i copy that array to one i can access from another form?

Again, vague example of what i'm doing below

VB.NET:
Public Class frm1
     'What would i need to put here?

     Public Sub button_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button.click

          Dim array(,,) As Double
          'some code

          Redim array(,,)
          'lots and lots more code that i'd rather not touch and then i have a large array full of data at the end

          'what can i put here to copy the array to a new array that can be accessed outside of this sub and form

     End Sub


End Class

Thanks for any help!
 
Declare your array as public in your class.

Public Array() as Double

Access and manipulate your array data however you want in your procedures.

Then, if you want to use this data in another class, you must either pass the array as a parameter to an instance of the other class via a property or sub etc.

Alternatively, use a global variables class that can be accessed by all of your forms that share data.
 
Ok, so you would suggest having a class that i use just to send variables to and then request variables from? I like the sound of that. How can i do it?
 
I usually do something along the lines of this:

VB.NET:
Public Class globals
    Public mainID As New OTGShared.globalString(Nothing)
    Public user As String

    'Add all shared variables here

End Class

Then in my main form, I'll declare an instance of this globals class.

VB.NET:
Public Class mainForm
    Public gObj As New globals

    'form code

End Class

Then from other classes that are to access the shared data, I do this.

VB.NET:
Public Class secondaryForm
    Private withevents mainID As OTGShared.globalString(Nothing) = mainForm.gObj.mainID

End Class

I do this, so that I can select which global variables I want my new class to "watch".

Then, for example, when mainForm.gObj.mainID is changed, I can handle an event in my secondaryForm to update the GUI with the new string.

However you do it will depend on what your doing. This is just my usual method.
 
Ok, so it seems that the situation is more ridiculous than i first thought.
All over my project i've used 'form.variable' to reference something from a previous form without any problems. form1.listbox1.selectedindex etc. It all works fine.

The only difference is in this case is that i make the parent form store a 3 dimensional array containing ~ 2 million values as the Double data type.
Is it possible that in this case, visual basic is deciding it's better to forget everything on this form when it loads another to free up memory?
 
It is relevant because you are trying access one forms objects from another form. In which case you have to deal with the instances of the forms, getting a basic understanding of instance versus class is essential.

I am sure you have heard the car speech before but I am going to try and simplify it.

We have a Car (Form1) [Class].
A Car has a steering mechanism, wheels, door (txtEnterName, aryOfInts, txtEnterAge) [Class objects].

I have a Car (frmNewForm)[Instance of the Class].
My Car has 4 wheels (aryOfInts{1,2,3,4}) [Instance].
You have a Car (frmNewFormAgain)[Instance of the Class].
Your Car has 6 wheels (aryOfInts{1,2,3,4,5,6}) [Instance].

If I want to see how many wheels your car has, I do not look at Car (Class) I look at Your Car (frmNewFormAgain) which is your instance of the car class.

Car.wheels does not have any information because it hasn't been given any yet.

frmNewFormAgain.wheels does have a value of 6 integers because as a part of it's instance we defined that it contains the integers 1 - 6.

My Car needs to be able to have a reference (see) to Your Car in order to be able to see that it has 6 wheels.

How does this apply to the question at hand?

We have 2 forms frmOne and frmTwo -- this is their classes

VB.NET:
Public Class frmOne

    Public aryOfInts() As Integer

    Private Sub frmOne_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'fills the aryOfInts
        aryOfInts = New Integer() {1, 2, 3, 4, 5, 6}

    End Sub

    Private Sub btnOpenForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenForm2.Click
        'Make an Instance of second form
        Dim frm2 As New frmTwo

        'Set Owner and show the new form
        frm2.Show(Me)

    End Sub
End Class

VB.NET:
Public Class frmTwo

    Private ints() As Integer


    Private Sub frmTwo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'because the instance of frmTwo opened from frmOne is owned by the instance of frmOne 
        ' we can use the Owner property to get a reference to the instance on this form

        Dim frm As frmOne = DirectCast(Me.Owner, frmOne)

        'sets this forms instance of it's integer array to same as the our temporary instance of the frmOne instance
        ints = frm.aryOfInts

        'this will not have a value because frmOne is the Class and aryOfInts is not an instantiated object in the class
        Dim newInt() As Integer = frmOne.aryOfInts

    End Sub
End Class
 
right, whilst fiddling with something else i've tripped over the answer to why it worked fine so many times but not in the case i wanted it to.
I'll try and explain as best i can for the general case in the hope that nobody else will make the error i have.

-Form1 has a button to open Form2 which has a button to open Form3.

-Form3 attempts to access a global array from Form2. This array is declared in the declarations area, but values are added to the array from within a sub (form load or whatever, i don't think it's important). Using Form2.array()


What i'd done the single time it didn't work was create a new instance of Form2 by doing this

VB.NET:
        Dim anotherform As New Form2
        anotherForm.Show()

Then all my references to the array in Form3 were actually pointing at Form2 instead of where they should have been pointing 'anotherForm'


Basically, i'm a bit of a failure and didn't really notice what i'd done until i went to edit something on the top level form. Thanks to everyone who tried to help!
 
Back
Top