Hi...I came here because I had problems posting at the C# Corner. Hopefully this forum will actually post my question instead of deleting it once I hit post....


I'm working on homework. Yes, I have given it quite some effort, from experimenting to researching. I've even looked everywhere for a C# API documentation, but can't find one. What I'm attempting to do would be so much easier to do with an array, but I have to use an enumeration. In Java, QBasic, HTML, and Visual Basic, I never saw an enumeration, so seeing this introduced in C# (which I'm gradually learning) is throwing me for a loop.

Just to prove I at least know what I'm trying to do and that I'm not entirely stupid, I'll explain how I would code it if it were an array of objects of type Carpet(String type, int priceTimes100). The casing may not be correct, but I'm used to Java casing:

String[,] carpet = new string[8,1];
carpet[0,0] = "Berber";
carpet[0,1] = 810;
carpet[1,0] = "Cable";
carpet[1,1] = 1827;
//...etc

for (int i = 0; i < carpet.length; i++)
[INDENT]Console.Write((i+1) + ": " + carpet[i, 0] + " - $" + ((double)carpet[i,1] / 100));[/INDENT]

Here's the enumeration I'm working with. The prices will be cast to doubles and divided by 100. I've tried everything I can think of to retrieve this data in a for loop, and nothing works.

enum Carpet { Berber = 810, Cable = 1827, Frieze = 747, Indoor_Outdoor = 810, Plush = 1251, Saxony = 1201, Shag = 1205, Sisal = 3801, Textured = 1827 }

I would appreciate any help I can get. Thanks!

What do you mean 'retrieve' the data? You have the data. If you want to use the data, put it in an array and iterate over it.

I'd love to use an array instead, but the guidelines specifically say to use an enumeration. So basically I need a method for retrieving the variable name of the xth integer in the enumeration...sorry if I wasn't clear to begin with.

Thanks. I tried what you suggested, and I'm coming up with build errors yet.

10   enum Carpet { Berber = 810, Cable = 1827, Frieze = 747, Indoor_Outdoor = 810, Plush = 1251, Saxony = 
11                1201, Shag = 1205, Sisal = 3801, Textured = 1827 }
12        
13        static void Main(string[] args)
14        {
15            for (int i = 1; i <= 10; i++)
16                    Console.Write(i + ": " + Enum.GetValues(Carpet)[i]);
17            int input;
18            int.TryParse(Console.ReadLine(), out input);
19
20            Console.Write("\nThe " + Enum.GetValues(Carpet)[input] + " carpet costs " +
21                    (double)(Carpet.Enum.GetValues(Carpet)[input]) + " per square foot.");
22            Console.Read();
23        }

The errors are as follows:
1: 'Carpet' is a 'type' but is used like a 'variable', line 16
2: The best overloaded method match for System.Enum.GetValues(System.Type)' has some invalid arguments, line 16
3: Argument '1': cannot convert from 'Carpet' to 'System.Type', line 16
4: 'Carpet' is a 'type' but is used like a 'variable', line 16
5: The best overloaded method match for System.Enum.GetValues(System.Type)' has some invalid arguments, line 16
6: Argument '1': cannot convert from 'Carpet' to 'System.Type', line 16
7: 'Carpet' does not contain a definition for 'Enum', line 21
8: 'Carpet' is a 'type' but is used like a 'variable'

Got another idea? Or maybe I misimplemented your idea?

First -- mark your code with the C# tag

use typeof(EnumType) if you're expecting a single enum type, or carpet.GetType() if you plan on using different enums

Enum.GetValues(typeof(Carpet))[i]);

Thanks so much for your help! Using Enum.GetNames(typeof(Carpet)), I was able to store the names in a string, and it appears the names are sorted numerically by their stored integer value. I think I can take the rest from here.

String[] names = Enum.GetNames(typeof(Carpet));

            for (int i = 0; i <= names.Length - 1; i++)
            {
                Console.Write(names[i] + "\n");
            }
This question has already been answered. Start a new discussion instead.