Classes and more types

 

While in VFP your only contact with types are through classes in the .NET Framework. There are Enums, Interfaces, Structs and of course Classes.

VFPCompiler lets you define these types using a syntax familiar to you through extensions to the DEFINE CLASS command.

Classes

To define a class

DEFINE CLASS MyClass [AS BaseClassName]

PROCEDURE MethodA
LPARAMETERS aParam, bParam

ENDDEFINE

Note that the base class is optional while in VFP it is required. If you don't specify a base class it is System::Object, the base class of the .NET Framework hierarchy.

Interfaces

A .NET interface is a concept similar to the COM Interface. It is a code contract, a group of properties and functions you know some object exposes so you can code against it. Interfaces are used widely in the .NET Framework so you may need to implement some interfaces in some scenarios.

To define an Interface.

DEFINE INTERFACE MyInterface
nProperty as integer

PROCEDURE MyMethod
TPARAMETERS tcInfo as string

ENDDEFINE

An interface does not have implementation for any member. Derived classes need to provide implementation for those members before they can be instantiated, otherwise they become abstract classes.

 

Structs

To define a Struct, which is a Value Type, a simplified form of class:

DEFINE STRUCT MyStruct

PROCEDURE MethodA
LPARAMETERS aParam, bParam

ENDDEFINE

An struct can't have a explicit base class.

Enums

An enum is a collection of constant values enclosed in a class as a way to improve the readability of your code.

DEFINE ENUM MyEnum  

MyValue1
MyValue2
MyValue3 = 6

ENDDEFINE

This defines a enum with three constants, MyValue with a default value of 0 (except if you specify otherwise), MyValue2 (default 1) and MyValue3 with the value of 6.

To use these constants in your code you write something like

? MyEnum::MyValue1