vb.net 如何访问私有子程序中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19149978/
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:20:01  来源:igfitidea点击:

how to access variables in private subs

vb.net

提问by SkyVar

OK this may be a simple but I can't find the answer anywhere. I'm trying to create a program and I need to access my variables in a private sub from another sub. I'm very new to VB. I also can't figure out why I can't access my orgVisttext box from the getInputsSub.

好的,这可能很简单,但我在任何地方都找不到答案。我正在尝试创建一个程序,我需要从另一个子程序访问私有子程序中的变量。我对 VB 很陌生。我也无法弄清楚为什么我无法orgVistgetInputsSub访问我的文本框。

Private Sub displayBtn_Click(sender As Object, e As EventArgs) Handles displayBtn.Click

        Dim organization As String
        Dim date as String
        Dim location As String
        Dim MandEexp As Decimal
        Dim airFareExp As Decimal
        Dim lodging As Decimal
        Dim taxi As Decimal

    End Sub

    Sub getInputs(ByRef organization As String, ByRef date as String, ByRef location As String, ByRef MandEexp As Decimal, ByRef airFareExp As Decimal,
                  ByRef lodging As Decimal, ByRef taxi As Decimal)

        organization = orgVistTB.text


    End Sub

Private Sub orgVisitTB_TextChanged(sender As Object, e As EventArgs) Handles orgVisitTB.TextChanged

    End Sub

回答by OneFineDay

Variables inside of a Sub are only available inside of it's sub. Since you not calling this sub you are not able to access the textbox. You can't use dateas a variable name - it is reserved for the Datedatatype. Your usage with this does not make since with your limited info on what your doing and your requirements. You can move them outside of this sub and have Classlevel variables if you need them to be available for other methods and the values be retained for later use.

Sub 内部的变量仅在其子内部可用。由于您没有调用此子程序,因此您无法访问文本框。您不能date用作变量名 - 它是为Date数据类型保留的。由于您对您所做的事情和您的要求的信息有限,您对此的使用不会产生影响。Class如果您需要它们可用于其他方法并且保留值以供以后使用,您可以将它们移到此子之外并具有级别变量。

Private Sub displayBtn_Click(sender As Object, e As EventArgs) Handles displayBtn.Click

    Dim organization As String
    Dim _date as String
    Dim location As String
    Dim MandEexp As Decimal
    Dim airFareExp As Decimal
    Dim lodging As Decimal
    Dim taxi As Decimal
    getInputs(organization, _date, location, MandEexp, airFare, lodging, taxi)
    'even if you set the variables inside this sub, if you don't
    'use then afterwards they lose scope and are garbage collected
End Sub

回答by Uriahs Victor

You need to create a public variable at the very top of your form code just after the class, but not assign it a value. Then you use that public variable inside the private subs. You will be able to get the data within them now.

您需要在类之后的表单代码的最顶部创建一个公共变量,但不要为其分配值。然后您在私有子程序中使用该公共变量。您现在将能够获取其中的数据。

create a form with 2 buttons and a label then name them accordingly (Mybtn_Click, Mybtn2_Click). Use my code and you will see that the variable from button 1 is passed to button 2, then button 2 affects label1. You will have to click button 1 first for the actual data to be passed to the variable.

创建一个包含 2 个按钮和一个标签的表单,然后相应地命名它们(Mybtn_Click、Mybtn2_Click)。使用我的代码,您将看到按钮 1 的变量传递给按钮 2,然后按钮 2 影响 label1。您必须先单击按钮 1 才能将实际数据传递给变量。

Public class My form

Public MyvariableName as string

Private Sub Mybtn_Click(sender As Object, e As EventArgs) Handles Mybtn_Click.Click

'Pass string to variable
MyVariableName = "Keep coding!"


End sub


Private Sub Mybtn2_Click(sender As Object, e As EventArgs) Handles Mybtn2_Click.Click

Label1.text = MyVariableName

End sub


End class

Let me know if you have a problem

如果您有问题,请告诉我

回答by sk2185

Your windows form using property is easy or Using web using session is easy way

使用属性的 Windows 窗体很简单,或者使用会话使用 web 是简单的方法

Property:

财产:

Private newPropertyValue As String
    Public Property NewProperty() As String
        Get
            Return newPropertyValue
        End Get
        Set(ByVal value As String)
            newPropertyValue = value
        End Set
    End Property

Session:

会议:

Session("sessionanme") = "Your text"

回答by Antwinette Jones

I am not sure if you need a sub procedure or not but I found that this was easier than using one.

我不确定您是否需要子程序,但我发现这比使用子程序更容易。

Public Class Expenses 'declaring these variables at class level so I can use them in any procedure Dim org As String Dim total2 As String Dim trips As Integer

公共类费用“在类级别声明这些变量,以便我可以在任何过程中使用它们 Dim org As String Dim total2 As String Dim trips As Integer

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    'I am declaring my variables 
    Dim Org As String
    Dim adate As String
    Dim ddate As String
    Dim days As String

    'declaring variable for my date calculation
    Dim date1 As DateTime = CDate(mtxtBox1.Text)
    Dim date2 As DateTime = CDate(mtxtBox2.Text)
    Dim numdays As TimeSpan = date2.Subtract(date1)
    Dim numdays2 As Integer = numdays.Days

    'Declaring my variables for the amounts input
    Dim afare As Integer
    Dim lodge As Integer
    Dim taxi As Integer
    Dim meals As Integer

    Dim total As Integer
    Dim MaE As Integer


    'Assigning values to the variables
    Org = txtOrg.Text.ToUpper
    adate = mtxtBox1.Text
    ddate = mtxtBox2.Text
    days = adate & " - " & ddate & " : " & "          " & txtLocation.Text.ToUpper

    'assigning valuables with format currency 
    afare = CInt(FormatCurrency(txtAFare.Text))
    lodge = CInt(FormatCurrency(txtLodging.Text))
    taxi = CInt(FormatCurrency(txtTaxi.Text))
    meals = CInt(FormatCurrency(txtMandE.Text))

    total = CInt(FormatCurrency(afare + lodge + taxi))
    MaE = CInt(FormatCurrency(meals / 2))

    'assigning value to show the total of expenses and 50% of meals and entertainment
    total2 = "TOTAL DEDUCTIBLE EXPENSES:" & "                     " & FormatCurrency(total + MaE)

    'Adding the items to my first list box 
    'I put spaces in "" to format the listbox I didn't know exactly how to use fmtstring
    lstReports.Items.Add("")
    lstReports.Items.Add("BUSINESS TRAVEL EXPENSES:")
    lstReports.Items.Add("***********************************************************")
    lstReports.Items.Add("TRIP TO ATTEND MEETING OF :")
    lstReports.Items.Add("")
    lstReports.Items.Add(Org)
    lstReports.Items.Add(days)
    lstReports.Items.Add("NUMBER OF DAYS:" & "                                                 " & numdays2)
    lstReports.Items.Add("")
    lstReports.Items.Add("EXPENSES:")
    lstReports.Items.Add("***********************************************************")
    lstReports.Items.Add("AIR FARE : " & "                                                          " & FormatCurrency(afare))
    lstReports.Items.Add("LODGING : " & "                                                          " & FormatCurrency(lodge))
    lstReports.Items.Add("TAXI FARE : " & "                                                        " & FormatCurrency(taxi))
    lstReports.Items.Add("TOTAL : " & "                                                               " & FormatCurrency(total))
    lstReports.Items.Add("")
    lstReports.Items.Add("MEALS AND ENTERTAINMENT EXPENSE:")
    lstReports.Items.Add("                                                                             " & FormatCurrency(meals))
    lstReports.Items.Add("")
    lstReports.Items.Add(total2)
    lstReports.Items.Add("")
    lstReports.Items.Add("____________________________________________________________")

    'This is to count how many trip submits I have and this is shown in the second list box when summary button is pressed
    trips += 1

End Sub