Passing array to container controls

  1. 5 days ago

    Clifford C

    Jun 29 Port Angeles, Washington
    Edited 5 days ago

    I need some direction on how to send a array to a container control. This is what I have now:

    I have a window1 that has been passed an array in a class of arrays or: Arrays.VentilationPrefsArray(day,i)
    Since this will be byRef it should point back to the original.

    Next I clone that byRef array:

    for day = 1 to 7
      for i = 0 to 7 Rem 8 values to work with
        // clone array because it was a pointer,  enter new values and clone back when saving
        ClonedVentArray(day,i)=Arrays.VentilationPrefsArray(day,i)
      next
    next

    Now I create 7 containerConrtols, one for each day of the week to use in in window1:

    var count as integer
         Rem create all container controls then hides them. They will be made visable when the associated day bottom is pressed
    for count = 0 to 6
      inst_of_CCV(count) = new ContainerControlVentalation
      inst_of_CCV(count).EmbedWithin(Self, 0, 0)
      inst_of_CCV(count).WeekDayTag = count     Rem assign day # to property "WeekDayTag" use to assign values to array elements  ClonedVentArray(WeekDayTag,i)
       inst_of_CCV(count).hide
    Next

    I would like to pass to each containerControl the byRef the cloned array. I will assume if I make a change to the cloned array in the containerControl it should be reflected in the cloned array held in a preference in window1.

    Am I good so far?

    I am not sure how to send the cloned array to the controlContainers and would like to try that since I have not been able to send values directly back to window1 cloned array from the containerControl without the containerControl in window1 going blank (Window1.ClonedVentArray(WeekDayTag,i) = me.value
    ). I'm not sure what causes that.

    @CliffordCoulter How is it that the (,) or (6,6) is not needed in the Load Sub method for VentArray = varray?

    Because you're passing a reference to the array, not the array itself per se. It's not copying any values from one array to another, it's setting both variables to point to the same array.

    Is not the holding window "self "and the the controlContainers "me" ?

    In a control's event, "me" is the control and "self" is the containercontrol. There's no shorthand to get the window at that point as the container is in the way.

    And there's no need to pass the array ByRef. It's already a reference. You only need to pass it ByRef if you wanted to replace the original array with a completely different one.

  2. 4 days ago

    Tim H

    Jun 29 Portland, OR USA
    Edited 4 days ago

    Arrays in Xojo are reference types, much like objects. Thus you can assign multiple array variables to the same array. If you change one, that change will be reflected in all the "arrays" that refer to the same array. I know that can be difficult to wrap ones mind around.

    For example,

    dim a(2) as integer  // allocate space for 3 integers, plus a reference to them named "a"
    dim b() as integer   // allocate a reference to an integer array, but no space yet
    dim n as integer
    
    b = a   // set "b" to refer to the same array as "a"
    a(1) = 7
    n = b(1)  // 7

    They are different names for the same array.

    In your case, you can add an array reference to the container control, Property: VentArray(0,0) as Integer. Then add a Load method that accepts an array as a parameter and sets the reference, as well as loading the values into its controls.

    Sub Load(varray(,) as integer)
       VentArray = varray
       // load up the control values from VentArray
    End Sub

    Then call the Load method in your code that creates the containers.

    for count = 0 to 6
      inst_of_CCV(count) = new ContainerControlVentalation
      inst_of_CCV(count).EmbedWithin(Self, 0, 0)
      inst_of_CCV(count).WeekDayTag = count    
      inst_of_CCV(count).Load(ClonedVentArray)
       inst_of_CCV(count).hide
    Next

    Any change that a container makes to its VentArray will be reflected in the window's ClonedVentArray. So if you have a slider in the container control, you can put code in its ValueChanged event like:

    self.VentArray(self.WeekDayTag, x) = me.value

    And that value will be set in window1's ClonedVentArray.

    Note: the syntax varray(,) as integer in a method signature means "a reference to a 2-dimensional array of integers that will be passed to the method".

  3. Clifford C

    Jun 29 Port Angeles, Washington

    @Tim H Arrays in Xojo are reference types, much like objects. Thus you can assign multiple array variables to the same array. If you change one, that change will be reflected in all the "arrays" that refer to the same array. I know that can be difficult to wrap ones mind around.

    For example,

    dim a(2) as integer  // allocate space for 3 integers, plus a reference to them named "a"
    dim b() as integer   // allocate a reference to an integer array, but no space yet
    dim n as integer
    
    b = a   // set "b" to refer to the same array as "a"
    a(1) = 7
    n = b(1)  // 7

    They are different names for the same array.

    In your case, you can add an array reference to the container control, Property: VentArray(0,0) as Integer. Then add a Load method that accepts an array as a parameter and sets the reference, as well as loading the values into its controls.

    Sub Load(varray(,) as integer)
       VentArray = varray
       // load up the control values from VentArray
    End Sub

    Then call the Load method in your code that creates the containers.

    for count = 0 to 6
      inst_of_CCV(count) = new ContainerControlVentalation
      inst_of_CCV(count).EmbedWithin(Self, 0, 0)
      inst_of_CCV(count).WeekDayTag = count    
      inst_of_CCV(count).Load(ClonedVentArray)
       inst_of_CCV(count).hide
    Next

    Any change that a container makes to its VentArray will be reflected in the window's ClonedVentArray. So if you have a slider in the container control, you can put code in its ValueChanged event like:

    self.VentArray(self.WeekDayTag, x) = me.value

    And that value will be set in window1's ClonedVentArray.

    Note: the syntax varray(,) as integer in a method signature means "a reference to a 2-dimensional array of integers that will be passed to the method".

    Tim, you nailed it. That is exactly where I was trying to get. What was causing me grief was that I was under the misconception a multidimensional cloned array needed to be in a class to be passed so that's what I was trying to do. You pointed out it could be done with the (,) syntax. Maybe something changed along the way from the RB days. I would assume you can do this for larger arrays (,,,,,).

    I do have couple more code question. How is it that the (,) or (6,6) is not needed in the Load Sub method for VentArray = varray?
    Is not the holding window "self "and the the controlContainers "me" ?

    It appears I do not need any reference to the days of the week any longer since the clone array takes care of itself. Clone copy it back when I'm satisfied and I'm good to go. Using this container control idea itself cut my coding requirements down to nothing compared to my last window design for the larger array. I'm curious to see if it's expandable. You mentioned it could be a resource issue but in my case, I do not believe it would be a problem. I open it to make changes then close it.

    I did need to change the stored preference VentArray(,) to VentArray(6,6) in your example, no foul.

    I do understand the pointer concepts of passing byRef and passing objects so this was good stuff to make it work Tim. You are the man! Until the next question....Cliff

  4. Tim H

    Jun 29 Answer Portland, OR USA

    @CliffordCoulter How is it that the (,) or (6,6) is not needed in the Load Sub method for VentArray = varray?

    Because you're passing a reference to the array, not the array itself per se. It's not copying any values from one array to another, it's setting both variables to point to the same array.

    Is not the holding window "self "and the the controlContainers "me" ?

    In a control's event, "me" is the control and "self" is the containercontrol. There's no shorthand to get the window at that point as the container is in the way.

    And there's no need to pass the array ByRef. It's already a reference. You only need to pass it ByRef if you wanted to replace the original array with a completely different one.

or Sign Up to reply!