"ByRef" and passing a jsonItem as a parameter. Is it necessary?

Thanks in advance for any help.

Im running into a problem that may or mayn’t be caused by how a jsonItem is passed as a parameter to a function.
Im confused and need some clarification if anyone can help.

with a sub routine passing a jsonItem does the byref have an implact at all?

ie
Sub DataTest(pParent as jsonItem)
// append an attribute to pParent
end sub

versus

Sub DataTest(byref pParent as jsonItem)
// append an attribute to pParent
end sub

if I have code similar to this

dim parentJson as new jsonItem

DataTest(parentJson)

// after this running sub will the parentJson hold the appended tribute from the sub routine?
// Do I have to use byRef in the sub?

it seems to no mater using BYREF or not?

I hope Im clear enough here Im just after the impact (if any) of using ByRef when passing jsonItem as a parameter.

It seems to work the same with or without? am I missing something here?

Thanks

Object instances are always passed ByRef, i.e. when passing an object to a parameter, a new instance is not created of that object. Only one instance exists. Thus, if you manipulate that instance in your method, you are manipulating the exact same instance that exists in your caller.

Thanks Jeremy
makes sense

cheers

I should add, ByRef is used with Intrinsic types, such as String, Integer, Double. It can also be used on variables that are of an Object type (or subclass of course) but are nil. A good example of the later is ParseDate.

This isn’t quite correct.

Objects are reference types - but that does not mean they are passed byref.
What that means is what your passing is a reference / pointer / address and so you can still change the contents of the thing that is referenced.
But object REFERENCES can be passed byval (the default) and BYREF (explicitly) like any other type.

So in a simple case something like

sub foo(i as integer)
  i = 6
end foo

Window1
  Event Open()
    dim i as integer = 9
 
    foo(i)

    break
  end event

in the open event i gets initialized & set to 9
in foo it gets altered FOR THIS SCOPE to 6
but then control returns to the open event i is unchanged & is still 9

if you change this to BYREF

sub foo(byref i as integer)
  i = 6
end foo

Window1
  Event Open()
    dim i as integer = 9
 
    foo(i)

    break
  end event

in the open event i gets initialized & set to 9
in foo , the original item (as we now are byref so we refer to the original item), it gets altered to 6
when control returns to the open event i is changed & is still 6

Now with an object reference

sub foo(d as date)
  i = new date
end foo

Window1
  Event Open()
    dim i as new date
 
    foo(i)

    break
  end event

in the open event i gets initialized & set to a reference 0x018a0f78
in foo it gets altered FOR THIS SCOPE to a new reference 0x0515C4B8
but then control returns to the open event i is unchanged & is still 0x018a0f78

if you change this to BYREF

sub foo(byref d as date)
  i = new date
end foo

Window1
  Event Open()
    dim i as new date
 
    foo(i)

    break
  end event

in the open event i gets initialized & set to a reference 0x01537818
in foo it gets altered FOR THIS SCOPE to a new reference 0x00750078
but then control returns to the open event i is changed & is still 0x00750078

exactly the same effect as a byref on a intrinsic nonreference type
whats different is that with a reference type you can change what it refers TO

Lots of fun and useful as heck sometimes

Also see https://forum.xojo.com/conversation/post/133994

Thanks Norman

Thanks for setting me straight Norman!