conception_native_0123
Well-known member
- Local time
- Yesterday, 20:51
- Joined
- Mar 13, 2021
- Messages
- 1,923
I was looking at byref but I do not understand what is. Can someone explain this? Thank you
sub g1
dim s as string
s = "value"
call g2(byref s as string)
's is now = "value2"?
end sub
sub g2(s2 as string)
s2 = "value2"
end sub
Sub g1()
Dim s As String
s = "value"
Call g2(s)
's is now = "value2"?
Debug.Print "new value of s", s
Call g3(s)
Debug.Print "new value of s", s
End Sub
Sub g2(s2 As String)
s2 = "value2"
End Sub
sub g3(Byval s3 As String)
s3 = "will not change here"
end sub
Sorry, but none of that is true. Can you show a single example of this or a reference supporting what you are saying? There is not replication of objects like you suggest. The truth in VBA is as follows. The difference in passing an object byref or by val is basically negligible.The problem with ByVal being a default is that for objects, you start replicating things that are expensive to replicate AND the "instantiators" for the object don't necessarily replicate the values in those structure.
I was fighting tooth-and-nail with Excel and was sloppy about ByVal vs ByRef. I can tell you that Excel app objects REALLY don't like ByVal for anything other than the simplest variables.
And for that matter, Word simply WILL NOT BEHAVE if you use ByVal for things that aren't true scalar variables. If it COULD be a structure, ByRef is the ONLY way for it to work right.
This is the same in Access, Excel, or Word or any vba object.Objects are always passed by reference. The ByRef and ByVal modifers indicate how the reference is passed to the called procedure. When you pass an object type variable to a procedure, the reference or address to the object is passed -- you never really pass the object itself. When you pass an object ByRef, the reference is passed by reference and the called procedure can change the object to which that reference refers to. When an object is passed ByVal an copy of the reference (address) of the object is passed.
I can tell you that Excel app objects REALLY don't like ByVal for anything other than the simplest variables.
I just feel byval should be the default, and byref should be a conscious decision. fwiw, I hardly ever change parameters/arguments within a sub, for this reason.
Objects are always passed by reference. The ByRef and ByVal modifers indicate how the reference is passed to the called procedure. When you pass an object type variable to a procedure, the reference or address to the object is passed -- you never really pass the object itself. When you pass an object ByRef, the reference is passed by reference and the called procedure can change the object to which that reference refers to. When an object is passed ByVal an copy of the reference (address) of the object is passed.
Yes pretty much everything he said in that post is wrong. I can quote multiple sources. The one I provided is by Chip Pearson.so majp, are you saying docman is wrong? last sentence which i quote you on, show above is that byval passes an address of the object, not a copy of the object. so 2 people here say 2 different things. right?
then you should be able to modify properties of the replica without effecting the original. But if you pass a reference to say a control by value and modify it (change it backcolor or value), I guarantee the original control changes.The problem with ByVal being a default is that for objects, you start replicating things that are expensive to replicate AND the "instantiators" for the object don't necessarily replicate the values in those structure.
@conception_native_0123[/USER] - As to what I meant by "simple variables" - non-object variables like strings and integers and dates and currencies and SINGLE/DOUBLE variables - and Booleans (Y/N variables) are all simple, scalar variables. Anything that isn't any kind of application object for any application including but not limited to Access, Excel, Word, PowerPoint, Outlook, etc. That is what I mean by "simple variables." Some references I have seen will use the term "Value Variables" to mean simple variables that have a single value.
And for that matter, Word simply WILL NOT BEHAVE if you use ByVal for things that aren't true scalar variables. If it COULD be a structure, ByRef is the ONLY way for it to work right.