Functions, procedures and methods in C# and VFP


void (etc)


Procedure, Function, Parameters, Return

This is where you'll find the biggest difference between the two languages. Although Visual FoxPro is now an object oriented language, it still retains the procedures and functions of the original dBase language. You can write a complete VFP application as a set of communicating and co-operating classes but you still have the flexibility of writing procedural code and sometimes this is very much easier. A C sharp application on the other hand has to be based entirely on classes.

C# Syntax Notes

When you start a new project in Visual Studio you are presented with a template something like this:

namespace MyApplication
  Class Program
    static void Main(string[] args)
       // Execution starts here

The declaration of Main here runs as follows:

This method belongs to the class rather than to an object based on the class. In practical terms this means that Main can be invoked without having to instantiate an object of class Program.
This method returns no value. If it returned, for example, an integer then the declaration would have started:
    static int Main(string[] args)
and the method would have ended with a return statement to return an integer value from the method.
This is the name of the method and must be unique within this class.
This method expects an array of strings as a parameter. If it expected no parameters then the brackets would have been empty and the declaration would have started:
    static void Main()
The array of strings will be named args.

Because C# is so object-oriented, there are no built-in general-purpose functions in the language. If you want to display "Hello World" then you must use the WriteLine method of the built-in Console object:

Console.WriteLine ("Hello World");

VFP Syntax Notes

Strictly speaking, a FoxPro function returns a value and has no other effects whereas a FoxPro procedure does have some external effect and returns no value.

A procedure is declared like this:

Procedure MyProc
  Lparameters tcParm1, tcParm2
  * Procedure code goes here

and called like this:

Do MyProc With "First parameter", "Second parameter"

A function is declared like this:

Function MyFunc
  Lparameters tcParm1, tcParm2
  * Function code goes here
  * It calculates a value for lcAnswer
  Return lcAnswer

and called like this:

lcValue = MyFunc("First parameter", "Second parameter")

In practice though you can use the function calling syntax on a procedure. If you examine the return value to a parameter you'll find that the procedure always returns true. There is however no need have an assignment statement. You can call a procedure as though it were a function and just throw the return value away:

MyProc("First parameter", "Second parameter")

A VFP method is just a function which is declared inside a Define Class block.

For  |  Language index  |  If