 |
Search
IT Courses |
|
|
|
|
|
|
|
|
|
|
|
IT
dart News Letter
|
Get
ITdart.com weekly headlines before it's published on our site! Subscribe
and receive the articles delivered to your inbox!
|
|
|
Creating an array of structures in VFP
The C languages provide for different datatypes to be defined as a part of a single structure, and then an array can be defined based upon that structure. Visual FoxPro allows you to implement this functionality using the
CUSTOM classes. To define a custom class in VFP you have to use the following syntax:
* Class definition define class Myclass as custom * Properties color = "Blue" status = "Active" * Methods function Myvalidation * Some code... endfunc enddefine Here you can define properties as well as methods for the class. Properties, when defined, behave like structure for an instance of the class. You can declare an array which contains instances of the class. This simulates the exact functionality of an array of structures.
The following code sample illustrates how to define an array of structures. The structure itself is defined in a custom class called sMyStruct. The code reads the values from the Products table and populates the array of structures.
open database C:\VFP\SAMPLES\DATA\TESTDATA.DBC use C:\VFP\SAMPLES\DATA\PRODUCTS.DBF go top && Start from the top * Define the array of structures public dimension oMyStruct(10) for i=1 to 10 oMyStruct(i)=createobject("sMyStruct") oMyStruct(i).Product_Name=PRODUCTS.Prod_name oMyStruct(i).Engineer_Name=PRODUCTS.Eng_name oMyStruct(i).Cost_Per_Unit=PRODUCTS.Unit_cost ?oMyStruct(i).Product_Name ?oMyStruct(i).Engineer_Name ?oMyStruct(i).Cost_Per_Unit skip endfor define class sMyStruct as custom Product_Name='' Engineer_Name='' Cost_Per_Unit=0 enddefine This provides flexibility of the arrays and the structured data access of VFP classes. As each item in the array is a fully functional instance of the class, this is more than just an array of structures. It is an active array of structures, because each element in the array can also have methods invoked. In addition, the properties can have assign and access method code that can run automatically when an element is read or changed.
Thus an array of object instances is a very powerful data structure. Consider that VFP allows each element in the array to be of different datatype. This means each element in an array could be an instance of a different class. Imagine the flexibility!
|
|
|