Article ID: 129314
Article Last Modified on 2/12/2000
SCATTER [FIELDS <FieldNameList> | FIELDS LIKE <Skeleton> | FIELDS EXCEPT <Skeleton>][MEMO] TO <ArrayName> | TO <ArrayName> BLANK | MEMVAR | MEMVAR BLANK | NAME <ObjectName>
GATHER FROM <ArrayName> | MEMVAR | NAME <ObjectName> [FIELDS <FieldList> | FIELDS LIKE <Skeleton> | FIELDS EXCEPT <Skeleton>] [MEMO]
* Program start.
* The following three commands close files and set up the environment
* to send output to the Visual FoxPro main screen.
ACTIVATE SCREEN
CLEAR
CLOSE DATABASES
* The next three commands create a new table and add two new records
* with values.
CREATE TABLE test1 (Name C(10), Age N(3))
INSERT INTO test1 (Name,Age) ;
VALUES ('John Doe',43)
INSERT INTO test1 (Name,Age) ;
VALUES ('Jane Smith',29)
CLOSE DATABASES
* The following command creates a new object 'oTable' from the custom
* class 'Table' defined at the end of this program. It uses the table
* 'test1' that was just made. The object 'oRow' is automatically
* created during the creation of oTable from the SCATTER NAME <object>
* command and has a property created for each field in the table.
oTable=CREATEOBJECT('Table','test1')
LIST
* The following command sends a message to the object oTable to run the
* method Next which moves the record pointer in the table ahead one.
oTable.Next()
* The next two commands send messages to assign new values to the
* properties of oTable.oRow.
oTable.oRow.Name='New Name'
oTable.oRow.Age=1
* Note that the following command is calling the user defined Gather
* function or method which then calls the GATHER NAME command.
oTable.Gather()
* The following sends a message to object oTable to run the method Previous
* and assign new values to properties before writing them to the
* table 'test1.dbf' with GATHER NAME command.
oTable.Previous()
oTable.oRow.Name='VFP'
oTable.oRow.Age=0
oTable.Gather()
LIST
RETURN
***************************
*** Class Definition ***
***************************
DEFINE CLASS Table AS Custom
* The keyword PROTECTED scopes the methods, properties and
* objects making them only accessible to methods in the
* current class definition.
PROTECTED cFileName
cFileName=''
oRow=''
* The Init event runs each time a new object is instantiated
* from its class.
PROTECTED FUNCTION Init(tcFileName)
* tcFileName: second parameter of CreateObject command,
* which is the name of the file to open.
SELECT 0
* Open the file in the next available workarea.
this.Open(tcFileName)
* Note that the following command is calling the user
* defined Scatter method, which then calls the
* SCATTER NAME command.
this.Scatter()
RETURN .T.
ENDFUNC
FUNCTION Open(tcf)
* tcf: the filename to open.
USE (tcf)
RETURN .T.
ENDFUNC
FUNCTION Close
USE
RETURN .T.
ENDFUNC
FUNCTION Next
IF NOT EOF()
SKIP
ENDIF
IF EOF() AND NOT BOF()
SKIP -1
ENDIF
this.Scatter()
RETURN .T.
ENDFUNC
FUNCTION Previous
IF NOT BOF()
SKIP -1
ENDIF
this.Scatter()
RETURN .T.
ENDFUNC
FUNCTION Scatter
* SCATTER NAME <objectname>
* In Visual FoxPro only, creates an object
* the properties of which have the same names as
* the fields in the table. The values of each
* of the object's properties are the contents
* of the fields in the table.
SCATTER NAME this.oRow MEMO
RETURN .T.
ENDFUNC
FUNCTION Gather
* GATHER NAME <objectname>
* Specifies an object the properties of which
* have the same names as fields in the table.
* The contents of each field are replaced by
* the value of the property with the same
* names as the fields.
GATHER NAME this.oRow MEMO
RETURN .T.
ENDFUNC
FUNCTION Destroy
this.Close()
RETURN .T.
ENDFUNC
ENDDEFINE
* End of code sample
Additional query words: VFoxWin 3.00
Keywords: kbcode KB129314