+ 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); } } }

10th Jul 2021, 10:49 AM
04Holden
04Holden - avatar
14 Antworten
+ 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.
10th Jul 2021, 8:49 PM
David Carroll
David Carroll - avatar
+ 2
I would abandon that PDF and find current material.
11th Jul 2021, 2:56 AM
David Carroll
David Carroll - avatar
+ 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);
10th Jul 2021, 11:33 AM
Paul
Paul - avatar
+ 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)
10th Jul 2021, 11:47 AM
04Holden
04Holden - avatar
+ 1
And I'm a beginner so it's hard to keep learning while the first lessons are like this
10th Jul 2021, 11:50 AM
04Holden
04Holden - avatar
+ 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.
10th Jul 2021, 12:08 PM
Paul
Paul - avatar
+ 1
That's all right. If you need to get a numeric value, then you should cast to a numeric type: (int)Kilograms.Aa
10th Jul 2021, 12:16 PM
Solo
Solo - avatar
+ 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?
10th Jul 2021, 12:22 PM
04Holden
04Holden - avatar
10th Jul 2021, 2:52 PM
Solo
Solo - avatar
+ 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.
11th Jul 2021, 2:17 AM
04Holden
04Holden - avatar
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 {...}
10th Jul 2021, 12:39 PM
Solo
Solo - avatar
0
i think you need remove "," symbol in 11,11
10th Jul 2021, 8:56 PM
Зябука Live
Зябука Live - avatar
0
Yes that is correct
12th Jul 2021, 9:50 AM
Tanmoy Bera
- 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));
11th Jul 2021, 5:08 PM
Hozyfa AL SALEH
Hozyfa AL SALEH - avatar