RE: Pass object by value not working...

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



You can not pass objects by value. In the grander scheme of thing when you
pass an object you are really only passing a pointer to the object and not
the entire object. There would be way too much overhead to pass objects by
value since when you pass by val the program makes a copy of the variable
being passed instead of passing the item itself.
--
HTH...

Jim Thomlinson


"Lyndon" wrote:

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...
    ... Private classVar1 As String ... Property Get NumberAs String ... ByVal, not ByRef ... Private Sub AnotherSub ...
    (microsoft.public.excel.programming)
  • 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)
  • Pass object by value not working...
    ... Private classVar1 As String ... Property Get NumberAs String ... Private Sub AnotherSub ... How do I force VBA to pass this Class ByVal or can't it be done..? ...
    (microsoft.public.excel.programming)
  • Re: VBA Code...
    ... are passing in a String value. ... Chip Pearson ... Private Sub Worksheet_Change ...
    (microsoft.public.excel.programming)
  • ByRef doesnt make it across invoke?
    ... I just found that passing arguments by reference to a function across ... string 'Failed' ... Private Shared StaticString As String ... Private Sub Form1_Load(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.vb)