Wiki Home

Access And Assign With Arrays


Namespace: VFP
Some interesting (undocumented?) facts about Access and Assign and array properties

  • Both _ACCESS() and _ASSIGN() require additional parameters to handle the array subscripts
  • The number of parameters required seems to depend on the DIMENSION of the array, not the way in which the element was referenced. For example, if o.aTest is dimensioned as [1,2], an assignment as o.aTest[1] will fire the aTest_Assign() method and it will be looking for two parameters, not one.
  • ReDIMENSIONing, ASCAN() and ACOPY() do not fire either method. These methods are not fired also when array accessed from the Control Source property of any control. _Access is not fired too when array is acessed as a row source of combobox or listbox. -- Vlad Grynchyshyn One could use this behaviour to good effect in making an autodimensioning array management class...


    Here is some test code to play with.

    CLEAR
    o = CREATEOBJECT ("Test")
    o.aTest [1] = "A"
    o.aTest [2] = "B"
    
    ? o.aTest [1, 2]
    
    DIMENSION o.aTest [5]
    =ASCAN (o.aTest, "A")
    =ACOPY (o.aTest, la)
    la [1] = "Z"
    =ACOPY (la, o.aTest)
    ? o.aTest [1]
    
    *=========================================
    DEFINE CLASS TEST AS RELATION
    *=========================================
    
    DIMENSION aTest [1, 2]
    
    FUNCTION aTest_ASSIGN (tvValue, tnRow, tnCol)
        WAIT WINDOW [aTest_ASSIGN(] + TRANSFORM (tvValue) + [,] + TRANSFORM (tnRow) + [,] + TRANSFORM (tnCol) + [)]
        THIS.aTest [tnRow, IIF (VARTYPE (tnCol) = "L", 1, tnCol)] = tvValue
    RETURN
    
    FUNCTION aTest_ACCESS (tnRow, tnCol)
        WAIT WINDOW [aTest_ACCESS(] + TRANSFORM (tnRow) + [,] + TRANSFORM (tnCol) + [)]
    RETURN THIS.aTest [tnRow, IIF (VARTYPE (tnCol) = "L", 1, tnCol)]
    
    ENDDEFINE
    


    You can use _Access and _Assign methods of array also to make access to array elements by other type of the index than just integer index. This means that VFP does not makes checking of the array index type prior to firing of the _Access and _Assign methods. Thus, such referense as 'Object.aArray["Name"]' will fire _access method with index parameter that contains "Name" string. Now, you can return a real array value using passed value. You can use any type here - dates, float numbers, objects (!) etc. - all will work just perfect. You cannot, however, use this reference in the Control Source property of the controls, because they don't fire _Access and _Assign methods.

    Another undocuemnted thing is that you can separate property access as to array and access as to the object. In other words, you can organize 2 properties with the same name - one is array and another is an object. To do this, just return object reference in the _Access method when parameter is empty or .NULL. This will allow correct executing of both following expressions:

    Object.aArray.Property && access to the property of the 'aArray' object
    Object.aArray [12] && access to the element of array

    For code sample that uses both these tricks see VFP Home Grown Collection - a simple collection sample that could be organized in VFP. In addition, see files section at the Universal Thread. You will find there downloadable collection object with sources and description.

    -- Vlad Grynchyshyn

    Contributors Lauren Clarke, Vlad Grynchyshyn
    Category Access And Assign
  • ( Topic last updated: 2007.04.27 02:08:39 PM )