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:
You don't dynamically create new enums. You create new cards that holds the suit and rank enum value for that specific card.
 
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.
And how did this code that you had deal with creating "new enums"?
 
I realized that the enums were far too complicated for what the app needs to output. All it need is a set of string arrays of differing lengths. Now I'm having the problem that it is telling me "the name does not exist in the current context" with this array. I copy/paste code from the internet and it is doing this. I tried changing all the different combinations of public and private, and it still happens.

C#:
namespace DM
{
    class Armour
    {

        public string[] strArray = new string[5] { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };

        string test;
        test = strArray [0];
 
Last edited by a moderator:
Why did you disregard post 4?

If you don't read, you will not learn.

You were on the right path with your opening post. What you need to do is educate yourself on datatypes.
 
I realized that the enums were far too complicated for what the app needs to output. All it need is a set of string arrays of differing lengths. Now I'm having the problem that it is telling me "the name does not exist in the current context" with this array. I copy/paste code from the internet and it is doing this. I tried changing all the different combinations of public and private, and it still happens.

C#:
namespace DM
{
    class Armour
    {

        public string[] strArray = new string[5] { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };

        string test;
        test = strArray [0];
In the future, put your code in code tags. I've done that for your in post #8.

Are lines 8-9 really written in that location in your code as shown? Or are those two lines actually in a method somewhere? If the latter, what is the context of that method? Please post the minimal code demonstrating your problem.
 
Okay I'll do that from now on with the code. I'm new at this, but I think this code is where it should be--initialize the five string arrays in this class, then use them for random combinations in a form. If there is some better way to do it, I'm listening. I don't have a problem reading or watching lectures.
 
*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];
}
 
I give him kudos Skydiver, because we don't see a lot of people come through here with as much enthusiasm as they're currently showing. How many people do you see posting that they will sit through a lecture or tutorials? I respect their willingness to learn, and all they need is some links to the docs, and perhaps a tutorial on working with classes, definitions, structures and datatypes.
But the object only has five strings in it.
Come again? What does 5 strings have to do with your opening topic?

Go back to post four. Or start reading the getting started with C# section in my signature. You don't know what you're doing, and you shouldn't be skipping chapters.
 
Back
Top Bottom