Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should constants for events go in a C# project

I'm extremely new to C# and I had a question of convention:

Where should constants that are associated with an event be stored?

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

As an explain, I want to define different constants for a private field called "_difficulty", and is set through my overridden EventArgs class's constructor.

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

Alternatively, I could make a class like "DifficultyConstants", then insert them there.

I was just curious as to what the convention was and would make the most sense for following OOP.

like image 857
kachingy123 Avatar asked Nov 28 '22 18:11

kachingy123


2 Answers

The convention is to not do this. What you're describing would be conventionally implemented as an enum rather than a set of named integer constants.

like image 159
mqp Avatar answered Dec 12 '22 03:12

mqp


As you are really adding levels like EASY, MEDIUM, HARD, which are at ordinal level from eachother, I would expect an enum to be used. Just as in other languages, you could use an public enum Difficulty {EASY, MEDIUM, HARD}.

But where do you leave such an enum? If you want it to be used in a lot of 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.

like image 23
Marnix Avatar answered Dec 12 '22 03:12

Marnix