|
-
May 31st, 2007, 01:41 AM
#1
Re: [02/03] Progress Bars
 Originally Posted by robertx
I use the pass by reference method as follows:
Code:
Public Sub UpdateProgressBar(ByRef myProgressBar As ProgressBar)
Dim i As Integer, iRecordCount As Integer
'Set record count here
iRecordCount = 100000
For i = 0 To (iRecordCount - 1)
'do something here
'increment progress bar
myProgressBar.Value = ((i + 1) * 100) / iRecordCount
Application.DoEvents()
Next
End Sub
There is no reason to pass the parameter by reference. You can set properties of the ProgressBar just the same when passing it by value. If you pass it by reference you are enabling the method to pass out a different ProgressBar than what was passed in. That is obviously not what you want.
-
May 31st, 2007, 02:19 AM
#2
Frenzied Member
Re: [02/03] Progress Bars
In this case I only want a pointer to the progress bar - I don't need to pass a copy. Since the purpose of the procedure is only to amend the progress bar's Value property, what is different about the progress bar that is returned to the calling procedure except that it's Value property has been changed to 100? I can't see any advantage in passing the progress bar ByVal as opposed to ByRef.
-
May 31st, 2007, 02:35 AM
#3
Re: [02/03] Progress Bars
 Originally Posted by robertx
In this case I only want a pointer to the progress bar - I don't need to pass a copy. Since the purpose of the procedure is only to amend the progress bar's Value property, what is different about the progress bar that is returned to the calling procedure except that it's Value property has been changed to 100? I can't see any advantage in passing the progress bar ByVal as opposed to ByRef.
You are confused as to how passing by value and by reference works. ProgressBar is a class so it's a reference type. That means that a ProgressBar variable contains a reference to a ProgressBar object. It does NOT contain a ProgressBar object itself. A reference is basically a pointer, so ALL variables whose type is a class are pointers.
When you pass a parameter to a method, whether you pass it by reference or by value affects the variable you're passing. If you pass by value then you make a copy of the variable and pass that. If your variable is a pointer to a ProgressBar then you're making a copy of a pointer, not a copy of a ProgressBar object. If you pass the variable by reference then you're passing a pointer to the variable, so a pointer to a pointer.
If your variable is a value type rather than a reference type then the variable contains the value itself rather than a pointer to an object. If you pass a value type variable by value then you're creating a copy of the value. If you pass a value type variable by reference then you're creating a pointer to the value.
It is simply not possible to pass an actual reference type object in .NET code because reference type objects are always accessed via a reference.
-
May 31st, 2007, 03:35 AM
#4
Frenzied Member
Re: [02/03] Progress Bars
OK. I follow. I passed the progress bar ByVal and ByRef and tested the Value property.
Code:
UpdateProgressBar(ProgressBar1)
MsgBox ProgressBar1.Value
In both cases the message box displayed "100" which is consistent with what you have described. Given that the outcome is the same, is there any advantage using ByVal and any disadvantage using ByRef in this instance?
I was so used to using ByRef in VB6 as it was the default that I have continued to use it as the default unless I specifically want to pass ByVal - that is when I don't want the value of the passed variable to be changed.
What was MS's rationale for changing the default to ByVal?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|