If you want another explanation (just in case you want to stay up a while longer) here it is:
You can specify in the declaration of a sub or a function if an argument should be passed ByVal or ByRef (the latter is the default). However a ByRef specification can be overwritten by the call by specifying that the argument should be made ByVal instead. All arguments are pushed to the stack by the call and later popped by the Sub/Function. Now when ByVal is used a copy of the data is created so the Sub/Function can't change the origional value. So if the Sub/Function has specified that an argument is ByVal it's up to the procedure to make the copy before it is popped from the stack. If however the call specify that the argument is ByVal it's up to the caller to first create the copy before it pushes the argument on the stack.
When an object is passed ByVal it still is a reference the only difference is that the reference can't be changed. Just create a new project and add a second Form to it (the project now contains two Forms, Form1 and Form2). Add the following code to Form1 to see how this works. Play around with the code and change the ByVal to ByRef to see the difference.VB Code:
Private Sub Form_Load() Dim frm As Form Set frm = Form1 Debug.Print frm.Caption '<-Shows the caption of Form1 MySub frm 'The above call will change the reference to Form2... '... or will it? Debug.Print frm.Caption '<- Which caption will be shown here? 'Unload Form2 which is loaded by MySub so you can end the application Unload Form2 End Sub Private Sub MySub(ByVal frm As Form) Debug.Print frm.Caption '<-shows the caption of Form1 Set frm = Form2 '<- Change the reference to point to another Form Debug.Print frm.Caption '<- shows the caption of Form2 End Sub




Reply With Quote