-1

Edit to explain how this is not a duplicate question: (feels like maybe this should be in meta)

1 - my question shows ignorance as to what the proper terminology is for the object in question.

2 - my question is NOT about the usefulness at all about the object in question.

These two points are understood by the first line of my question "Apologies in advance, I do not know the terminology so bear with me here." and a clear lack of questioning of what I could do with the object respectfully.


Apologies in advance, I do not know the terminology so bear with me here.

I want to build an object/list that is just like the System.ConsoleColor object.

What I mean is that for ConsoleColor there are a set number of attributes in an array and you can choose something friendly like 'blue' instead of having to remember that 9 is blue.

So for my function I want to be able to do something like the below, where foo is a MyObject that can only be Type1 or Type2.

public void MyFunction(MyObject foo) {
    switch (foo) {
        case MyObject.Type1:
            Console.WriteLine("myobject's type1");
            break;
        case MyObject.Type2:
            Console.WriteLine("myobject's type2");
            break;
    }
}

Thank you.

UpTide
  • 307
  • 3
  • 13
  • 4
    That's called an _enum_. – SLaks May 08 '17 at 22:46
  • What you want is an `enum`. – itsme86 May 08 '17 at 22:46
  • Why the downvotes? Am I not allowed to not know what an `enum` is? – UpTide May 08 '17 at 22:56
  • 2
    I downvoted for the clear lack of research. Just Google "c# System.ConsoleColor", go to the very first link, and you'll see `public enum ConsoleColor` then just search "c# enum"... – Camilo Terevinto May 08 '17 at 22:59
  • 2
    Be aware that enums aren't as restrictive as you might think. There's nothing stopping someone from doing `MyObject bar = (MyObject)3;` when the enum only has definitions for values 1 and 2. – itsme86 May 08 '17 at 23:02
  • You may also find useful these questions and their answers: https://stackoverflow.com/questions/2119714/when-to-use-enums-and-when-to-replace-them-with-a-class-with-static-members, https://stackoverflow.com/questions/7352120/list-of-const-int-instead-of-enum, and https://stackoverflow.com/questions/15398215/storing-and-indexing-constants-in-c-sharp – Peter Duniho May 08 '17 at 23:05

1 Answers1

0

You want an enumeration:

public enum MyObject
{
    Type1,
    Type2
}

public class Program
{
    public static void Main(string[] args)
    {
        MyObject foo = MyObject.Type1;
        switch (foo) {
            case MyObject.Type1:
                Console.WriteLine("myobject's type1");
            break;
            case MyObject.Type2:
                Console.WriteLine("myobject's type2");
                break;
        }
    }
}
Andrew Harris
  • 1,419
  • 2
  • 17
  • 29
  • Thank you! I figured it was easy; will mark as answer after the time limit. – UpTide May 08 '17 at 22:49
  • You could have at least pointed to Documentation or the MSDN... – Camilo Terevinto May 08 '17 at 23:00
  • 1
    @CamiloTerevinto yep, but now they know what they are looking for they can find it themselves. EDIT: See, perfect example! was able to find something themselves. – Andrew Harris May 08 '17 at 23:04
  • @AndrewHarris which is the first thing I did when I saw your object. – UpTide May 08 '17 at 23:04
  • @AndrewHarris Pointing to the right resource (like in this case could be the mark as duplicate, StackOverflow Documentation or MSDN) is far better than giving the OP the code – Camilo Terevinto May 08 '17 at 23:07
  • 1
    Whole point was they didn't know what it was called to be able to ask the right question. 1st line of the question "Apologies in advance, I do not know the terminology so bear with me here." – Andrew Harris May 08 '17 at 23:09