Reputation: 14145
I have three enums inside my class
public enum CPUBrandEnum {
Intel = 1,
AMD = 2
}
and
public enum CPUTypeIntel {
Celeron,
Pentium4
}
public enum CPUTypeAMD {
ASeries,
ESeries
}
so I have property which hold cpu brand
public CPUBrandEnum CPUBrand { get; set; }
public SelectedCPUType ???
but since I don't know which cpu type will be choosed by the user in runtime I don't know how to construct propertytype which will hold choosen cpu.
If those are object I would derive them from some interface ICPUType but in this case (enum) I'm stuck.
Update: Let me explain usage of this property SelectedType.
User will select cpu brand type from CPUBrandEnum, based on that value I will display either CPUTypeIntel of CPUTypeAMD.
So based on that value SelectedCPUType can be one choosed value from CPUTypeXXX enum.
Upvotes: 0
Views: 162
Reputation: 42260
No, you can't implement an interface on an enum.
You could do this:
enum CPU
{
IntelCeleron,
IntelPentium,
AmdASeries,
AmdESeries
}
This approach allows you to pick a CPU based on the values that exist in this enum only, or you could use [System.Flags]
[System.Flags]
enum CPU
{
Intel = 1,
Amd = 2,
Celeron = 4,
Pentium = 8,
ASeries = 16,
ESeries = 32
}
This will allow you to combine values like so
var cpu = CPU.Intel | CPU.Pentium;
But it also opens the playing field to incorrect values like so
var cpu = CPU.Amd | CPU.Pentium;
Upvotes: 2
Reputation: 6276
Rather creating two enums, just create a single enum, CPUTye
public enum CPUTypeIntel
{
Celeron,
Pentium4,
ASeries,
ESeries
}
enum
in .NET can not implement interface, and moreover the enum
are just a distinct type that consists of a set of named constants.
EDIT I do not understand why you are trying to have two enum
when the nature and the definition is almost same. Both represent the CPUType
and to have a single named list for all the CPUType
is more logical then the other way how you approach to the problem.
Upvotes: 1
Reputation: 2524
I would have two properties of type CPUTypeIntel, and CPUTypeAMD, and the for each enum have a default value of None, e.g. public enum CPUInteltype {None, Celeron, Pentium4}
Then you can later test those properties, and only do work if the value is not None.
Upvotes: 1
Reputation: 62472
An enum
cannot implement an interface.
If you're looking to mark the absence of an enum value then you've got two option. One is to have an enum member called Unknown
(or something similar). For example:
public enum CPUBrandEnum {
Unknown = 0,
Intel = 1,
AMD = 2
}
The other is to use a nullable value:
public CPUBrandEnum? CPUBrand { get; set; }
Upvotes: 1
Reputation: 1500495
No, enums can't implement interfaces in .NET. They're really just named numbers.
Even if they could implement interfaces, you'd end up with the potential for bad data (e.g. an "Intel ASeries".
It's not clear what you want to do with these values anyway, but it's possible that you should just have one enum with all the valid possibilities. That might not work for your real situation, of course.
Upvotes: 10