Re: Pass object by value not working...

Tech-Archive recommends: Fix windows errors by optimizing your registry



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
news:esIdmGlPHHA.1252@xxxxxxxxxxxxxxxxxxxxxxx
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" <lyndon.webster@xxxxxxxxx> wrote in message
news:1169486318.046279.296510@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.





.



Relevant Pages

  • Re: Pass object by value not working...
    ... Objects are always passed by reference, ... Private classVar1 As String ... Property Get NumberAs String ... Private Sub AnotherSub ...
    (microsoft.public.excel.programming)
  • Stealth Collection
    ... raised a question about reference counting. ... Private Sub Class_Initialize ... Public Function GetItem(Key As String) As Object ...
    (microsoft.public.vb.general.discussion)
  • Re: Using Drag and Drop between controls
    ... ' Project must have a component reference to: ... ' Fires then mouse button is pressed to the down position, ... Private Sub TreeView1_MouseDown(_ ... Dim oTargetNode As Node ...
    (microsoft.public.vb.controls)
  • Compatibilty between .NET framework and EXCEL
    ... COM object I wrote in C# from EXCEL (see VB code ... one using a reference ... Private Sub moComObj_MyEvent(ByVal StringParam As String) ...
    (microsoft.public.excel.programming)
  • Re: Problem with reference counting
    ... creates it, it stores an uncounted reference to the object, using its key. ... Private Sub Class_Initialize ... ' get a long pointer to the item ... Dim Obj As Object ...
    (microsoft.public.vb.general.discussion)