How do I access the enumerated item with an indexer and assign array string to it for display?

Posted by g00p3k on Stack Overflow See other posts from Stack Overflow or by g00p3k
Published on 2009-03-23T02:38:25Z Indexed on 2012/10/03 15:37 UTC
Read the original article Hit count: 153

Filed under:

EDITED: Updated 3/23/09. See rest of post at bottom. I'm still having trouble with the indexer. Anymore help or examples would really help me out.

  1. 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.

  2. Write a class MyFriends that contains an indexer that provides access to the names of your friends.

namespace IT274_Unit4Project
{

    public class MyCourses
    {

        // enumeration that contains an enumeration of all the courses that 
        // student is currently enrolled in
       public enum CourseName {IT274= 0,CS210 = 1}

        // 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

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.

Please do not provide me with direct code answers, unless a MSDN style explanation that is generalized and not specific to my project. ie:

 public class MyClass
{ string field1;
string field2;

//properties
public string Value1
get etc...

Thanks!

© Stack Overflow or respective owner

Related posts about c#