好的,这可能很简单,但我无法在任何地方找到答案。我正在尝试创建一个程序,我需要从另一个sub访问私有子中的变量。我对VB很新。我也无法弄清楚为什么我无法从orgVist
Sub访问我的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
答案 0 :(得分:1)
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
答案 1 :(得分: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
如果您遇到问题,请告诉我
答案 2 :(得分:0)
您的窗体使用属性很容易或使用网络使用会话很容易
属性:
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"
答案 3 :(得分:0)
我不确定您是否需要子程序,但我发现这比使用子程序更容易。
公共类费用 '在类级别声明这些变量,以便我可以在任何过程中使用它们 Dim org As String Dim total2 As String 昏暗的旅行作为整数
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