devxlogo

Objects in Visual FoxPro

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 ClassNew Property (from the Class Designer) or FormNew 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 LINEDIMENSION aInfo[3,3]PROC Init     WAIT WIND "Hello"ENDPROCENDDEFINE

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 loDataloData = 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 FORMADD OBJECT oData AS MyDataObject OF MyData.VCXPROC Init     WAIT WIND "Hello"ENDPROCENDDEFINE

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist