Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual FoxPro
Expertise: Beginner
Aug 23, 1999

Objects in Visual FoxPro

Question:
How do you set a Visual FoxPro class or array as the value of a property of another VFP class?

It appears that if you add a property to a VFP class, you can only set its value to a string, number, or T/F value.

Answer:
I have two answers for you.

Array

Let's focus on the array first. The trick with the array property is including its dimensions in the property name.

For example, let's say that you want to add an array property (3 rows, 3 columns) called aInfo to a class.

If you are creating a class in the Class Designer or Form Designer:

  1. Choose Class\New Property (from the Class Designer) or Form\New Property (from the Form Designer).
  2. Type the following:
    aInfo[3,3]
    
  3. Press the Add button.
Here is an example of creating the property in a programmatically defined class (i.e., stored in a PRG file):
 
DEFINE CLASS MyClass AS LINE
DIMENSION aInfo[3,3]

PROC Init
     WAIT WIND "Hello"
ENDPROC

ENDDEFINE
The definition of the property must occur before the first PROC/FUNC in the class.

Object

Now for the object reference. Let's say you have a form to which you want to add an object reference called oData. The oData object needs to point to an instance of a class called MyDataObject.

There are several ways to define an object reference. The first way is to add a property to the form and then execute the following code (from the Init method of the form):

LOCAL loData
loData = CREATEOBJECT("MyDataObject")
this.oData = loData
This approach makes it necessary to manually release the object reference before the form is destroyed. Otherwise the data object becomes a dangling object reference and the form will not release. When it is time to destroy the form you should do the following:
this.oData = .NULL.
The second way to add an object reference is to use the AddObject method. In this case, you do not add a property to the form. You could put the following code into the Init method of the form:
this.AddObject("oData","MyDataObject")
Note how you provide the property name in the first parameter and the name of the class in the second parameter. AddObject is only a method of containers so this only applies to forms, toolbars, and containers—and the custom class.

Dropping things from the toolbar onto the designer is the equivalent of calling AddObject.

To add classes programmatically, you need to use ADD OBJECT (see DEFINE CLASS for more help). Here is an example:

DEFINE CLASS MyForm AS FORM
ADD OBJECT oData AS MyDataObject OF MyData.VCX

PROC Init
     WAIT WIND "Hello"
ENDPROC

ENDDEFINE
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date