Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing Variables

Posted on 2000-03-25
4
Medium Priority
?
182 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 140 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Suggested Courses

782 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