2013-10-03 32 views
0

OK,這可能是一個簡單的,但我不能在任何地方找到答案。我正在嘗試創建一個程序,並且需要從另一個子文件的私人子文件中訪問我的變量。我對VB很陌生。我也想不通,爲什麼我不能從getInputs次訪問我orgVist文本框。如何訪問變量在私人潛艇

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

我假設你打電話getInputs內displayBtn_Click()。如果這是真的,你應該從orgVistTB.Text獲取文本。 –

+1

這是不可能的,你必須將它們移動到Sub之外。你錯誤地輸入了orgVisitTB。 –

+0

@HansPassant如果我將它們移到Sub的外面不會使它們成爲全局變量嗎?我的任務將不允許使用全局變量 – SkyVar

回答

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 
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" 
0

我不知道,如果你需要一個子程序或不,但我發現這比使用一個更容易。

公共類支出 「聲明在類級別這些變量,所以我可以在任何程序 昏暗的組織使用它們作爲字符串 昏暗共2作爲字符串 昏暗的旅行作爲整數

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 
1

你需要創建一個公共變量在表單代碼的最上面,而不是分配給它一個值。然後你在private subs中使用這個公共變量。您現在可以獲取其中的數據。

創建表格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 

讓我知道如果你有經過可變組織作爲第一個參數一個問題