Arrays in C# and VFP

C#

int[] etc, New

VFP

Declare, Dimemnsion, Public Array

C# Syntax Notes

Arrays in C# are based on the System.Array base class and have to be declared and then instantiated:

int[] myList;
myList = new int[10];

The first element in a C# array is number 0 so these statements have created an array of elements myList[0] to myList[9]. Arrays in C# are typed and all elements hold the default value for their data type after they have been instantiated. You can set the individual values of the elements by providing a list

int[] myList = new int[10]{1,1,2,3,5,8,13,21,34,55};

or in an equivalent and more commonly-used short form:

int[] myList = {1,1,2,3,5,8,13,21,34,55};

You cannot resize an array in C# once it has been instantiated.

Multi-dimensional C# arrays

C# allows two types of multi-dimensional arrays. You can declare a rectangular array or a jagged array.

A two-dimensional rectangular array is the familiar rectangle of rows and columns where each row has the same number of columns. For example a 3 x 4 array:

int[,] myRect;      // Note the comma between the brackets.
myRect = new int[3, 4];

An array in C# can hold any type of value and a jagged array is just an array whose elements are themselves arrays. These arrays can be of different sizes because all arrays are of the same base type regardless of size. This declares a two-dimensional jagged array consisting of three arrays with 3, 6 and 5 elements respectively:

int[][] myJagged;
myJagged = new int[3][];
myJagged[0] = new int[3];
myJagged[1] = new int[6];
myJagged[2] = new int[5];

Processing C# arrays

The System.Array class provides us with a wide range of properties and methods when working with arrays. A few of the most useful are:

  • Copy() - copies a range of elements to another array.
  • Find() - finds the first element matching a condition.
  • For Each() - scans the array and processes each element.
  • Length - the number of elements in an array.
  • Resize() - changes the size of an array.
  • Sort() - sorts the elements of a one-dimensional array.

Collections in C#

C# provides several other types of collections apart from arrays. Use one of these if you need more features than the basic array class can provide:

  • Dictionary - associates keys with values.
  • List - grows in size automatically.
  • Queue - a classic First-In, First-Out (FIFO) queue.
  • Stack - a classic Last-In, First-Out (LIFO) stack.

VFP Syntax Notes

Arrays have always been handled in an odd way in FoxPro. As with the rest of the language they are loosely typed. Very loosely typed. Different elements of the array can hold different types of data. Arrays can be one-dimensional or two-dimensional and do not lose their contents if they are resized - not even if they are resized from a one-dimensional to a two-dimensional form. You resize a FoxPro array by repeating the Dimension or Declare statement.

The first element in a FoxPro array is number 1.

An array must be declared before it can be used:

Dimension laList[10]             && Declares a one-dimensional array
Declare laItems[5]               && Also declares a one-dimensional array
Public Array gaTable[10, 2]      && Declares a public two-dimensional array

All these commands will create the array and will store .F. in each element. You can set all the elements to a new value with a single command:

Declare laItems[5]
laItems = 12

Processing VFP arrays

FoxPro includes a lot of array-handling commands. Most of these start with an "A" - for example:

  • Acopy - Creates a copy of an array.
  • Adir - Stores directory information in an array.
  • Aelement - Returns a single subscript from a 2-dimensional array.
  • Alen - Gives the number of rows, columns, or elements.
  • Asort - Sorts elements in an array in ascending or descending order.
  • Ascan - Searches an array .
  • Asubscript - Generates 2-dimensional subscripts from the element number.

Collections in VFP

Visual FoxPro 8 introduced a generic collection class with standard methods such add() and remove(). It does not offer the same rich and specialised range as in C#.

Note for VB programmers

Do not use dim as an abbreviation for dimension. It feels like the right thing to do for anybody who has ever worked in any language based on BASIC but of course FoxPro won't understand it. Fox will understand dime as an abbreviation and versions with Intellisense will expand the abbreviation to dimension.

Variables  |  Language index  |  Comments

Related Items

Site Map

Site map of the Alvechurch Data site

Read More

VFP and C#.

C# is very different from Visual FoxPro so conversion from one language to the other is difficult.

Read More

Comments in C# and VFP

Adding comments to programs in C# and Visual FoxPro

Read More

Constants in C# and VFP

Declaring constants in C# and Visual FoxPro

Read More

Variables in C# and VFP

Declaring variables in C# and Visual FoxPro

Read More