5 Replies - 2217 Views - Last Post: 08 June 2012 - 06:16 PM Rate Topic: -----

#1 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Passing data between functions

Posted 08 June 2012 - 06:47 AM

Hello,

This may seem a novice question but I cannot seem to get it to work. I would like to have custom methods to pass data between function, or to keep it in memory throughout the program.

Example:

Private function info()
Addr1 = addr1.text
Addr2 = addr2.text
City = city.text
State = state.text
Zip = zip.text

end function



Could I run this function towards the beginning of the program and use it throughout until the form is disposed? I can't seem to get the information to stick once I get out of the function, I will need to use the same information multiple times throughout the application so I figured if I could make a function to keep the info rather than declaring the 5 lines in every option but anytime I call the method, it fills it correctly then when I get back to the main function the info goes back to 'nothing'.

I may be having the wrong thinking with this, is it possible to pass data around between functions like you could in Java?

Is This A Good Question/Topic? 0
  • +

Replies To: Passing data between functions

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5632
  • View blog
  • Posts: 12,080
  • Joined: 02-June 10

Re: Passing data between functions

Posted 08 June 2012 - 06:53 AM

Its what we call "scope". A thing exists between the {} it was created in. So if you make a thing with a for{} loop, that's its scope. If you want to access a thing for the entire class, then define it in the class braces

class someClass
{
   int yogi = 5; // Accessible to the class
   
   void someMethod
   {
        int booboo = 3; // Accessible to the method

        while(true)
        {
           string Ranger = "Smith"; // Accessible to the loop
        }
   }
}


For VB you don't have the braces, but should be able to track this just the same. A thing exists within the context of where you defined it. Define it in a function, it is alive only in the function. Define it within the class, it lives for the lifespan of the class.

Are you following a book of some kind? This sort of thing is explained early on, in almost every "Learn {languge} in 21 days" type book.
Was This Post Helpful? 0
  • +
  • -

#3 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: Passing data between functions

Posted 08 June 2012 - 04:57 PM

Define the variables

Imports...
Private Class MyProgram

Dim Addr1, Addr2, City, State, Zip  As String
Dim X, Y AS ...
Private Sub MyProgram_Load...
.
'Some Code.
.
End Sub

Private Function Info() As... 'Functions Return A Value, Otherwise Use a Sub

Addr1 = Addr1.Text
.
.
.
End Function




In the body of the class, before defining any function, so they have a global scope. You can read or change their value on any placce of your code.

This post has been edited by ricardosms: 08 June 2012 - 04:58 PM

Was This Post Helpful? -1
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Passing data between functions

Posted 08 June 2012 - 05:06 PM

A Function should really only act on its input parameters and return one thing.

Using Globals makes (to me) the code stink of rotten code.

This post has been edited by AdamSpeight2008: 08 June 2012 - 05:06 PM

Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5632
  • View blog
  • Posts: 12,080
  • Joined: 02-June 10

Re: Passing data between functions

Posted 08 June 2012 - 05:19 PM

View Postricardosms, on 08 June 2012 - 05:57 PM, said:

In the body of the class, before defining any function, so they have a global scope. You can read or change their value on any placce of your code.


No. Don't use globals on that scale. That's just bad practice that most companies won't allow or accept out of their coders for a reason.

View PostCriel, on 08 June 2012 - 07:47 AM, said:

is it possible to pass data around between functions like you could in Java?


Yes. Absolutely. A great deal of java and C# can't even be distinguished, and C# is just a different .NET language. If you already know java then C# would be a better transition for you than VB
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Passing data between functions

Posted 08 June 2012 - 06:16 PM

The trouble with putting all of your variables at maximum scope is first that they are harder to keep track of, and second that your objects become "state-heavy". Two concepts should apply: only keep variables for as long as you need them, and use "need to know" logic to determine your scope.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1