I do not know when these variables are emptying.

mechwarrior3

Well-known member
Joined
Dec 19, 2005
Messages
67
Programming Experience
Beginner
Alright,

Here is the situation: I have a main form (Form1 (Yes, I know the naming convention for the forms are horrible.)). This Form has two fields filled out and once those fields are filled by the user, the main form makes a call to another form (Form3). This form has several text boxes that are filled by the user. Then the user clicks the OK button. That triggers all of the values in the text boxes to be saved into an array of strings. Then, a call is made to a friend sub routine in the main form. That friend sub routine passes the String array parameter to the main form and saves each string in the array to a new variable. The size of the array is hard coded (albeit, rather poorly) so the amount of necessary variables is known. Once the friend sub routine concludes, Form3 closes and we are returned to the Form1. When I click on the Save button on Form1, I look and all of those string variables that I just set with the friend sub routine are empty. All of them. Nothing left. What is going on? It seems like they are being emptied between the conclusion of the friend sub routine and the closing of Form3. Is this correct? If so, how do I fix that?

Here is the code for Form3 (all of it) There are 12 text boxes and an OK button:
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] cmdPNumOK_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] cmdPNumOK.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] frm1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Form1
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] PNumArray() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = {"", "", "", "", "", "", "", "", "", "", "", ""}
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum1.Text, "000"), 0)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum2.Text, "000"), 1)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum3.Text, "000"), 2)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum4.Text, "000"), 3)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum5.Text, "000"), 4)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum6.Text, "000"), 5)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum7.Text, "000"), 6)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum8.Text, "000"), 7)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum9.Text, "000"), 8)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum10.Text, "000"), 9)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum11.Text, "000"), 10)
PNumArray.SetValue(DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum12.Text, "000"), 11)
frm1.AssignPNumbers(PNumArray)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] DefaultPNum([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] txtString [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] txtDefault [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] (txtString = "") [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] txtDefault
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] txtString
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form3_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
DefaultSettings()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DefaultSettings()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtPNum1.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][COLOR=black]
[/COLOR][/COLOR][/SIZE]

Here is the friend sub routine, AssignPNumbers (the 12 strPNums do exist as global variables on Form1):
VB.NET:
[/COLOR][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Friend[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] [COLOR=black]AssignPNumbers([/COLOR][/SIZE][COLOR=black][SIZE=2][COLOR=blue]ByVal[/COLOR][/SIZE][SIZE=2] PNumArray() [/SIZE][/COLOR][COLOR=blue][SIZE=2]As[/SIZE][SIZE=2] [/SIZE][SIZE=2]String[/SIZE][/COLOR][SIZE=2][COLOR=black])[/COLOR]
[COLOR=black]strPNum1 = PNumArray(0)[/COLOR]
[COLOR=black]strPNum2 = PNumArray(1)[/COLOR]
[COLOR=black]strPNum3 = PNumArray(2)[/COLOR]
[COLOR=black]strPNum4 = PNumArray(3)[/COLOR]
[COLOR=black]strPNum5 = PNumArray(4)[/COLOR]
[COLOR=black]strPNum6 = PNumArray(5)[/COLOR]
[COLOR=black]strPNum7 = PNumArray(6)[/COLOR]
[COLOR=black]strPNum8 = PNumArray(7)[/COLOR]
[COLOR=black]strPNum9 = PNumArray(8)[/COLOR]
[COLOR=black]strPNum10 = PNumArray(9)[/COLOR]
[COLOR=black]strPNum11 = PNumArray(10)[/COLOR]
[COLOR=black]strPNum12 = PNumArray(11)[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][COLOR=black]

Any help is greatly appreciated. :)
[/COLOR][/SIZE]
 
Okay. Now I'm trying something a little different. Instead of using a friend sub routine, I've defined a public property on Form1.

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] GetPNumbers() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].strPNumbers
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Value [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].strPNumbers = Value
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Set
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][COLOR=black]
[/COLOR][/COLOR][/SIZE]

Form3 calls to this in the cmdPNumOK_Click sub routine after assigning all the values to PNumArray. Now, I use the Set part of this property to set the Public variable (strPNumbers (defined as an array of strings)) to the value of PNumArray. Then, Form3 closes. When, the code returns to Form3, I lose all information stored within strPNumbers. Why?! I am getting so frustrated. The data is not actually being saved anywhere. I can't set the values from Form3 to anything in Form1!

What is going on? Why is the data being reset from when the values are set in my public property GetPNumbers to when the code returns to Form3? strPNumbers is the only data that gets screwed up. PNumArray does not. It stays unchanged as the code goes from Form3 to Form1 and back again. What is going on? Please help.

Note: I am using Form.ShowDialog(Me) to display Form3.
 
There are many ways to accomplish your goal, the best depends on the architecture of the app. I'll show you one. To find about the others, follow the link in my signature titled 'Multiple Forms' and read all 4 articles.

The problem you're having is the very first line of code in the cmdPNumOK_Click procedure: Dim frm1 As New Form1, you're creating a new instance of Form1. So the array is being stored in the new Form1 instead of the original instance of Form1.
For the following code to work, on Form3 set the OK button's DialogResult property to 'OK'.
In Form1:
VB.NET:
    Private Sub openForm3()
        Dim frm3 As New Form3
        If frm3.ShowDialog = Windows.Forms.DialogResult.OK Then
            strPNum1 = DefaultPNum(frm3.txtPNum1.Text, "000")
            strPNum2 = DefaultPNum(frm3.txtPNum2.Text, "000")
            ' ...
        End If
    End Sub

    Public Function DefaultPNum(ByVal txtString As String, ByVal txtDefault As String) As String
        If txtString.Trim = String.Empty Then
            Return txtDefault
        Else
            Return txtString
        End If
    End Function
 
Last edited:
Back
Top