Method returns String() byval or byref

I had a String() property in a module’s method (CpCfRndArray) that all of a sudden on my Mac that was returned as byref.
rw is passed to the method as an integer.
RandArray is a property of the module and is a string( , MnStuff.kCols) (2 dimensions)
MnStuff.kCols is a constant integer

[code]Dim cpRArray(MnStuff.kCols) As String
Dim i As Integer
For i = 0 to MnStuff.kCols
'test = RandArray(rw, i)
cpRArray(i) = RandArray(rw, i) 'If AUsed(rw) = 0 Then
Next

Return cpRArray()[/code]

For years, cpRArray was actually a property of the module. All of a sudden this return seemed to function as byref.
CpCfRndArray returns to a method this array (cpRArray) to another 1-d array. That 1-d array is in this module one of two properties of the method.
Just recently the 2 properties became identical. It took a while to understand since methods used to return byVal, As soon as I changed my method to the above with a local property versus global it became stable again.
Am I correct that “Method returns String() byval”? What could have happened to cause this change?

perhaps if you posted the signature for the function?

FUNCTION <name>(<parameters>) as <what>

CpCfRndArray (method name)
rw as integer (value passed to)
String() (return)
Global (Scope)

ok… that was not what I asked for…
copy / past the ENTIRE function … exactly as it appears in you program

That is the code above. Note: “passed to” is parameters

the previous code was:
CpRArray(MnStuff.kCols) (property) String type

[code]Dim i As Integer
For i = 0 to PartOf
'test = RandArray(rw, i)
CpRArray(i) = RandArray(rw, i) 'If AUsed(rw) = 0 Then
Next

Return CpRArray()[/code]
with parameters

rw As integer, PartOf As integer

I’m not sure when I got rid of PartOf. Sometime after June 6

it would be a lot easier to follow if you pasted the entire method including params etc rather than snippets

without the function signature it is impossible to determine how things are passed in/out of the function

  • click on the function in the left pane of the IDE
  • select COPY
  • then PASTE it here

For some reason, I have to run the debegger to get this:

[code]Function CpCfRndArray(rw As integer) As String()
//CopyLineCfRandArray(rw As integer) as String()
//MnStuff.LrnWdWheel Required = True
//for getting data
'dim test As String
Dim cpRArray(MnStuff.kCols) As String
Dim i As Integer
For i = 0 to MnStuff.kCols
'test = RandArray(rw, i)
cpRArray(i) = RandArray(rw, i) 'If AUsed(rw) = 0 Then
Next

Return cpRArray()
End Function[/code]

Not sure what else a signature is

there is no BYVAL or BYREF inferred here… you are returning a String Array plain and simple
the array will be assigned to whatever variable you use to call this…

dim myArray() as string
myArray=CpCfRndArray(14)

So… that being said… please restate your question

For some reason it changed from, well, byval to being a pointer.
byref is the only thing in XOJO like a pointer - Correct?
I have no idea what happened.

ok… this is making no sense… what do you mean a POINTER? (and yes I know what a pointer is)
Variables like INTEGER, SINGLE, DOUBLE etc can be BYVAL or BYREF (with BYVAL the default)
OBJECTS can only be BYREF.

However you are ONLY passing an INTEGER here…
So what is it that makes you think there is some kind of an issue…

This has been an awful lot of “discussion” with zero content or direction

“it changed”… WHAT is “IT”, and what do you think IT changed from or to?

Sorry, before I used pointer I wanted to make I could use it.
Sorry. I put the string before into a (#1) global property that was copied into a (#2) single dimension array. The (#1) array should have copied to (#2) as opposed to being a pointer.
That’s the only explanation I have for something changing after I run the above method.
I guess what I’m asking is there anything I could have done to prevent/change the assignment as a pointer?
I have never used byval. How could I have used it to make sure the assignment was byval?
If that isn’t the question, I’m not sure what to ask.

No

Certain types, like integers, colors, doubles are not pointer based in any way
The value is stored in the “address” itself
These are known as “value types”

Others like things created with “new” are “reference types” and a pointer is used to get to the actual data (hence a reference)
Arrays are more like this than they are value types

I’ll illustrate what this means
Create a new desktop app and in the Window1 open even put this code

Dim list() As Integer

For i As Integer = 1 To 5
  list.append i
Next

untitled(list)

Break

Then add a public method as follows

Public Sub Untitled(foo() as integer)
  foo.append 6
End Sub

Note i do NOT pass the array to my method BYREF but U can change its contents, add to it, etc

I’m wondering if somehow your code is doing something similar

Of course this leads to “what would pass an array byref mean and allow me to do?”
First off what does “byref” on a reference type mean ?
Well lets step back and see what does byref on a VALUE type mean ?
It means that we can “replace” the value stored. For a thing like an integer this means we could overwrite whatever integer value is stored in that address
Since a reference type is an “address” (at a very low level) a BYREF reference type means … we could replace the value
And this value is the ADDRESS of some other data

If you alter the method from the example before to this you’ll see the difference
This one REPLACES the entire array - not just alter the contents of an existing one

Public Sub Untitled(byref foo() as integer)
  Dim a() As Integer
  a.append 543
  
  foo = a
End Sub

When you assign a variable that holds an array to another variable that expects an array, both will point to the same array. If you want a copy, you have to explicitly make a copy through code.

dim s1() as string = array( "a", "b" )
dim s2() as string = s1
s2( 0 ) = "d"
MsgBox s1( 0 ) // "d"

[quote=337437:@Kem Tekinay]When you assign a variable that holds an array to another variable that expects an array, both will point to the same array. If you want a copy, you have to explicitly make a copy through code.
[/quote]
Right - same as the assignment for any type you can create using “new” (folderitems, picture, etc regardless of how you acquire the reference)
What you copy on such an assignment is the address, not all the values.
Its a subtle but necessary to understand difference in any language.

Thanks.
Norm. I don’t overwrite methods for the above reason. It gets complicated. I’m not sure how I’d track the change via overwriting.
Kem & Norm. For some reason, that return did for a long time copy (not get overwritten) and I was hoping you could enlighten me.
You have clarified I made the right choice in changing to copying to a temporary string.
I have more to learn about NEW

[quote=337439:@Arthur Gabhart]For some reason, that return did for a long time copy (not get overwritten) and I was hoping you could enlighten me.
[/quote]

I’m pretty sure that the behavior that Norman and I described has been true for as long as Xojo/Real Studio/REALbasic has been in existence. My conclusion is that you are now doing something differently without realizing it, or you’ve had a bug for a while that went unnoticed.