vb.net 如何在 subs 中保持变量值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19147088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:19:49  来源:igfitidea点击:

How do I keep a variable value across subs?

vb.netvisual-studio-2005

提问by

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.

我有一些由 IDE 制作的代码,使文本框成为 Subs。我需要将变量“printString”的值从一个 Sub 保留到另一个。我的搜索似乎表明第一个 sub 需要变成一个函数,但是当我这样做时它会中断。

    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?

我将如何保留该变量值?

采纳答案by ??ssa P?ngj?rdenlarp

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

问题是其中之一Scope。简而言之,在这种情况下:

Private Sub txtSerialNumber_Leave(...

    Dim printString As String = My.Resources.Fuel_Cell_Label

printStringis 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:

printString是局部变量,因为您Dim在过程中声明了它 ( )。这每次都会创建一个新的。为了给它模块级范围,只需在任何 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

更多关于Visual Basic 中的作用域

回答by Adam

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.

然后,您可以在程序中的任何位置调用该公共属性,并知道将返回什么。如果您没有 get 和 set,您可以只返回属性中的值。

回答by Daniel Gee

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.

为此,请单击 Visual Studio 顶部菜单上的“项目”,然后单击“添加模块”。

Then add the Public variable to the module:

然后将 Public 变量添加到模块中:

Module Module1
     Public printString As String =""
End Module