Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 5th, 2011, 06:06 PM
Registered User
 
Join Date: Feb 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question display name & value of ENUMs?

I am trying to teach myself C# using books.

I would like to display the name and the value of a collection of enums.

For instance:

Code:
        public enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
     foreach (x in Days) {
         Console.Write ("{0} {1} ", x.name, x.value);
         }
should display: Sunday 0 Monday 1 Tuesday 2 Wednesday 3 Thursday 4 Friday 5 Saturday 6

What do I replace the "x in Days" with to get these values?

Thanks in Advance
 
Old February 10th, 2011, 12:49 PM
Registered User
 
Join Date: Feb 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Enums have a default type of int. So, for instance, Sunday will have a value of 0 assigned to it (you can change what the value is if you like). There is an example in the book to shows how to do this (page 105). Here would be come code for your example:

Code:
namespace ForumExample
{
    enum days
    { 
        sunday,
        monday,
        tuesday,
        wednesday,
        thursday,
        friday,
        saturday
    }

    class Program
    {
        static void Main(string[] args)
        {
            int daysInt;
            string daysString;
            days myDay = days.sunday;
            daysInt = (int)myDay;
            daysString = Convert.ToString(myDay);
            Console.WriteLine("{0} {1}", daysInt, daysString);
            Console.ReadKey();
        }
    }
}
This will generate the info for Sunday. Not sure if you can use a foreach statement on an enum.
 
Old April 15th, 2011, 03:36 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Code:
            foreach (int value in Enum.GetValues(typeof(Days)))
            {
                Console.Write("{0} {1} ", Enum.GetName(typeof(Days), value), value);
            }
Christian
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel





Similar Threads
Thread Thread Starter Forum Replies Last Post
Enums VS Structs MoDulus Intro Programming 4 January 19th, 2012 10:42 AM
Enums.cs allanhu BOOK: ASP.NET Website Programming Problem-Design-Solution 3 November 2nd, 2004 10:37 PM
working with enums & properties miguel.ossa C# 2 January 22nd, 2004 04:16 AM
Enums for accounts??? rcarter BOOK: ASP.NET Website Programming Problem-Design-Solution 3 November 8th, 2003 12:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.