ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Pass object by value not working... (https://www.excelbanter.com/excel-programming/381664-pass-object-value-not-working.html)

Lyndon[_2_]

Pass object by value not working...
 
Hi all,

Is there any way to pass an object (created from a custom Class) by
Value in VBA..? I have tried using the ByVal keyword in my procedure
but the object is still being passed by reference (the original object
is modified in the procedure code). Here's my code:

For the Class module (called Test):

Private classVar1 As String

Property Get Number() As String
Number = classVar1
End Property

Property Let Number(ByVal newvalue As String)
classVar1 = newvalue
End Property

In the main Module:

Private Test1 As Test

Private Sub Initiate()
Set Test1 = New Test
Let Test1.Number = "1"

Call AnotherSub(Test1)

MsgBox Test1.Number 'Shows 2 not 1 even though it should be passed
ByVal, not ByRef
End Sub

Private Sub AnotherSub(ByVal t As Test)
Let t.Number = "2"
End Sub

How do I force VBA to pass this Class ByVal or can't it be done..?

Thanks,
Lyndon.


Bob Phillips

Pass object by value not working...
 
Objects are always passed by reference, not value. Why do you need to, the
code should control it?

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)


"Lyndon" wrote in message
oups.com...
Hi all,

Is there any way to pass an object (created from a custom Class) by
Value in VBA..? I have tried using the ByVal keyword in my procedure
but the object is still being passed by reference (the original object
is modified in the procedure code). Here's my code:

For the Class module (called Test):

Private classVar1 As String

Property Get Number() As String
Number = classVar1
End Property

Property Let Number(ByVal newvalue As String)
classVar1 = newvalue
End Property

In the main Module:

Private Test1 As Test

Private Sub Initiate()
Set Test1 = New Test
Let Test1.Number = "1"

Call AnotherSub(Test1)

MsgBox Test1.Number 'Shows 2 not 1 even though it should be passed
ByVal, not ByRef
End Sub

Private Sub AnotherSub(ByVal t As Test)
Let t.Number = "2"
End Sub

How do I force VBA to pass this Class ByVal or can't it be done..?

Thanks,
Lyndon.




merjet

Pass object by value not working...
 
I don't think it can be done. How about declaring a new
instance of Test, e.g. Test1c and set its property values
equal to those of Test1? Do the latter with something
like this:

Sub CopyTestObj(x As Test, x2 As Test)
With x2
.Number = x.Number
End With
End Sub

Hth,
Merjet


Peter T

Pass object by value not working...
 
At serious risk of being castigated by both Bob and Jim, you can pass
objects ByVal or ByRef. However in your example you are not changing the
reference to your class to a different instance of your class, merely
changing a property of the one and only class.

This might demonstrate.

Sub aaa()
Dim rA As Range, rB As Range

Set rA = Range("A1")
Set rB = Range("A2")
rA.Value = 1: rB.Value = 2

foo rA, rB
MsgBox rA.Address, , rA.Value
MsgBox rB.Address, , rB.Value

End Sub
Function foo(ByRef r1 As Range, ByVal r2 As Range)

Set r1 = Range("B1")
Set r2 = Range("B2")

r1.Value = 10: r2.Value = 20

End Function

Regards,
Peter T

"Lyndon" wrote in message
oups.com...
Hi all,

Is there any way to pass an object (created from a custom Class) by
Value in VBA..? I have tried using the ByVal keyword in my procedure
but the object is still being passed by reference (the original object
is modified in the procedure code). Here's my code:

For the Class module (called Test):

Private classVar1 As String

Property Get Number() As String
Number = classVar1
End Property

Property Let Number(ByVal newvalue As String)
classVar1 = newvalue
End Property

In the main Module:

Private Test1 As Test

Private Sub Initiate()
Set Test1 = New Test
Let Test1.Number = "1"

Call AnotherSub(Test1)

MsgBox Test1.Number 'Shows 2 not 1 even though it should be passed
ByVal, not ByRef
End Sub

Private Sub AnotherSub(ByVal t As Test)
Let t.Number = "2"
End Sub

How do I force VBA to pass this Class ByVal or can't it be done..?

Thanks,
Lyndon.




Bob Phillips

Pass object by value not working...
 
Castigation coming <g.

The object is not copied and passed to the called routine when passed as
ByVal, the reference to the object is passed by value instead of reference.
As you clearly demonstrate, even though the object itself is not changed, a
property of the object is changed, hence no copy.

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)


"Peter T" <peter_t@discussions wrote in message
...
At serious risk of being castigated by both Bob and Jim, you can pass
objects ByVal or ByRef. However in your example you are not changing the
reference to your class to a different instance of your class, merely
changing a property of the one and only class.

This might demonstrate.

Sub aaa()
Dim rA As Range, rB As Range

Set rA = Range("A1")
Set rB = Range("A2")
rA.Value = 1: rB.Value = 2

foo rA, rB
MsgBox rA.Address, , rA.Value
MsgBox rB.Address, , rB.Value

End Sub
Function foo(ByRef r1 As Range, ByVal r2 As Range)

Set r1 = Range("B1")
Set r2 = Range("B2")

r1.Value = 10: r2.Value = 20

End Function

Regards,
Peter T

"Lyndon" wrote in message
oups.com...
Hi all,

Is there any way to pass an object (created from a custom Class) by
Value in VBA..? I have tried using the ByVal keyword in my procedure
but the object is still being passed by reference (the original object
is modified in the procedure code). Here's my code:

For the Class module (called Test):

Private classVar1 As String

Property Get Number() As String
Number = classVar1
End Property

Property Let Number(ByVal newvalue As String)
classVar1 = newvalue
End Property

In the main Module:

Private Test1 As Test

Private Sub Initiate()
Set Test1 = New Test
Let Test1.Number = "1"

Call AnotherSub(Test1)

MsgBox Test1.Number 'Shows 2 not 1 even though it should be passed
ByVal, not ByRef
End Sub

Private Sub AnotherSub(ByVal t As Test)
Let t.Number = "2"
End Sub

How do I force VBA to pass this Class ByVal or can't it be done..?

Thanks,
Lyndon.






Peter T

Pass object by value not working...
 
You explained much better than 1, but it's what I meant!

Regards,
Peter T


"Jim Thomlinson" wrote in message
...
Agreed (and a good point to note) your code does show the difference

between
passing an object by ref or by value. By ref you pass the actual pointer

to
the object and by val you pass a copy of the pointer. The pointer is

modified
for R1 and not modified for R2. But it is important to note that the
underlying properties of the objects passed are left wide open no matter

how
you pass the object. When we look at this code...

Sub aaa()
Dim rA As Range, rB As Range

Set rA = Range("A1")
Set rB = Range("A2")
rA.Value = 1: rB.Value = 2

foo rA, rB
MsgBox rA.Address, , rA.Value
MsgBox rB.Address, , rB.Value

End Sub
Function foo(ByRef r1 As Range, ByVal r2 As Range)

r1.Value = 10: r2.Value = 20 'Permanent changes to the object

End Function

... you will note that you are able to change the value of R1 and R2 and
that the change is a permanent modification of the objects A1 and A2 (not
temporary as the byVal might lead you to believe). The pointer is changed
however and when we return back to Sub aaa the change becomes clear.

We are both right in our limited scope. You are corrent in that the

pointer
is passed by reference or by value and I am correct in that the underlying
object is permanently modified no matter how it is passed.
--
HTH...

Jim Thomlinson


"Peter T" wrote:

At serious risk of being castigated by both Bob and Jim, you can pass
objects ByVal or ByRef. However in your example you are not changing the
reference to your class to a different instance of your class, merely
changing a property of the one and only class.

This might demonstrate.

Sub aaa()
Dim rA As Range, rB As Range

Set rA = Range("A1")
Set rB = Range("A2")
rA.Value = 1: rB.Value = 2

foo rA, rB
MsgBox rA.Address, , rA.Value
MsgBox rB.Address, , rB.Value

End Sub
Function foo(ByRef r1 As Range, ByVal r2 As Range)

Set r1 = Range("B1")
Set r2 = Range("B2")

r1.Value = 10: r2.Value = 20

End Function

Regards,
Peter T

"Lyndon" wrote in message
oups.com...
Hi all,

Is there any way to pass an object (created from a custom Class) by
Value in VBA..? I have tried using the ByVal keyword in my procedure
but the object is still being passed by reference (the original object
is modified in the procedure code). Here's my code:

For the Class module (called Test):

Private classVar1 As String

Property Get Number() As String
Number = classVar1
End Property

Property Let Number(ByVal newvalue As String)
classVar1 = newvalue
End Property

In the main Module:

Private Test1 As Test

Private Sub Initiate()
Set Test1 = New Test
Let Test1.Number = "1"

Call AnotherSub(Test1)

MsgBox Test1.Number 'Shows 2 not 1 even though it should be passed
ByVal, not ByRef
End Sub

Private Sub AnotherSub(ByVal t As Test)
Let t.Number = "2"
End Sub

How do I force VBA to pass this Class ByVal or can't it be done..?

Thanks,
Lyndon.







Peter T

Pass object by value not working...
 
Castigation coming <g.

Was anticipating, and came back to leathered up and prepared !!

But we're all agreed so I can stand down <g

Regards,
Peter T


"Bob Phillips" wrote in message
...
Castigation coming <g.

The object is not copied and passed to the called routine when passed as
ByVal, the reference to the object is passed by value instead of

reference.
As you clearly demonstrate, even though the object itself is not changed,

a
property of the object is changed, hence no copy.

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)


"Peter T" <peter_t@discussions wrote in message
...
At serious risk of being castigated by both Bob and Jim, you can pass
objects ByVal or ByRef. However in your example you are not changing the
reference to your class to a different instance of your class, merely
changing a property of the one and only class.

This might demonstrate.

Sub aaa()
Dim rA As Range, rB As Range

Set rA = Range("A1")
Set rB = Range("A2")
rA.Value = 1: rB.Value = 2

foo rA, rB
MsgBox rA.Address, , rA.Value
MsgBox rB.Address, , rB.Value

End Sub
Function foo(ByRef r1 As Range, ByVal r2 As Range)

Set r1 = Range("B1")
Set r2 = Range("B2")

r1.Value = 10: r2.Value = 20

End Function

Regards,
Peter T

"Lyndon" wrote in message
oups.com...
Hi all,

Is there any way to pass an object (created from a custom Class) by
Value in VBA..? I have tried using the ByVal keyword in my procedure
but the object is still being passed by reference (the original object
is modified in the procedure code). Here's my code:

For the Class module (called Test):

Private classVar1 As String

Property Get Number() As String
Number = classVar1
End Property

Property Let Number(ByVal newvalue As String)
classVar1 = newvalue
End Property

In the main Module:

Private Test1 As Test

Private Sub Initiate()
Set Test1 = New Test
Let Test1.Number = "1"

Call AnotherSub(Test1)

MsgBox Test1.Number 'Shows 2 not 1 even though it should be passed
ByVal, not ByRef
End Sub

Private Sub AnotherSub(ByVal t As Test)
Let t.Number = "2"
End Sub

How do I force VBA to pass this Class ByVal or can't it be done..?

Thanks,
Lyndon.









All times are GMT +1. The time now is 12:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com