Click to See Complete Forum and Search --> : BY REF CALL
joy@work
September 13th, 1999, 07:53 AM
Hi
Can any body tell why there is no internal implementation of BYVAL reference in Visual Basic .
Ok If any !! Can you explain me How VB implements it.
Chris Eastwood
September 13th, 1999, 07:57 AM
Not too sure if I understand your problem here.
VB Allows parameters to be passed ByVal and ByRef in function names, procedure names, property names etc.
eg.
public Sub MyRoutine(byval sSomeThing as string)
or
public Sub MyRoutine(byref sSomeThing as string)
or
public Sub MyRoutine(sSomeThing as string)
If you omit the 'ByRef' or 'ByVal' keyword, a parameter will default to 'ByRef'.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
joy@work
September 14th, 1999, 01:55 AM
Hi Chris,
Now I can tell you What I got from the VB and its Documentation
In VB ,IF you have a declration Like:
sub DoString(byval s as string)
end sub
then Internally ,it interprets this as :
Sub RealDoString(byref sParam as string)
Dim s as string
s = sParam
My question is why this happens and what's the Cost of BYVAL parameters in VB
-JOY
Chris Eastwood
September 14th, 1999, 02:54 AM
Hi Joy
> sub DoString(byval s as string)
> end sub
>then Internally ,it interprets this as
>:Sub RealDoString(byref sParam as string)
>Dim s as string
>s = sParam
I think you've mis-interpreted it. If you do this :
Sub DoString(s as string)
'
' some code
'
End Sub
'
Then the string 's' is passed byref - if you use 'byval' it's just that - by value (ie. cannot be changed)
The difference between the two are quite simple when you think about it from a 'c' point of view:
ByRef
This passes the value by reference - a pointer if you like, to the original variable. Any changes you make in the routine are reflected outside of the routine in the variable. This takes up only a few bytes in the call-stack to pass variables this way.
ByVal
This takes a copy of the variable and places it on the 'stack' for that routine. Passing variables in this way eats up more memory, but gives you the added advantage of knowing that your value will still be the same once the called routine/property/function has finished.
Of the two, functions that have 'ByRef' arguments tend to be executed faster (slightly), because they don't have to build up (and retrieve from) such a large stack of variables - there is however the downside that bugs can easily be introduced.
It's a matter of logic really. If you are passing in a variable to a routine that doesn't need to be changed in that routine, pass it by value, otherwise by reference. If you're program is very slow and you are passing values by value, change the procedures to 'ByRef' and make sure that he logic still holds.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
joy@work
September 14th, 1999, 07:33 AM
Hi Chris
Thanks A lot
I am giving you Some Codes .Just Check it Actually I find it in Documentation.
Here CParam is an arbitary Class or Object having a Property NAME.
Sub TestObjParam(byref refP as CParam, byval valP as CParam, _
byref refNewP as CParam, byval valNewP as CParam)
Dim refNew as new CParam, valNew as new CParam
‘ Change properties
refNew.Name = “Changed”: valNew.Name = “Changed”
‘ Change objects
set refNewP = refNew: set valNewP = valNew
refP.Name = “Changed”: valP.Name = “Changed”
End Sub
Then Call this subroutine in some Event or Procedure , just Like this:
Dim ref as new CParam, val as new CParam
Dim refNew as new CParam, valNew as new CParam
ref.Name = “Unchanged”: val.Name = “Unchanged”
refNew.Name = “Unchanged”: valNew.Name = “Unchanged”
TestObjParam ref, val, refNew, valNew
Debug.print “byref object: “ & ref.Name
Debug.print “byval object: “ & val.Name
Debug.print “byref new object: “ & refNew.Name
Debug.print “byval new object: “ & valNew.Name
My Question is why val.Name changes ? Why We couldn't Copy a Object in VB
Bye,
-JOY
Chris Eastwood
September 14th, 1999, 08:13 AM
Ahhh....
I see why you are getting confused now. Here's some notes straight from the MSDN KB Article Q161308
>>
When you pass an object by value to a procedure, you can modify its properties in the procedure. Using ByVal with an object parameter affects how the object can be redefined in the procedure. If an object variable is passed to a procedure by using the ByVal keyword and the object parameter is set to a different object, the object variable still references the original object. Conversely, if an object variable is passed to a procedure by reference and the object parameter is set to a different object, the object variable references this different object. This article provides examples that highlight the functionality of the ByVal keyword.
<<
That should help you understand why the objects are being acted upon in that manner.
Are you trying to create a 'copy' of the objects in your TestObjParams routine ? You can't copy an Object in VB, only set references to it with another object type. You'd need to provide a 'clone' method on your object to copy it.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
joy@work
September 15th, 1999, 08:12 AM
Hi Chris,
Thank a lot for clearing my confusion.
Actually I am thinking from VC and Java point of view.Because they provide the features for the copy of the Object .
bye,
Joy
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.