# Using Enumerations in Wijmo

## Content

Several Wijmo controls have properties that take enumeration values.

For example, the **FlexChart**'s **chartType** property takes **wijmo.chart.ChartType** values.

## Setting enumeration properties

The recommended way to set enumeration properties is as follows:

```javascript
import { ChartType } from '@mescius/wijmo.chart';

// setting the value of an enumeration property
chart.chartType = ChartType.Line;
```

The following alternatives are also valid and produce the same result:

```javascript
// wijmo.chart.ChartType.Line has value 3:
chart.chartType = 3;

// enumerations are automatically parsed
chart.chartType = 'Line';
```

## Getting enumeration properties

Getting the property will return 3 in all cases. If you want to get the value as a string (to show in the UI for example), you can do it as follows:

```javascript
// getting the enumerated value as a number
console.log(chart.chartType); // outputs "3"

// getting the enumerated value as a string
console.log(ChartType\[chart.chartType\]); // outputs "Line"
```

## Converting enumeration values

You can use the enumeration classes to convert between strings and the corresponding numbers by indexing. For example:

```javascript
// convert enumeration value to string
console.log(ChartType\[3\]); // outputs "Line"
console.log(ChartType\[1000\]); // outputs "null"

// convert string to enumeration value
console.log(ChartType\['Line'\]); // outputs "3"
console.log(ChartType\['NoSuchValue'\]); // outputs "null"
```

## Note for .NET Developers

The .NET, **Enum** class provides methods called [GetNames](https://docs.microsoft.com/en-us/dotnet/api/system.enum.getnames?redirectedfrom=MSDN&view=netcore-3.1#System_Enum_GetNames_System_Type_) and [GetValues](https://docs.microsoft.com/en-us/dotnet/api/system.enum.getvalues?redirectedfrom=MSDN&view=netcore-3.1#System_Enum_GetValues_System_Type_) that return the names and values defined by any enumeration.

The code below shows how you could implement similar methods to get the names and values defined by TypeScript enumerations (as used in Wijmo):

```javascript
import { DataType } from '@mescius/wijmo';
// get the names defined by an enumeration
function getEnumNames(enumClass) {
    var names = \[\];
    for (var key in enumClass) {
        var val = parseInt(key);
        if (isNaN(val)) names.push(key);
    }
    return names;
}

// get the values defined by an enumeration
function getEnumValues(enumClass) {
    var values = \[\];
    for (var key in enumClass) {
        var val = parseInt(key);
        if (!isNaN(val)) values.push(val);
    }
    return values;
}

// sample usage:
var nn = getEnumNames(DataType); // returns \[ 'Object', 'String', 'Number', 'Boolean', 'Array' \]
var vv = getEnumValues(DataType); // returns \[ 0, 1, 2, 3, 4 \]
```