tags:

views:

324

answers:

4

i have a class and have declared an enum in it like

public enum file_type {readonly, readwrite, system}

Now based on a condition i want to set the enum file_type to a value something like

if("file is markedreadonly")
   file_type = readonly;

is it possible to do so in c#

+4  A: 

By writing file_type = readonly, you are trying to change the definition of an Enum at runtime, which is not allowed.

Create variable of type file_type and then set it to readonly.

Also, please use .NET naming standards to name your variable and types. Additionally for Enums, it is recommended to have a 'None' enum as the first value.

public enum FileType { None, ReadOnly, ReadWrite, System}

FileType myFileType = FileType.None;

if( //check if file is readonly)
    myFileType = FileType.ReadOnly;
SolutionYogi
A: 

Your syntax is wrong. file_type is a type, not a variable. Also, you need @readonly, since readonly is a reserved word.

John Saunders
A: 

The enums themselves behave more like types. You need a variable of type file_type which you can then assign to *file_type.readonly*

public enum FileType
{
    ReadOnly,
    ReadWrite,
    System
}

FileType ft;

if (...) ft = FileType.ReadOnly;
nickd
A: 
Malcolm