ConvertToEnumExpression
Table of Contents
What it is
ConvertToEnumExpression returns an Enum, as a result of a conversion from another type, for example from a string to an Enum.
Before converting properties from one type to another, it may be necessary to check if the conversion is possible, in order to prevent runtime errors. When there is no doubt, the conversion can be done without checking.
- See LINQ for details about LINQ and the AI-Framework.
- Go to the category for all LINQ expressions.
Compare with CanConvertToEnumExpression
Code example
public StringProperty ExampleString => StringProperty(description: "string example");
public EnumProperty ExampleEnum => EnumProperty(description: "enum example");
public IMethodCall AssignEnum()
{
return Method(() => new Body
{
new If(ExampleString.CanConvertToEnum())
{
ExampleEnum.Assign(ExampleString.ConvertToEnum())
}
});
}
Explanation of the example
A StringProperty that contains the value "A", can not be converted to an Enum.
A StringProperty that contains the value "123", can be converted to an Enum. It results in the Enum 123.
In this example, the LINQ expression in line 8 CanConvertToEnum checks if the ExampleString can be converted to the type Enum.
The actual conversion (ConvertToEnum) is done in line 10.