Collections, Arrays & Collections, Arrays & IterationIteration
Mike FeltmanMike Feltman
F1 TechnologiesF1 Technologies
AgendaAgenda Collections in VFPCollections in VFP Working with CollectionsWorking with Collections Writing Better Code with CollectionsWriting Better Code with Collections Using Collections for Object ManipulationUsing Collections for Object Manipulation Adding Power to VFP ArraysAdding Power to VFP Arrays Collections vs. ArraysCollections vs. Arrays Merging Arrays & CollectionsMerging Arrays & Collections
Who Am IWho Am I
President F1 Technologies since 1990President F1 Technologies since 1990 Co-author Visual FoxExpressCo-author Visual FoxExpress ConsultantConsultant Xbase Developer since dBase Xbase Developer since dBase
III/FoxBase 1987III/FoxBase 1987
““Objects” Collections in VFPObjects” Collections in VFP
_SCREEN, _VFP, FormSet, _SCREEN, _VFP, FormSet, DataEnvironment, Toolbar, DataEnvironment, Toolbar, Container, PageFrame, Page, Grid, Container, PageFrame, Page, Grid, Column, CommandGroup, Column, CommandGroup, OptionGroup, Container, Custom, OptionGroup, Container, Custom, ControlControl
Other Collections in VFPOther Collections in VFP _VFP_VFP
FormsForms ProjectsProjects
ProjectProject FilesFiles ServersServers
XMLAdapterXMLAdapter TablesTables
XMLTableXMLTable FieldsFields
““Pseudo” Collections in VFPPseudo” Collections in VFP _SCREEN.Controls_SCREEN.Controls Form.ControlsForm.Controls Custom.ControlsCustom.Controls Control.ControlsControl.Controls Toolbar.ControlsToolbar.Controls Container.ControlsContainer.Controls Column.ControlsColumn.Controls
_SCREEN.Forms_SCREEN.Forms FormSet.FormsFormSet.Forms CommandGroup.ButtCommandGroup.Butt
onsons OptionGroup.ButtonsOptionGroup.Buttons PageFrame.PagesPageFrame.Pages Grid.ColumnsGrid.Columns
The Collection ClassThe Collection Class New in VFP 8New in VFP 8 Key PropertiesKey Properties
CountCount KeySort (0-3)KeySort (0-3)
Key MethodsKey Methods Add(eItem, cKey, eBefore, eAfter)Add(eItem, cKey, eBefore, eAfter) Item(eIndex)Item(eIndex) GetKey(eIndex)GetKey(eIndex)
Iterating CollectionsIterating Collections FOR lnI = 1 TO loCollection.CountFOR lnI = 1 TO loCollection.Count FOR EACH loObject in loCollectionFOR EACH loObject in loCollection FOR EACH loObject in loCollection FOR EACH loObject in loCollection
FOXOBJECTFOXOBJECT
Examples: Iterate1.prg, Iterate2.prg, Examples: Iterate1.prg, Iterate2.prg, Iterate3.prgIterate3.prg
UI Support for CollectionsUI Support for Collections
ListboxListbox ComboBoxComboBox
Example: CollectionDemo.SCXExample: CollectionDemo.SCX
Writing Better Code with Writing Better Code with CollectionsCollections
Problem: This.Parent.Page2.txtCompany Problem: This.Parent.Page2.txtCompany creates a tightly coupled dependency.creates a tightly coupled dependency.
Solution: Dynamically retrieved Object Solution: Dynamically retrieved Object references eliminate tight coupling.references eliminate tight coupling.
o=o(…) or o=f(…)o=o(…) or o=f(…)
Using Collections for Multiple Using Collections for Multiple Object ManipulationsObject Manipulations
ACAC CCCC WCWC
Returning an ArrayReturning an Array From within a class method a member From within a class method a member
array can be used as the return value.array can be used as the return value. A function can return an array by calling A function can return an array by calling
a class method that returns an array.a class method that returns an array.
Example: aConcact in utility.prgExample: aConcact in utility.prg
Array UtilitiesArray Utilities aCompactaCompact aConcataConcat aFirstaFirst aJoinaJoin aLastaLast aReverseaReverse aUniqueaUnique aWithoutaWithout ArrayDemo.PrgArrayDemo.Prg
Wrapping an Array in a Wrapping an Array in a CollectionCollection
Fox Data Objects – ADO / LINQ likeFox Data Objects – ADO / LINQ like DataCollection.prg, cDataCollection DataCollection.prg, cDataCollection
of cDataof cData
ConclusionConclusion Collections in VFP provide convenient ways Collections in VFP provide convenient ways
to access objectsto access objects Retrieving object references from collections Retrieving object references from collections
can make code a lot more flexible and stablecan make code a lot more flexible and stable VFP has a lot of powerful options available VFP has a lot of powerful options available
on arrayson arrays Collection and array implementations are Collection and array implementations are
just different enough that each has their own just different enough that each has their own place. place.
Top Related