Question passing local variables to next function

tommyt11

Member
Joined
Dec 21, 2015
Messages
23
Location
Columbus, Ohio
Programming Experience
5-10
I thought I posted this but apparently didn't take

I have a win form app I am building.
Initially I build a variable string from following code and need to pass it to next function which sends to dll and on to server. My problem is I create the string its there but I lose it before I can pass it to next function. So apparently I'm either creating wrong and not holding it or somewhere i'm dropping it in the code. Please help
Here is code to both Functions.

 Function MyFunc(ByVal Variable1 As String) As Object 
 On Error Resume Next
        Dim appData As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
        ' Checks if Local file exists and then creates string (Variable1) from correct parameters
        If My.Computer.FileSystem.FileExists(appData & "\[path]\MyFile.txt") = True Then

            'This reads file (appData & "\[path]\MyFile.txt") And creates object
            Dim line As String = System.IO.File.ReadLines(appData & "\[path]\MyFile.txt").First
            Dim fields() As String = line.Split(",".ToCharArray())
            Dim Task = "GetID"
            Dim Text1 = fields(0)
            Main.Text1.Text = fields(0)
            Variable1 = (Text1 + "," + Const1 + "," + Const2 + "," + Const3)
            MessageBox.Show(Variable1, "Yo it worked")'trace
            'This should create a representation of object for server (Variable1)
        Else
            Dim Text2 = Main.Text2.Text
            Variable1 = (Text2 + "," + Const1 + "," + Const2 + "," + Const3)
            MessageBox.Show(Variable1, "Yo it worked")'trace
        End If
        'Test it to see if got it
        If Variable1 = "" Then
            MsgBox("Don't got it")
        Else
            MsgBox("I'm calling NextFunctioin CCM")
            'Send to CCM    
               Return Variable1
        End If
        Return Variable1.ToString
        MsgBox(Variable1,, "how bout here")   ' I have string until here then it vanishes.
    End Function

    ' NOT Working 160127 6:55 am
    Function ProgramStarts(ByVal Variable2 As String, ByVal Variable1 As Object) As Object
                On Error Resume Next
        Variable1.ToString
        MsgBox(Variable1)' this gets Nothing
        Variable2 = Variable1 ' so I don't get it here
        MsgBox(Variable2,, "darn")' or here
        'This will make call to DLL once completed
 
        'Start New Code to go directly to server from form submit
    End Function
 
Last edited by a moderator:
Of course it vanishes. You said it yourself in the thread title: it's a LOCAL variable. Local variables only exist in the block in which they are declared. In your case, Variable1 is a method parameter so it exists for the life of that method call and then ceases to exist when the method completes. The fact that both those methods have parameters named Variable1 doesn't mean that they are the same variable. If you want a single variable to persist its value across multiple method calls then you must declare that variable outside all those methods, i.e. as a member variable, also known as a field.
 
So,, thanks for your response jim. I declare it as a Public ? Const? Property? I don't see field as an option. or member. Do I declare it = nothing and then can set value in local code or what is my option. Because with the code you see It may have 1 variable in the string that is not consistent. I have never worked with variables before outside of a single function. Please advise. Appreciate your input.
T
 
Variables are variables. Member variables are exactly the same as local variables except that they continue to exist and maintain their values between method calls. Don't try to make it more complex than it is. Exactly like a local variable, a member variable is Nothing until you assign a value to it and that assigned value remains until you assign something else to it.
I declare it as a Public ? Const? Property? I don't see field as an option. or member.
A member variable is declared in the same way as a local variable. It's a variable, not a constant, so you wouldn't declare it as a constant. It's a field, not a property, so you wouldn't declare it as a property.

As with other members, you can add an access modifier if you don't want to accept the default or you want to be explicit about the access level even if it is the default. Good practice dictates that you provide an access modifier on EVERY member and fields should almost always be Private. If you think that it needs to be Public then you should most likely be using a property rather than a field.
Do I declare it = nothing
Just like local variables, member variables are Nothing by default so there's no specific reason to explicitly set it to Nothing unless you want to be explicit. There's nothing wrong with that but, if you do it, make sure you're consistent and do it for ALL fields.
 
Okay, understand now. I was so confused in the function return end that I continued to overwrite my own code instead of leaving it alone. Now understand the member thing and access point. Thanks for straightening me out. Life is easier now.
 

Latest posts

Back
Top