tags:

views:

85

answers:

3

This should be fairly simple question. I'm using DocX library to create new word documents. I wanted to make a test word document to see how each TableDesign (enum) looks like to choose the one I need.

Designs\Styles that can be applied to a table. Namespace: Novacode Assembly: DocX (in DocX.dll) Version: 1.0.0.10 (1.0.0.10)

Syntax:

public enum TableDesign

Member name
Custom
TableNormal
TableGrid
LightShading
LightShadingAccent1
....

And so on. I would like to get a list of those TableDesign's so i could reuse it in a method creating new table with new design for all possibilities, but I don't really know how to get the list from that enum:

foreach (var test in TableDesign) {
      createTable(documentWord, test);
}

How do I get that?

+4  A: 

Found answer myself:

    // get a list of member names from Volume enum,
    // figure out the numeric value, and display
    foreach (string volume in Enum.GetNames(typeof(Volume)))
    {
        Console.WriteLine("Volume Member: {0}\n Value: {1}",
            volume, (byte)Enum.Parse(typeof(Volume), volume));
    }

For my specific case I've used:

 foreach (var test in Enum.GetNames(typeof(TableDesign))) {
     testMethod(documentWord, test);
 }

and in testMethod I've:

tableTest.Design = (TableDesign) Enum.Parse(typeof(TableDesign), test); 

It worked without a problem (even if it was slow, but I just wanted to get things quickly (and being onetimer performance didn't matter).

Maybe it will help someone in future too :-)

MadBoy
If the enum consists of sequential values with a known start and stop, it would be simpler to just loop on the enum (or an int equivalent thereof). The above method is *slow*.
Steven Sudit
I've updated the post to actually what I've used. It was copy paste from different source that gave me a head start how it should be used (more or less). Didn't need the performance. One time use only :-)
MadBoy
+1  A: 

Alternately:

foreach (var volume in Enum.GetValues(typeof(Volume))) 
{ 
    Console.WriteLine("Volume Member: {0}\n Value: {1}", 
        volume, (int) volume); 
} 

GetValue will return an Volume[] of the values as enums. Printing an enum value will call its ToString(), rendering it by it name. Casting to int (better than byte) will give its number.

James Curran
*its* instead of *it's*
Yuriy Faktorovich
@Yuriy: As much fun as it is to correct Americans on their English, I edited it to remove those errors, so you might as well remove your comment. :-)
Steven Sudit
Yes, this is faster and somewhat less convoluted. I still wouldn't call it fast, though.
Steven Sudit
A: 
Adam