Wiki Home

Add Property Function


Namespace: People
The Add Property Function lets you add properties to an object during runtime. While most base classes already have an Add Property() method this function can be used on the empty class.

****************
Example: Simulating a Structure

One often requested item that you still don't find in VFP is a struc (structure). For more information on what a structure is see ?Structure?

Here is an example of a class that will return structure objects. Create a method for each structure you want to use in your application.

DEFINE CLASS Structures AS relation

   FUNCTION Employee
   * Example of an Employee structure

   Local Employee

   Employee = CREATEOBJ("Empty")

   AddProperty(Employee,"FirstName","")
   AddProperty(Employee,"LastName","")
   AppProperty(Employee,"Empnum","")
   AddProperty(Employee,"dob", {})
   AddProperty(Employee,"Status","")
   AddProperty(Employee,"Type","")
   AddProperty(Employee,"Job","")
   AddProperty(Employee,"Salary", 0.00)
   AddProperty(Employee,"WeeklyPay", 0.00)

   RETURN Employee

ENDDEFINE


Then, you could create a collection of employees if you wanted:

oStructs   = CREATEOBJ('Structures')
oEmployees = CREATEOBJ('Collection')

oEmployees.Add(oStructs.Employee())
With oEmployees(1)
   .FirstName = "Joe"
   .LastName = "Schmoe"
   .Empnum = "10001"
   .dob = {^1970-10-08}
   .Status = "Active"
   .Type = "Regular Full Time"
   .Job = "Programmer"
   .Salary = 120000
ENDWITH

For Each Emp IN oEmployees
   Emp.WeeklyPay = Emp.WeeklyPay + (Emp.Salary/52)
EndFor


fixed the code above so it will run, as I was interested to see if it did! -- Tom

Category Code Needs Testing Category VFP 8 New Features
( Topic last updated: 2004.08.17 01:32:51 AM )