Main Content

Enumerations for Property Values

Syntax for Property/Enumeration Definition

You can restrict the values that are allowed for a property to members of an enumeration class. Define the property as restricted to a specific enumeration class in the class definition using this syntax:

propertiesPropNameEnumerationClassend

This syntax restricts values ofPropNameto members of the enumeration classEnumerationClass.

Example of Restricted Property

For example, theDaysclass defines a property namedToday. The allowed values for theTodayproperty are enumeration members of theWeekDaysclass.

TheWeekDaysclass defines the enumerations:

classdefWeekDaysenumerationMonday, Tuesday, Wednesday, Thursday, Fridayendend

Use theWeekDaysenumerations to restrict the allowed values of theTodayproperty:

classdefDayspropertiesTodayWeekDaysendend

Create an object of theDaysclass.

d = Days; d.Today = WeekDays.Tuesday;
d = Days with properties: Today: Tuesday

Representing Enumeration Members with char Vectors

The automatic conversion feature enables users of theDays类赋值的Todayproperty as either enumeration members,charvectors, or string scalars. TheTodayproperty is restricted to members of theWeekDaysenumeration class. Therefore, you can assign acharvector that represents a member of theWeekDaysclass.

d = Days; d.Today ='Tuesday';

Also, you can use a string scalar:

d = Days; d.Today ="Tuesday";

For more information on restricting property values, seeValidate Property ValuesandProperty Class and Size Validation.