Main Content

Pass System.Enum Arguments

Call .NET Methods with System.Enum Arguments

An example of an enumeration is System.DayOfWeek. To see how to call a .NET method with this input type, use the GetAbbreviatedDayName method in the System.Globalization.DateTimeFormatInfo class. The following code displays the abbreviation for “Thursday”.

% Create a DayOfWeek object
thisDay = System.DayOfWeek.Thursday;
dtformat = System.Globalization.DateTimeFormatInfo;
% Display the abbreviated name of the specified day based on the 
% culture associated with the current DateTimeFormatInfo object.
dtformat.GetAbbreviatedDayName(thisDay)

To view the function signature for the GetAbbreviatedDayName method, type:

methodsview('System.Globalization.DateTimeFormatInfo')

Search the list for GetAbbreviatedDayName.

NameReturn TypeArguments
GetAbbreviatedDayNameSystem.String RetVal( System.Globalization.
DateTimeFormatInfo this,
System.DayOfWeek dayofweek)

For more information, search the Microsoft website for DateTimeFormatInfo.

Use System.Enum in MATLAB

In MATLAB®, an enumeration is a class having a finite set of named instances. You can work with .NET enumerations using features of the MATLAB enumeration class and some features unique to .NET. Some ways to use the System.DayOfWeek enumeration in MATLAB:

  • Display an enumeration member. For example:

    myDay = System.DateTime.Today;
    disp(myDay.DayOfWeek)
  • Use an enumeration in comparison statements. For example:

    myDay = System.DateTime.Today;
    switch(myDay.DayOfWeek)
        case {System.DayOfWeek.Saturday,System.DayOfWeek.Sunday}
            disp("Weekend")
        otherwise
            disp("Work day")
    end
  • Perform calculations. For example, the underlying type of DayOfWeek is System.Int32 which you can use to perform integer arithmetic. To display the date of the first day of the current week, type:

    myDay = System.DateTime.Today;
    dow = myDay.DayOfWeek;
    startDateOfWeek = AddDays(myDay,-double(dow));
    ToShortDateString(startDateOfWeek)
    
  • Perform bitwise operations. For examples, see Creating .NET Enumeration Bit Flags.

For more information, see: