tags:

views:

161

answers:

5

I'm working on this homework project and having trouble understanding the text explaining how to correctly take the accessed value of the enumeration and then apply the string array value to it. Can you please help me understand this? The text we are using is very difficult and poorly written for a beginner to understand, so I'm kind of on my own here. I've got the first parts written, but need some help on the accessing of the enumeration value and assigning, I think I'm close, but don't understand how to properly get and set the values on this.

Write a class, MyCourses, that contains an enumeration of all the courses that you are currently taking. This enum should be nested inside of your class MyCourses. Your class should also have an array field that provides a short description (as a String) of each of your courses. Write an indexer that takes one of your enumerated courses as an index and returns the String description of the course.

namespace Unit_4_Project
{
    public class MyCourses
    {

        // enumeration that contains an enumeration of all the courses that 
        // student is currently enrolled in
        public enum CourseName
        {
            IT274_01AU,
            CS210_06AU
        }

        // array field that provides short description for each of classes, 
        // returns string description of the course
        private String[] courseDescription = 
         {"Intermediate C#: Teaches intermediate elements of C# programming and software design",
          "Career Development Strategies: Teaches principles for career progression, resume preparation, and overall self anaylsis"};

        // indexer that takes one of the enumerated courses as an index 
        // and returns the String description of the course
        public String this[CourseName index]
        {
            get
            {
                if (index == 1)
                    return courseDescription[0];
                else
                    return courseDescription[1];
            }
            set
            {
                if (index == 1)
                    courseDescription[0] = value;
                else
                    courseDescription[1] = value;
            }
        }

    }


}//end public class MyCourses
A: 

What you're missing is that enums convert to ints. (In fact, under they covers, they basically are ints.)

And arrays can be indexed by ints.

Try indexing your array with a parameter of with the type of your enum.

tpdi
A: 

Here is my response from CodeGuru:

Enumerations are strongly typed, so you cannot compare it to an int directly. You can however cast the enumeration to an int. So you would have this instead:

if ( ( ( int ) index ) == 1)
     return courseDescription[0];
else
     return courseDescription[1];
Ed Swangren
+1  A: 

You're close, you just went a little nutty there at the end. The CourseName is nothing but a number. You can index directly into your courseDescription array ...

courseDescription[(int)index]

and you have it. :)

JP Alioto
+1  A: 

Enums do not implicitly convert to ints. But you can explicitly convert them.

public String this[CourseName index]
{
  get { return courseDescription[(int)index]; }
  set { courseDescription[(int)index] = value; }

If you are going to use enums this way, you should define the actual numerical value they represent.

public enum CourseName
{
  IT274_01AU = 0,
  CS210_06AU = 1
}

While the current implementation of enums will work, there is nothing that says it will do so in the future.

Samuel
A: 

The trick here is that Enumerations are integral datatypes at their core. This means you can cast back and forth between Enum and Int32 if you want to do so:

You need to change the "get" section to:

public String this[CourseName index]
{
    get {
        return courseDescription[(int)index];
    }        
}

This works since the enum is basically equivalent to:

public enum CourseName
{
    IT274_01AU = 0,
    CS210_06AU = 1
}
Reed Copsey