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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With