Passing Parameters - A ByRef vs ByVal Question 
Author Message
 Passing Parameters - A ByRef vs ByVal Question

Hi all

Ive written a procedure to pass a form as a parameter to sub - see
below.

What I'd like to know is, should I be setting the form to nothing or
will this{*filter*}up my program ?  Or should I be passing it as a
ByVal ?

Public Sub sSelectTextBoxText(pformName As Form)
    Dim priTextBox As TextBox

    Set priTextBox = pformName.ActiveControl

    ' Select text
    priTextBox.SelStart = 0
    priTextBox.SelLength = Len(priTextBox.Text)

    ' Destroy instance of textbox & form
    Set priTextBox = Nothing
    Set pformName = Nothing
End Sub

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Passing Parameters - A ByRef vs ByVal Question

on Thu, 09 Nov 2000 14:27:28 GMT bearing the following
fruit:

Quote:
>Hi all

>Ive written a procedure to pass a form as a parameter to sub - see
>below.

>What I'd like to know is, should I be setting the form to nothing or
>will this{*filter*}up my program ?  Or should I be passing it as a
>ByVal ?

AFAIK objects are always passed byRef even if you specify
byVal.

Since it is passed byref then setting the form to nothing
could be a bad idea.

Why pass the form anyway? Why not pass the textbox

J

Quote:
>Public Sub sSelectTextBoxText(pformName As Form)
>    Dim priTextBox As TextBox

>   Set priTextBox = pformName.ActiveControl

>   ' Select text
>  priTextBox.SelStart = 0
>    priTextBox.SelLength = Len(priTextBox.Text)

>   ' Destroy instance of textbox & form
>    Set priTextBox = Nothing
>    Set pformName = Nothing
>End Sub



Wed, 18 Jun 1902 08:00:00 GMT  
 Passing Parameters - A ByRef vs ByVal Question
Thanks for your repy Jan, I hadnt thought of that !  If I pass just the
textbox, will it have a complete reference it's form and everything ?

Thanks

Ed


Quote:

> on Thu, 09 Nov 2000 14:27:28 GMT bearing the following
> fruit:

> >Hi all

> >Ive written a procedure to pass a form as a parameter to sub - see
> >below.

> >What I'd like to know is, should I be setting the form to nothing or
> >will this{*filter*}up my program ?  Or should I be passing it as a
> >ByVal ?

> AFAIK objects are always passed byRef even if you specify
> byVal.

> Since it is passed byref then setting the form to nothing
> could be a bad idea.

> Why pass the form anyway? Why not pass the textbox

> J

> >Public Sub sSelectTextBoxText(pformName As Form)
> >    Dim priTextBox As TextBox

> >   Set priTextBox = pformName.ActiveControl

> >   ' Select text
> >  priTextBox.SelStart = 0
> >    priTextBox.SelLength = Len(priTextBox.Text)

> >   ' Destroy instance of textbox & form
> >    Set priTextBox = Nothing
> >    Set pformName = Nothing
> >End Sub

Sent via Deja.com http://www.*-*-*.com/
Before you buy.


Wed, 18 Jun 1902 08:00:00 GMT  
 Passing Parameters - A ByRef vs ByVal Question
On balance it will probably{*filter*}up your program if you pass it ByRef

What you are passing is *not* the form itself, but a *reference* to
the form.  

If you destroy a reference then you cannot access the form via that
reference.

Form.Unload destroys the 'visual' component of the form

When all references to the form are destroyed then the 'non-visual'
part of the form is destroyed and the Terminate event fires.

If you pass ByVal then you are passing and destroying a second
reference to the form, if you pass ByRef then you are passing and
destroying the caller's reference.

If the caller's reference happens to be the last one, then the non
visual element will go and the form will terminate.

Either way - destroying a reference in anything other than the module
that set it up is a spectacularly good way of lousing up your program.

Quote:

>Hi all

>Ive written a procedure to pass a form as a parameter to sub - see
>below.

>What I'd like to know is, should I be setting the form to nothing or
>will this{*filter*}up my program ?  Or should I be passing it as a
>ByVal ?

>Public Sub sSelectTextBoxText(pformName As Form)
>    Dim priTextBox As TextBox

>    Set priTextBox = pformName.ActiveControl

>    ' Select text
>    priTextBox.SelStart = 0
>    priTextBox.SelLength = Len(priTextBox.Text)

>    ' Destroy instance of textbox & form
>    Set priTextBox = Nothing
>    Set pformName = Nothing
>End Sub

>Sent via Deja.com http://www.*-*-*.com/
>Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Passing parameters ByRef or ByVal

2. byVal vs ByRef

3. ByVal vs ByRef

4. Byval vs Byref

5. ByVal vs. ByRef -- preformance

6. byval vs. byref

7. ByRef Vs ByVal

8. Pass arguments ByVal or ByRef

9. Questions regarding objects, assignments, byval, byref, etc...

10. ByVal or Byref : performance question ?

11. ByVal or Byref : performance question ?

12. > passing vb6 optional byref parameter into c#

 

 
Powered by phpBB® Forum Software