Solved

Passing Variables

Posted on 2000-03-25
4
176 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
ID: 2657316
Can you make your question a bit more clear please ...
0
 
LVL 3

Expert Comment

by:Foyal
ID: 2657582
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
ID: 2661029
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
ID: 2661482
Adjusted points from 25 to 35
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

830 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