0

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

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
4

4 回答 4

1

Sub 内部的变量仅在其 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
于 2013-10-03T03:00:12.840 回答
1

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

创建一个带有 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

如果您有问题,请告诉我

于 2015-06-29T21:46:25.787 回答
0

使用属性的 Windows 窗体很简单,或者使用会话使用 Web 很简单

财产:

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("sessionanme") = "Your text"
于 2013-10-03T03:29:36.553 回答
0

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

Public Class Expenses '在类级别声明这些变量,以便我可以在任何过程中使用它们 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
于 2014-10-28T08:15:06.140 回答