views:

174

answers:

2

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!

+1  A: 

(resisting the urge to write code)

First off, an enumeration is a named list of integers and (per MSDN) the approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

Also, remember that courseDescription is an array of strings and the purpose of the indexer is to give you an index into that array (ie. [0] returns the first string, [1] returns the second, etc.).

Jim H.
thanks for your time, i'll be working hard w/this info, hopefully i'll get it right :-)
Sheldon
+2  A: 

First of all, the base type of an enumeration has to be a numeric value type, so you can't have an enumeration with base type string. The following isn't going to compile:

public enum CourseName
{
    Class1 = "IT274-01AU: Intermediate C#", 
    Class2 = "CS210-06AU: Career Development Strategies"
}

So change it to use the default base type of int. Something like the following will do, but change the names as you see fit (you might want to use the course name instead of the code, for example). Remember also that you should use meaningful names whenever possible in an enumeration.

public enum Courses
{
    IT274_01AU, 
    CS210_06AU
}

(I know you said you didn't want specific code examples, but I think this one illustrates my point much more clearly than any explanation.)


Second, you're on the right track with the indexer, but you have to think of how to relate the enumeration to the array of string descriptions. Remember, an enumeration is nothing more than a finite set of glorified (named) numbers. With the above Courses enumeration, you have two values named IT274_01AU and CS210_06AU. So in the indexer, you have to map each of these values to the string description. There are multiple ways to do it, but the simplest one would be a switch statement, for example:

switch (myEnum)
{
case value1:
    return string1;
case value2:
    return string2;
}

Another option, however is to explicitly map the enum values to its base type, and use the base type to index into your array. For example, if you have the enum

public enum Enumerations
{
    value1 = 0,
    value2 = 1
}

then you can index directly into an array using myArray[(int)myEnum]. This may be of use and is the slightly-more-advanced-but-less-lines-of-code-and-arguably-easier-to-understand method.

lc
thanks I'll be reviewing and reading thoroughly tomorrow as well after printing at work, thanks so much for your time. it really does help me out and I appreciate it. :-)
Sheldon