Const ByRef?

Is there such a thing as passing parameters as Const ByRef.

For instance, if I am passing a vector object to a method in C++, I would do it like this:

void myFunciton( const std::vector<int>& v ){ // v is a reference to a constant std::vector of ints }

As opposed to this:

void myFunciton( std::vector<int> v ){ // v is a copy of a vector of ints }

Now, in Xojo, let’s say we want to pass a very large array. What is the best way to do this? Is there a such thing as a Const ByRef?

This is how I assume you’d do it, BUT, I am a tad anal and would like v() to be const. Is it possible?

Sub myFunction( ByRef v() As Integer) ' v is a reference to an array of ints End Sub

On a side-note, this should take longer to process since it’d be passing a copy, not a reference, to v… correct?

Sub myFunction( ByVal v() As Integer) ' v is a copy to an array of ints End Sub

thanks…
-andy.

Yeah
Don’t use byref like you are - its unnecessary

Sub myFunction( v() As Integer) ' v is a reference to an array of ints End Sub
Arrays are already passed by reference anyways
So what I gave you will allow you to manipulate the members of the array but not change the array reference itself

Yours would actually let you do

Sub myFunction( ByRef v() As Integer) dim someNewArray(-1) as Integer v = someNewArray End Sub
and change out the array for a new one

I thought by default arrays were passed ByVal

What about instantiated classes? Those sent ByVal by default?

Or is it just primitives that are ByVal?

When you pass an object or array to a function, you are already passing a reference, not a copy. (The same is true with strings). There is no performance advantage in using ByRef.

Now, if you want the method to be able to change the value of the original variable, that’s when you’d use ByRef. For example:

dim v() as Integer = Array( 1, 2, 3 )
SomeMethod( v )

// Without ByRef
Sub SomeMethod (arr() As Integer)
  // arr has a reference to the same array that v points to
  arr = Array( 4, 5, 6 )
  // arr has a reference to a brand-new array
End Sub
// v still equals 1, 2, 3

// With ByRef
Sub SomeMethod (ByRef arr() As Integer)
  // arr is a stand-in for the variable v
  arr = Array( 4, 5, 6 )
End Sub
// Now v contains a new array 4, 5, 6

I should add one more:

Sub SomeMethod (arr() As Integer)
  arr.Append 0
End Sub
// Now v is 1, 2, 3, 0

[quote=91711:@Kem Tekinay]I should add one more:

Sub SomeMethod (arr() As Integer) arr.Append 0 End Sub // Now v is 1, 2, 3, 0 [/quote]

But, you didn’t pass arr() ByRef?

Correct, because arr points to the same array that v points to, and I modified the array. It’s the same as this:

dim a() as Integer = Array( 1, 2, 3 )
dim b() as Integer = a // Both hold the same array

a.Append 9
b.Append 0

// Since there is only one array, both a and b point to the values (1, 2, 3, 9, 0)

OK, I get it… It’s been a while since I have used ByRef/ByVal… Thanks for refreshing my memory.

In the case of an reference type (which strings & arrays are) what you pass is a reference.
And if you pass that byref now the reference is byref meaning you can change what it refers to.

Essentially reference types are implicitly “const” unless you explicitly say “byref” which most times you really don’t want to.

In reality most code in Xojo doesn’t use “byref” much

Leave it out until you absolutely HAVE to have it and you’ll be correct more often than not

It’s a lot different than C++ in this regard

OK – Perfect. Thank you, all!