SkyVar
SkyVar

Reputation: 351

how to access variables in private subs

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 orgVist text box from the getInputs Sub.

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

Upvotes: 0

Views: 19045

Answers (4)

Uriahs Victor
Uriahs Victor

Reputation: 1155

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.

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

Upvotes: 2

Antwinette Jones
Antwinette Jones

Reputation: 13

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

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

Upvotes: 0

OneFineDay
OneFineDay

Reputation: 9024

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 date as a variable name - it is reserved for the Date datatype. 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 Class level variables if you need them to be available for other methods and the values be retained for later use.

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

Upvotes: 1

senthilkumar2185
senthilkumar2185

Reputation: 2566

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

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"

Upvotes: 0

Related Questions