1

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
inspectorG4dget
115k30 gold badges159 silver badges253 bronze badges
asked Apr 16, 2009 at 23:40

5 Answers 5

1

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. :)

answered Apr 16, 2009 at 23:46
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Apr 16, 2009 at 23:42

Comments

0

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];
answered Apr 16, 2009 at 23:45

Comments

0

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.

answered Apr 16, 2009 at 23:55

Comments

0

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
}
answered Apr 17, 2009 at 0:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.