byRef to container controls

  1. 5 days ago

    Clifford C

    Jun 28 Port Angeles, Washington
    Edited 5 days ago

    When a class containing arrays is passed the arrays are available byRef. If the receiving window contains several containerControls would the containerControls need to have the referenced array object pasted to each and would they byRef back to the holding window?

    If passing values back to the holding windows array preference from a containerControl would you call it something like this: HoldingWindow.arrayPreference(x,y) or some other way?

  2. Eric W

    Jun 28 Testers, Xojo Pro
    Edited 5 days ago

    Arrays are always passed byref unless you iterate though each one and assign the elements individually.

    By a class passed byref to a window I take it you mean an instance of the class is assigned to a property added to the Window that was set to be of that class.

    In that case, although I work with Xojo Web, I would say you should be able to access the object containing the array from within the Container directly from the Window using Self.PassedObject.arrayPreference(x,y,). Self is the default, so you don't need the Self prefix. Me on the other hand gives a reference to the control itself, in this case the container.

    See https://docs.xojo.com/UserGuide:Me_vs._Self

  3. 4 days ago

    Clifford C

    Jun 29 Port Angeles, Washington

    @Eric W Arrays are always passed byref unless you iterate though each one and assign the elements individually.

    By a class passed byref to a window I take it you mean an instance of the class is assigned to a property added to the Window that was set to be of that class.

    In that case, although I work with Xojo Web, I would say you should be able to access the object containing the array from within the Container directly from the Window using Self.PassedObject.arrayPreference(x,y,). Self is the default, so you don't need the Self prefix. Me on the other hand gives a reference to the control itself, in this case the container.

    See https://docs.xojo.com/UserGuide:Me_vs._Self

    I posted another question on the foram about this after trying to send the byRef array to the container controls. I'm not sure how to do this yet. And I was not able to send values from the containerControls to the array in the main window without problems.

  4. Tim H

    Jun 29 Portland, OR USA

    See my reply in the other thread.

  5. Norman P

    Jun 29 Testers, Xojo Pro outside admiring the sunshine,...

    @Eric W Arrays are always passed byref unless you iterate though each one and assign the elements individually.

    NO .... arrays are NOT passed byref
    They are a reference type
    There's a HUGE difference since you CAN pass an array byref and that means something completely different
    https://blog.xojo.com/2018/12/12/what-kind-of-variable-are-you/
    https://www.great-white-software.com/blog/2019/05/28/follow-up-on-what-type-am-i/

    1) pass an array to a method - this will alter the CONTENTS of the members of the array

               dim a() as string
              a = array("1", "2", "3")_
               foo( a )
    
              sub foo( a() as string )
    
                      for i as integer = 1 to a.ubound
                           a(i) = str(i)
                      next
    
             end sub
     

    2) pass an array to a method BYREF this will give you back a completely NEW array replacing the one you originally had

               dim a() as string
              a = array("1", "2", "3")_
               break // in the debugger make sure you have show object IDS ON
                          // write down the object ID (the giant hex value for A here)
    
               foo( a )
    
               break // in the debugger make sure you have show object IDS ON
                          // compare the value of A's ID now to the one you wrote down before
    
              sub foo( byref a() as string )
                           dim bar() as string
                           a = bar
    
             end sub
     
  6. Eric W

    Jun 29 Testers, Xojo Pro
    Edited 4 days ago

    Norman, your first example returns the same whether it's byval or byref (albeit with different memory usage).
    In the second example, a = bar if not byref only changes a within foo. So reference types do make a copy when passed byref?
    The Xojo documentation is confusing at best:

    When you pass parameters by value, it doesn't do this [byRef] because in effect the parameter only represents a copy of the data itself.
    By default all arrays and objects are reference types that are passed ByVal. Because they are reference types, changes made to that array or object will be reflected in the calling method. This is no different than assigning an array or object to a second variable or property.

    There's nothing said about byref making a copy of the array in a new memory location at all! It says byval does that effectively except for arrays. Hence my belief that byref was effectively redundant for arrays (so I never bothered with it), but I stand corrected. Is this copying via byref true of all reference types?

  7. Markus R

    Jun 29 Testers, Xojo Pro Europe / Germany / Lower Saxon...
    Edited 4 days ago

    you can give a multi array with objects to a method like this

    Public Sub Untitled(a(, ) as class1)
      System.DebugLog a(0,0).Value.toString
    End Sub

    test with a class1 that have one Property Value as Integer

    Var a(10,10) As Class1
    a(0,0) = New Class1
    a(0,0).Value = 1
    Untitled(a)

    same with integer

    Public Sub Untitled1(b(, ) as integer)
      
    End Sub

    test

    Var b(10,10) As Integer
    
    Untitled1(b)

    usual all object passed byref withoit the use of this word in the parameter definition.
    but if you will replace this object inside a other method with a new one i think you need byref as hint for the compiler.

  8. Clifford C

    Jun 30 Port Angeles, Washington

    @Markus R System.DebugLog a(0,0).Value.toString

    What's going on here: System.DebugLog a(0,0).Value.toString

  9. Markus R

    Jun 30 Testers, Xojo Pro Europe / Germany / Lower Saxon...

    @CliffordCoulter a(0,0) = New Class1
    a(0,0).Value = 1

    i had put an object in, this object have a property Value

    a(0,0) = New Class1
    a(0,0).Value = 1

    this get the object from the array at (0,0), this object property can accessed and the integer was changed into a string because DebugLog accept only a string parameter. DebugLog output text in the area below the editor as you may know.

    System.DebugLog a(0,0).Value.toString

  10. 3 days ago

    Norman P

    Jun 30 Testers, Xojo Pro outside admiring the sunshine,...

    @Eric W Norman, your first example returns the same whether it's

    This has to do with the fact that the "reference" is never altered - only the data that the references leads to is altered (the data IN the array)

    Reference types are basically a pointer to the actual data - their "the address of the data in memory"
    They just say "the data is located over here"

    Value types, like integers, colors, doubles, booleans, hold the data directly. When you store 12 in an integer there is a spot in memory that holds that value.
    When you pass that to a method byval, which is the default, you get a copy of that value for use in the method.
    Any alterations to it are NOT stored back in the original.

    When you pass one of these byref you are passing "the address" - the location of the data - not the value.
    And so in the method any changes go to that address to read it OR write it and all those changes are done to the original.

    With a reference type, its a bit different.
    Reference types already are "the location of the data" - unlike a value type like an integer they are not "the value". Ehy just say "go to this spot in memory and you'll find the data there"
    And any read / write can alter the data referred to because what was passed was the location of the data.
    When you pass one of these types byref what you can now do is, as shown in the second example, assign an entirely new location.
    In that example I could change the entire array.

  11. Eric W

    Jun 30 Testers, Xojo Pro
    Edited 3 days ago

    Understood, but upon thinking your second example over, if a = bar in foo only changes a in foo and not in the calling procedure when a is passed byval, yet a(0) = "hello world" always changes a beyond foo regardless of byval or byref, suggests a = bar changes the local reference to a if a is passed byval but changes a's non-local reference if passed byref. Looks like under Xojo's hood no copy of a's elements are ever made, ByVal merely copies a pointer to the array while byref adopts a's pointer?

    So it seems the elements of an array are always effectively passed byref but Xojo's built-in pointer to the array itself is passed byval as the default. If byRef passes the pointer to the array (as distinct from any elements) by reference instead, so a = bar in your example overwrites a's address value to be the same as bar's address value.

    So I should have said something like:

    "Elements of arrays are always effectively passed byref, but which array the passing parameter represents (the array's object ID) is passed byval by default, so a passed variable can be assigned another array as a whole with non-local effect only if it was passed byref. So assignments to a passed array's individual elements always have non-local effects (unless a locally scoped array was assigned to a byval passed variable, in which case the byval variable now represents the local array)."

    Does that sound right?

  12. Markus R

    Jun 30 Testers, Xojo Pro Europe / Germany / Lower Saxon...
    Edited 3 days ago

    if you have a= "Hello" or i = 123
    byval means you give a method the value "Hello" or 123
    byref means you give the method a or i
    objects as parameter did not have a value only the reference to it, its never byval.
    + xojo can not copy objects what would be similar as byval.
    if you need a object copy we need a self made method in the class.

  13. Norman P

    Jun 30 Testers, Xojo Pro outside admiring the sunshine,...

    A REFERENCE type ALWAYS allows you to change the data the reference points at
    In this case the data the reference points at is the data IN The array

    What you cannot do is change WHAT array is referred to (forget the data IN the array)

    In my first example the ONLY thing you can do is change the values IN the various positions in the array

    The second one actually returns a NEW array replacing the original

    Lets see if I can give an analogy
    The first is like being able to change the contents of the mailbox at the end of your driveway
    The mailbox remains the same - but the contents can change
    Thats the first example

    In the second I change the mailbox itself and so its very likely to contents also change

  14. Markus R

    Jun 30 Testers, Xojo Pro Europe / Germany / Lower Saxon...
    Edited 3 days ago

    simple explained - usually everything that is longer than a pointer/reference/integer (32/64bit)
    is or should given to a method as reference to save the time of copy data again and again.
    copy 4 bytes is faster than copy thousand bytes for a string can have.
    byval for a string is possible but not fast.
    byref for integer can have side effects to the calling method higher up.

  15. Norman P

    Jun 30 Testers, Xojo Pro outside admiring the sunshine,...
    Edited 3 days ago

    is an empty array larger than a pointer ?
    is the type a reference type or not ?
    those things matter

  16. Markus R

    Jun 30 Testers, Xojo Pro Europe / Germany / Lower Saxon...

    @Norman P is an empty array larger than a pointer ?
    is the type a reference type or not ?
    those things matter

    normal you give the method the array surroundings and not the content.
    empty or not you have a list and just this list struct use memory.
    so it make sense as parameter that is a reference to this 1 to XXXXXXXXXXX list.

  17. Eric W

    Jun 30 Testers, Xojo Pro
    Edited 3 days ago

    @Markus R objects as parameter did not have a value only the reference to it, its never byval.

    What is passed is a pointer to a data structure, which pointer can be passed byval or byref, as Xojo's reference states:

    By default, all arrays and objects are reference types that are passed ByVal. Because they are reference types, changes made to that array or object will be reflected in the calling method. This is no different than assigning an array or object to a second variable or property.

    I think I've got my head around what that means for the rare case when I want to assign a new object/array to a passed object/array as a whole.

  18. Norman P

    Jun 30 Testers, Xojo Pro outside admiring the sunshine,...

    following your logic

    Class Foo
    End Class
    
    dim f as new Foo
    

    Should F be passed to methods byref or not ?
    Understanding the difference between value and reference types and byval & byref and how these all play together is important
    I've seen some really fun bugs in user code from a very clear misunderstanding of how these work and what they mean

  19. Eric W

    Jun 30 Testers, Xojo Pro

    My understanding at present:

    If i pass f byval (the default) there's no risk of substituting foo as a whole, I can only operate foo according to its properties, methods etc.

    If I pass f byRef, I could swap in another foo complete, without other parts of my code referencing f knowing this happened. If foo represents sensitive info that could be kinda embarrassing.

    This is how I understand the concept.

  20. Norman P

    Jun 30 Testers, Xojo Pro outside admiring the sunshine,...

    Bingo !

  21. Newer ›

or Sign Up to reply!