c# - Change value of Enum -
I'm reading about Enums and came up with an example where the author showed that How do we typecast a enum to int and int in a enum . However, I could not find how we can change the value of an enumeration by making reference to TrickScore . Public enum TrickScore {Sit = 7, Beg = 25, RollOver = 50, Fetch = 10, ComeHere = 5, Speak = 30,}
This code block Fetch The value of the counting varies. I am unable to understand how the score is set to TrickScore.Fetch . When I call on score.ToString () , then it gets received .
int value = (int) TrickScore.Fetch * 3; MessageBox.Show (value.ToString ()); Triccor score = (triccor) value; MessageBox.Show (score.ToString ());
I'm scared your understanding of enums right. This line of code:
int value = (int) TrickScore.Fetch * 3; The value of TrickScore.Fetch does not change. It takes the value of TrickScore.Fetch (10), multiplying it by 3 and specifying it on a variable value . In this case you are using TrickScore.Fetch , as if it is a variable that holds a number. When you have something like y = x * 5 , you do not change the value of x . Only the results are being reported to y .
The next line in your code is this:
Trick score score = (triccor) value; Then you have your variable value (30) from the previous operation, you entered the value of TrickScore enum type score variable.
The point of note is that enum inherited from int does not mean that enums It is a good practice to do arithmetic action, I would like to avoid them on the int and execute the mathematical operations, after all, it is actually TrickScore.Beg and To add TrickScore.ComeHere and to understand the result in TrickScore.Speak is understandable?
Comments
Post a Comment