Question Passing Variables between Subs...

ndavenport

Member
Joined
Sep 2, 2009
Messages
8
Programming Experience
Beginner
I'm having an issue where I need to pass the value in a variable from one sub to another. Here is the scenario. I have two forms; Main_Form and frmClient. I take a value from frmClient and pass it into a sub called Form_Activate located in Main_Form. I then need to take the value from Form_Activate and use it in a sub called when the user selects a menu item in the toolstrip.

Starting point is frmClient and the code there is this:

Private Sub Button1_Click(ByVal sender as System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Client As Integer
Client = cmbClient.SelectedValue
Main_Form.Form_Activate(Client)

End Sub

The value gets passed to the Main_Form sub names Form_Activate. This is working correctly and the value shows up as expected.

Public Sub Form_Activate(ByVal ClientReceived As Integer)

pintClient = ClientReceived

End Sub

Now here is where I run into the problem. I need to carry the value stored in pintClient to another sub on the form named UpdateDatabaseToolSTripMenuItem. This sub is called when the user clicks a menu item named Update Database.

Private Sub UpdateDatabaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateDatabaseToolStripMenuItem.Click

'some code

End Sub

If I attempt to use the variable pintClient directly, it has a value of 0 by the time it is called in the UpdateDatabaseToolStripMenuItem_Click Sub.
 
Last edited:
You need to declare the variable with the Public keyword instead of Dim. Then use the fully-qualified name of the variable, which includes the name of the form when accessing it from another form. For example:

In Form1:

Public mydat As String
mydat = "whatever"


In Form2:

Dim value As String
value = Form1.mydat


Apply this general principle to your own code.
 
pintClient is a public variable, but when the second sub is called through the menu item click, it contains no data. It does contain data when originally set in the Form_Activate() sub. Both subs are on the same form.

As soon as the UpdateDatabaseToolStripMenuItem_Click sub is called, the pintClient variable resets back to 0 instead of maintaining it's value.
 
Last edited:
You need to declare the variable with the Public keyword instead of Dim. Then use the fully-qualified name of the variable, which includes the name of the form when accessing it from another form. For example:

In Form1:

Public mydat As String
mydat = "whatever"


In Form2:

Dim value As String
value = Form1.mydat


Apply this general principle to your own code.
That's what properties are for, instead of declaring the variable itself as public/friend you instead create a public/friend property.
 
So I tried to store the value in a label as a temporary fix to try to see better what is going on. When the UpdateDatabaseToolStripMenuItem_Click is invoked, it clears the text value in the label as well.

So instead of the variable in the Form_Active sub, I populated the text of the label. Not sure why the data returned from the Form_Active sub gets wiped out by the UpdateDatabaseToolStripMenuItem_Click sub. Clearing the value is the first thing that happens when run. :(
 
delete the bin and obj folders from the project, then zip the project and attach it to a post, something's not adding up.
 
Yeah, something's definitely not adding up. How could even the values in the label be reset. Unless you close the form...
 
Yeah, it really got me crossed...I decided to change routes and put the control on the main form. I was concerned about how this would look but I think I'm okay with it. Now it is working as well. Not sure what was going on, but I think at this point I'm moving on. Thanks for all the great feedback! :cool:
 
Back
Top