Question

I have some code made by the IDE that makes the textboxes as Subs. I need to retain the value of the variable 'printString' from one Sub to another. My searching seems to indicate that the first sub needs to be turned into a function, but when I do that it breaks.

    Private Sub txtSerialNumber_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSerialNumber1.Leave
    'condition the scanned serial number
    Dim serialNumber As String = txtSerialNumber1.Text
    serialNumber = serialNumber.ToUpper()
    serialNumber = serialNumber.Trim()
    txtSerialNumber2.Focus()
    'replace tokens
    'read the label file into a string variable
    Dim printString As String = My.Resources.Fuel_Cell_Label
    'read the top position into a variable
    Dim topPosition As String = My.Settings.prntLabelTop
    'read the left position into a variable
    Dim leftPosition As String = My.Settings.prntLabelLeft
    'replace the tokens with values
    printString = printString.Replace("[[Top]]", topPosition)
    printString = printString.Replace("[[Left]]", leftPosition)
    printString = printString.Replace("[[serial_1]]", serialNumber)
End Sub

Private Sub txtSerialNumber2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSerialNumber2.Leave
    'condition the scanned serial number
    Dim serialNumber2 As String = txtSerialNumber2.Text
    serialNumber2 = serialNumber2.ToUpper()
    serialNumber2 = serialNumber2.Trim()
    'reset the front panel controls
    txtSerialNumber1.Text = ""
    txtSerialNumber2.Text = ""
    txtSerialNumber1.Focus()
    'replace the last token
    printString = printString.Replace("[[serial]]", serialNumber2)
    'print the label string
    Dim Printer As New Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6.Printer
    Printer.Print(printString)
    'MessageBox.Show(printString)
End Sub

And yes, I am new to this.

How would I keep that variable value?

Was it helpful?

Solution

The issue is one of Scope. In a nutshell in this case:

Private Sub txtSerialNumber_Leave(...

    Dim printString As String = My.Resources.Fuel_Cell_Label

printString is a local variable since you declare it (Dim) inside a procedure. This creates a new one each time. To give it module level scope, just declare it outside any of the subs:

Dim printString As String =""

Private Sub txtSerialNumber_Leave....
    ' it will retain the

End Sub

Be sure that somewhere when logical you reset it to "" or it will keep accumulating. In that example you have no idea for sure which one they will leave or if they will re-enter the text, so you will need to rethink how you are doing that. Some sort of "OK" or "Done" button which calls a sub with the code from both those events would be one way.

More about Scope in Visual Basic

OTHER TIPS

You can setup that value as a property like this:

Public Property Number() As Integer
Get
    Return _count
End Get
Set(ByVal value As Integer)
    _count = value
End Set
End Property

Then, you can call that public property anywhere in the program and know what you will get returned. If you don't have a get and set you can just return the value in the property.

If you are going to have a few of these types of variables, to keep things organized, you can create a Module and put Public variables inside the Module

Do this by clicking Project on the menu on the top of Visual Studio and then click on Add Module.

Then add the Public variable to the module:

Module Module1
     Public printString As String =""
End Module
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top