Where constants for events go in a C # project

I am extremely new to C # and I had a question about consensus:

Where should event related constants be stored?

Should they be included in the same place where I define my EventArgs?

As an explanation, I want to define different constants for a private field called "_difficulty", and it is set through my redefined constructor of the EventArgs class.

Let's say the constants were, public const int EASY = 0, MEDIUM = 1, HARD = 2; (I assume the naming convention is all caps)

Alternatively, I could create a class such as "TifficultyConstants" and then paste them there.

I was just interested to know what a convention is and will be of the greatest importance for following the PLO.

+4
source share
6 answers

As you really add levels, such as EASY, MEDIUM, HARD, which are in order from eachother, I would expect enum be used. As in other languages, you can use public enum Difficulty {EASY, MEDIUM, HARD} .

But where do you leave such an enumeration? If you want it to be used in many different eventArgs, I would recommend using some abstract base class:

 public class LevelEventArgs : EventArgs { public enum Difficulty { EASY, MEDIUM, HARD } } 

And then let all your EventArgs inherit from this class.

+7
source

The agreement should not do this. What you describe will be conditionally implemented as enum , not a set of named integer constants.

+12
source

Instead, I would use an enumeration:

 public enum DifficultyLevel { Easy = 0, Medium = 1, Hard = 2 } 

Thus, all values โ€‹โ€‹are defined in a structure that is well defined as being associated with a type, and not as free constants that can be associated with anything.

Declare your personal field as:

 private DifficultyLevel _difficulty; 

Assign a value similar to this:

 _difficulty = DifficultyLevel.Easy; 

By specifying the numerical values โ€‹โ€‹for the enumeration, you can also convert them to known values โ€‹โ€‹of integer values โ€‹โ€‹and from them, if you need:

 _difficulty = (DifficultyLevel)1; int level = (int)_difficulty; 
+3
source

Constants should be declared near or in the object with which they are most often associated with their use. If your main use of constants is to create custom EventArgs, this sounds like a great place to define them. If they are used everywhere, it is usually recommended to use Common or CommonUtil.

One tip; consider creating an enumeration for these values โ€‹โ€‹instead of being individual constants. They are an interconnected, mutually exclusive set of values โ€‹โ€‹indicating status; definition of enumeration values โ€‹โ€‹in the textbook.

+2
source

It all depends on how your code is configured. In some cases, it may be in a common namespace within your project or solution.

I have several projects where it is in the common namespace, i.e. Company.Common

+1
source

It looks like you want to list

  public enum Difficulty { Easy, Medium, Hard } 
+1
source

Source: https://habr.com/ru/post/1344156/


All Articles