枚举和数组索引?

发布于 2024-07-17 03:54:05 字数 1695 浏览 5 评论 0原文

我正在做这个家庭作业项目,但无法理解解释如何正确获取枚举的访问值然后将字符串数组值应用于它的文本。 你能帮我理解这一点吗? 我们使用的文本对于初学者来说非常困难且写得不好,所以我在这里有点靠自己了。 我已经编写了第一部分,但需要一些有关访问枚举值和分配的帮助,我想我已经接近了,但不明白如何正确获取和设置此值。

编写一个类 MyCourses,其中包含您当前正在学习的所有课程的枚举。 该枚举应该嵌套在您的 MyCourses 类中。 您的班级还应该有一个数组字段,提供每门课程的简短描述(作为字符串)。 编写一个索引器,将枚举的课程之一作为索引并返回课程的字符串描述。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

童话里做英雄 2024-07-24 03:54:05

你已经很接近了,只是最后你有点疯狂了。 CourseName 只是一个数字。 您可以直接索引到 courseDescription 数组中......

courseDescription[(int)index]

然后您就拥有了它。 :)

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

独行侠 2024-07-24 03:54:05

您缺少的是枚举转换为整数。 (事实上​​,在它们的背后,它们基本上都是整数。)

并且数组可以通过整数进行索引。

尝试使用枚举类型的参数对数组进行索引。

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.

蹲在坟头点根烟 2024-07-24 03:54:05

这是我来自 CodeGuru 的回复:

枚举是强类型的,所以你不能将它直接与 int 进行比较。 但是,您可以将枚举强制转换为 int。 所以你会得到这个:

if ( ( ( int ) index ) == 1)
     return courseDescription[0];
else
     return courseDescription[1];

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];
能否归途做我良人 2024-07-24 03:54:05

枚举不会隐式转换为整数。 但您可以显式转换它们。

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

如果您要以这种方式使用枚举,您应该定义它们代表的实际数值。

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

虽然目前枚举的实现是可行的,但没有任何迹象表明它将来会这样做。

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.

来世叙缘 2024-07-24 03:54:05

这里的技巧是枚举的核心是整数数据类型。 这意味着如果您愿意,您可以在 Enum 和 Int32 之间来回转换:

您需要将“get”部分更改为:

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

这有效,因为枚举基本上相当于:

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文