+ 5
What is wrong with my enumeration? C#
I think I write exactly like in my tutorial book But the result are just "Aa" and "Bb" not the number 2 and 4. using System; namespace SoloLearn { class Program { enum Kilograms { Aa = 2, Bb = 4, } static void Main(string[] args) { Console.WriteLine("value of Aa = {0}", Kilograms.Aa); Console.WriteLine("value of Bb = {0}", Kilograms.Bb); } } }
14 Respuestas
+ 4
04Holden What's the book you're referring to?
Also... is that book was published in 2002, that's the first year C# 1.0 was officially released.
However, I seem to recall having to cast to (int) to extract the value even when C# was first released.
Share the specific title, author, publisher, and page... I'll try to see what you're missing if I can locate a copy.
+ 2
I would abandon that PDF and find current material.
+ 1
I am not sure if it is really needed, but it works if you cast it to be an integer.
Console.WriteLine("value of Aa = {0}", (int)Kilograms.Aa);
+ 1
Thank you but the point I'm worrying the most in my question is why the code isn't right while it is exactly what written in the guide book.
Perhaps this mobile app lacks some functions on pc, or the way to write this statement has changed in C#? (my book is from 2002)
+ 1
And I'm a beginner so it's hard to keep learning while the first lessons are like this
+ 1
Languages change over time. If your book is from 2002 you might expect some changes. Use google if you find something like this, or ask. Beginning programming is the most difficult part. It gets easier when you are more experienced.
+ 1
That's all right. If you need to get a numeric value, then you should cast to a numeric type:
(int)Kilograms.Aa
+ 1
But isn't in Enumeration there's only numeric? The string never be allowed so it doesn't matter cause it's always numeric value?
+ 1
An example of using "enum".
https://code.sololearn.com/cxUk4S1lh6DM/?ref=app
+ 1
David Carroll It's not an English book, and I'm not sure when exactly it was published cause it's pdf. But the book is compiled from:
Programming C#, Jesse Liberty, O’Reilly.
C# in 21 Days, Bradley L.Jones, SAMS.
Windows Forms Programming with C#, Erik Brown, Manning.
MSDN Library – April 2002.
0
Enumeration type - must necessarily represent an integer type (byte, int, short, long).
If the type is not explicitly specified, the default is int.
Example:
public enum Kilograms: byte {...}
0
i think you need remove "," symbol in 11,11
0
Yes that is correct
- 1
Just you need to definition the Aa and Bb
like this:
Console.WriteLine("value of Aa = {0}", (int)(Kilograms.Aa));
Console.WriteLine("value of Bb= {0}", (int)(Kilograms.Bb));