Constructing an enum instance [duplicate] - c#

This question already has answers here:
Can you add to an enum type in run-time
(7 answers)
Closed 8 years ago.
I have this enum function with some elements:
public enum TrackingTypeEnum
{
None,
Start,
PageView,
Foreground,
Background,
Push,
}
And I want to add some elements there, but not manually in the function, i want to use "new" command but dosn't work.
I've tried this:
TrackingTypeEnum Custom = new TrackingTypeEnum;
Any solution?
Thanks!

According to official documentation, you can't use enum keywork in such a way. In sort, an enum is a list of constants you can assign values to.
Read: http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

That's not what enums are for- you should only use them to store every possible state of an object.
If you really do want to use enums, you can store those additional values in a dictionary, like it's covered in this SO answer:
C#: can you add to an enum type in run-time

Related

Reflection and group variables [duplicate]

This question already has answers here:
Find a private field with Reflection?
(11 answers)
Attributes on Field in Class
(2 answers)
Closed 2 years ago.
I am not sure yet what is the best way to do this, I found that you can assign attributes to a class but there is no way to assign an attribute to a variable of the class.
Example
[attribute author....]
public class ABC
int variable1;
what I am looking for is to assign an attribute to variable1.
so something like this
[attribute author....]
public class ABC
[attribute or something to tie to variable1]
int variable1;
So when I use reflection to access the class, I can get additional information from each variable. The reason for this is that I am creating an editor and I want to group variables in the editor but I don't want to hard code the variable names or keep a list of variables that should be grouped.
Any idea how to make something like that?
class_name.GetType().GetFields() returns the class variables aka fields.
Use a Dictionary to store attributes for these fields.

Having created an enum, how would I be able to assign them a decimal type value in a get method? [duplicate]

This question already has answers here:
Can you use Enum for Double variables?
(6 answers)
Closed 3 years ago.
I am trying to create a program in where I am trying to give/assign a created enumeration list, a monetary value to be returned. However I am unsure how I can do this.
public decimal GetAccessoriesCost()
{
?????????
}
public enum Accessories
{
None,
StereoSystem,
LeatherInterior,
StereoAndLeather,
ComputerNavigation,
StereoAndNavigation,
LeatherAndNavigation,
All
}```
I want Stereo to be 20.20
and Leather to be 10.10
The enums in C# (unlike Java) have to be cardinal (something that is basically an integer or a whole number). see this answer for more details Can you use Enum for Double variables?
The reason enums have this constraint is to be closer to c++ and c where the enums are a fancy way to group integer constants in a namespace and probably generate a sequence.

Is there a way to get all the possible return values of a method in C#? [duplicate]

This question already has an answer here:
Is using static bytecode analysis to determine all the possible paths through a given method a variant of trying to solve the Halting Problem?
(1 answer)
Closed 4 years ago.
Suppose there's a method which returns enum. But it returns only a subset of all the values of the enumeration. Can I find out programmatically which values are obtainable?
Example. I have an enum, which describes color with 100 values. Method GetCurrentTrafficLightsState can return only 3 colors of 100. I want to pass the method GetCurrentTrafficLightsState into some other method and get 3 colors as response.
No there isn't. You cannot even determine if it will return at all, see https://en.wikipedia.org/wiki/Halting_problem

Enum Inheritance solution [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Enum “Inheritance”
I've been trying to do something like inheritance in Enum. I wanted a base Enum with multiple values from different Enums.
the best approach will be answered below.
Enum is a value type and consequently sealed, i.e. cannot be inherited.
See also Enum “Inheritance”

How to loop through the ChartColorPalette properties and add to list? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I enumerate an enum?
I am using the Microsoft Chart Controls for .NET 3.5 (C#) and have a chart in a winform.
My hope is to allow the user to change the color palette based on their preference.
How do I iterate through the color properties of the ChartColorPalette and add them to a combobox list?
I know it should be something like:
for each(something in ChartColorPalette)
{
combobox.items.add(something.ToString);
}
You can enumerate the names in your enum via the GetNames class method...
foreach(string s in Enum.GetNames(typeof(ChartColorPalette))
{
}
then later if you need the enum for the name you can parse the name value...
var val = (ChartColorPalette)Enum.Parse(typeof(ChartColorPalette),"theValue");
See how to enumerate an enum.

Categories