Resolved How do you add an enum?

Joined
Aug 10, 2020
Messages
19
Programming Experience
1-3
Hello. I'm trying to make something like a random card-draw generator Windows Form app .(Net Framework). I have all the code for one--which has enums--and I am trying to convert it. But I can't figure out how you create new enums. Thanks in advance!

C#:
namespace Exercise9
{
    /// <summary>
    /// An enumeration for card ranks
    /// </summary>
    public enum Rank
    {
        Ace,
        Two,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen,
        King
    }
}
namespace Exercise9
{
    /// <summary>
    /// An enumeration for card suits
    /// </summary>
    public enum Suit
    {
        Clubs,
        Diamonds,
        Hearts,
        Spades
    }
}
 
Last edited by a moderator:
*sigh* I was hoping you had a different response.

The issue is that line 9 is not allowed to exist where it currently is. You can't just have a chunk of code floating where declarations are expected by the compiler. Code needs to live inside methods (or getters/setters).

This is valid:
C#:
class Car
{
    int [] gears = { 1, 2, 3, 4 };

    void Baz()
    {
        int currentGear;
        currentGear = gears[0];
    }
}

This is invalid:
C#:
class Car
{
    int [] gears = { 1, 2, 3, 4 };

    int currentGear;
    currentGear = gears[0];
}
That fixed it. Thanks!
 
You should already know how to structure your code, but do you understand why it works?

And is there a reason you're avoiding my questions?
 
Of course I did. Why do you think I was telling you to go back to post 4...

You've changed what you were doing because you didn't know what you were doing in the first place.

Now since changing your code and question, do you understand why Skydiver's example works?
 
Back
Top Bottom