In the .NET Framework, an enumeration commonly represents constant values as symbolic or literal names. For example, if you have the type Color, then instead of using the values 0, 1, and 2, you can use Red, Green, and Blue. In C/AL, you must assign an enumeration to an Integer data type, so you must convert symbolic names in the .NET Framework to integers in C/AL.

Example

This example shows how to use an enumeration in integer expressions in C/AL and then convert the text string back to an integer to store it in the enumeration variable. In the following C# example, the enumeration is contained in the Microsoft.Dynamics.Nav.EnumSample assembly.

C#  Copy Code
namespace Microsoft.Dynamics.Nav.EnumSample 
{
	public class DataTypes
	{
		public enum ItemEnum
		{
			Item1,
			Item2,
			Item3,
			Item4
	};
}
}

In the Microsoft Dynamics NAV object, define the following C/AL variables.

Variable name DataType SubType Length

varDotNet

DotNet

'Microsoft.Dynamics.Nav.EnumSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=18cff5e0dac2f853'. Microsoft.Dynamics.Nav.EnumSample.DataTypes.ItemEnum

formatResult

Text

30

alInteger

Integer

To handle the enumerator type, add the following code to a C/AL function.

  Copy Code
// Assign an integer to the .NET Framework enumeration.
// This assigns Item3 because enum values are zero-based.
varDotNet := 2; 
// Convert the enum object to a text string, which is equivalent to the
// .NET Framework ToString method.
formatResult := format(varDotNet);
if formatResult <> 'Item3' then
  error('Wrong enumeration value. Expected %1, but actual was %2.', 'Item3', formatResult);
// Assuming that the enum value is stored as a text string, convert it back to the
// proper enum value.
varDotNet := varDotNet.Parse(varDotNet.GetType(), 'Item4');
// The varDotNet enum should now contain the integer value 3 and the enum 'Item4'.
alInteger := varDotNet;
if alInteger <> 3 then
  error('Wrong enumeration integer value. Expected %1, but actual was %2.', 3, alInteger);
formatResult := format(varDotNet);
if formatResult <> 'Item4' then
  error('Wrong enumeration value. Expected %1, actual was %2.', 'Item4', formatResult);

See Also