Solved

Passing Variables

Posted on 2000-03-25
4
174 Views
Last Modified: 2010-08-05
I am still in the beginning stages of learning VB. Could you give a good explanatory example of passing variables and when to use 2 different names in the 1st procedure vs. the 2nd procedure?
0
Comment
Question by:Devlu
4 Comments
 
LVL 1

Expert Comment

by:ndb
Comment Utility
Can you make your question a bit more clear please ...
0
 
LVL 3

Expert Comment

by:Foyal
Comment Utility
Private Sub FirstSub(i As String, j As Integer)

    txtText.caption = i & " " & CStr(j)
   
    'i = S from SecondSub
    'j = k from SecondSub
   
End Sub

Private Sub SecondSub(k As Integer, S As String)
   
    k = 35
    S = "String came from Second Sub"
    txtText.Caption = ""
    FirstSub S,k
   

End Sub

The called subs varibles "inherit" the values of the variables sent to it... Make sure you get the types right and the order you send in correct.
0
 
LVL 2

Accepted Solution

by:
Chrism earned 35 total points
Comment Utility
Devlu,

What you have asked is not straightforward.  The examples listed above show you the syntax to be able to write a function or sub and pass parameters to them.

Incidentally - to call these sub, just use:

call name_of_sub(param1,param2,etc)

where param1 (etc) is the name of the parameters you wish to pass.

The question about naming of parameters in the calling line and within the subroutine or function is a lot to do with personal preference.  If it is the same variable doing the same job, I tend to use the same name, but there is an entire school of thought that indicates you should always change the name within the sub or function.

The only real rule is: within each sub or function, you must use the same name. ie: the variable name you use is only defined within the context of that sub.

The questions you have not asked are:

Do these variables change value in the sub, and is this change passed back?

Can I change the variables in the sub, and not pass the change back?

The answers are: it depends how the params are passed, either ByRef or ByVal, and you can look these up for a better explanation of what each does.

A good place to start for all of this kind of stuff is:

http://vb-world.net/beginning/

Enjoy...
0
 

Author Comment

by:Devlu
Comment Utility
Adjusted points from 25 to 35
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now