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
现在基于我想要将enum file_type设置为类似值的条件
if("file is markedreadonly")
file_type = readonly;
is it possible to do so in c#
是不是可以在c#中这样做
4 个解决方案
#1
By writing file_type = readonly, you are trying to change the definition of an Enum at runtime, which is not allowed.
通过编写file_type = readonly,您尝试在运行时更改Enum的定义,这是不允许的。
Create variable of type file_type and then set it to readonly.
创建file_type类型的变量,然后将其设置为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.
另外,请使用.NET命名标准来命名变量和类型。此外,对于Enums,建议将'None'枚举作为第一个值。
public enum FileType { None, ReadOnly, ReadWrite, System}
FileType myFileType = FileType.None;
if( //check if file is readonly)
myFileType = FileType.ReadOnly;
#2
Your syntax is wrong. file_type is a type, not a variable. Also, you need @readonly, since readonly is a reserved word.
你的语法错了。 file_type是一个类型,而不是一个变量。此外,您需要@readonly,因为readonly是一个保留字。
#3
The enums themselves behave more like types. You need a variable of type file_type which you can then assign to file_type.readonly
枚举本身的行为更像是类型。您需要一个file_type类型的变量,然后您可以将其分配给file_type.readonly
public enum FileType
{
ReadOnly,
ReadWrite,
System
}
FileType ft;
if (...) ft = FileType.ReadOnly;
#4
You can definitely do this, almost exactly as you indicated. Note: I've changed casing to conform to accepted C# coding guidelines.
你绝对可以这样做,几乎完全如你所说。注意:我已经更改了外壳以符合接受的C#编码指南。
public enum FileType { ReadOnly, ReadWrite, System }
...
FileType fileType; FileInfo file = new FileInfo("C:\full\path\to\file"); if (file.IsReadOnly) fileType = FileType.ReadOnly
FileType fileType; FileInfo file = new FileInfo(“C:\ full \ path \ to \ file”); if(file.IsReadOnly)fileType = FileType.ReadOnly
Also, check out System.IO.FileAccess and System.IO.FileAttribute enums. They may provide you with a standard implementation that is more suitable. Personally, I prefer to use a system type rather than "rolling my own" -- less work and more easily understood by other programmers.
另外,请查看System.IO.FileAccess和System.IO.FileAttribute枚举。它们可能为您提供更合适的标准实现。就个人而言,我更喜欢使用系统类型而不是“滚动自己” - 更少的工作,更容易被其他程序员理解。
HTH
#1
By writing file_type = readonly, you are trying to change the definition of an Enum at runtime, which is not allowed.
通过编写file_type = readonly,您尝试在运行时更改Enum的定义,这是不允许的。
Create variable of type file_type and then set it to readonly.
创建file_type类型的变量,然后将其设置为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.
另外,请使用.NET命名标准来命名变量和类型。此外,对于Enums,建议将'None'枚举作为第一个值。
public enum FileType { None, ReadOnly, ReadWrite, System}
FileType myFileType = FileType.None;
if( //check if file is readonly)
myFileType = FileType.ReadOnly;
#2
Your syntax is wrong. file_type is a type, not a variable. Also, you need @readonly, since readonly is a reserved word.
你的语法错了。 file_type是一个类型,而不是一个变量。此外,您需要@readonly,因为readonly是一个保留字。
#3
The enums themselves behave more like types. You need a variable of type file_type which you can then assign to file_type.readonly
枚举本身的行为更像是类型。您需要一个file_type类型的变量,然后您可以将其分配给file_type.readonly
public enum FileType
{
ReadOnly,
ReadWrite,
System
}
FileType ft;
if (...) ft = FileType.ReadOnly;
#4
You can definitely do this, almost exactly as you indicated. Note: I've changed casing to conform to accepted C# coding guidelines.
你绝对可以这样做,几乎完全如你所说。注意:我已经更改了外壳以符合接受的C#编码指南。
public enum FileType { ReadOnly, ReadWrite, System }
...
FileType fileType; FileInfo file = new FileInfo("C:\full\path\to\file"); if (file.IsReadOnly) fileType = FileType.ReadOnly
FileType fileType; FileInfo file = new FileInfo(“C:\ full \ path \ to \ file”); if(file.IsReadOnly)fileType = FileType.ReadOnly
Also, check out System.IO.FileAccess and System.IO.FileAttribute enums. They may provide you with a standard implementation that is more suitable. Personally, I prefer to use a system type rather than "rolling my own" -- less work and more easily understood by other programmers.
另外,请查看System.IO.FileAccess和System.IO.FileAttribute枚举。它们可能为您提供更合适的标准实现。就个人而言,我更喜欢使用系统类型而不是“滚动自己” - 更少的工作,更容易被其他程序员理解。
HTH