what type would this array be?

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

I think I might create an enumeration with the values Off, Red, Yellow,
Blue, and Overloaded and I might use this for bitwise comparison.

So, when initializing the arrays, if I wanted to load the values "Off"
or "Red" into my arrays instead of "0" and "1", would this still be a
string array? Or would it be an array of the type of my enum? I would be
reading strings from the text file, I guess, but I wasn't sure if this
would be interpreted as enum values (Off and Red), or if another step
was necessary to convert them. If so, then I could just convert my
arrays of 0s and 1s into Offs and Reds, but I don't know how. A foreach
loop, maybe?
 
Or would it be an array of the type of my enum?
It depends on how you declare you array.
If you declare it as: YourEnumType[] theArray, then yes.
I would be
reading strings from the text file, I guess, but I wasn't sure if this
would be interpreted as enum values (Off and Red), or if another step
was necessary to convert them.
Yes, convertion is necessary. Use Enum.Parse. If you decorated your
enum declaration with FlagsAttribute, that method can even parse
composite value in the form like "Red, Yellow, Blue".

To convert int to enum and vice versa, you only need to cast it.
 
Truong said:
Or would it be an array of the type of my enum?

It depends on how you declare you array.
If you declare it as: YourEnumType[] theArray, then yes.
I would be
reading strings from the text file, I guess, but I wasn't sure if this
would be interpreted as enum values (Off and Red), or if another step
was necessary to convert them.

Yes, convertion is necessary. Use Enum.Parse. If you decorated your
enum declaration with FlagsAttribute, that method can even parse
composite value in the form like "Red, Yellow, Blue".

To convert int to enum and vice versa, you only need to cast it.

What's the difference between [Flags] and [FlagsAttribute]? I've seen
both used in the help files for what seemed like the same thing.
 
They are just the same thing. The class name in .NET library is
"XXXAttribute". In C#, you can optionally leave the work "Attribute"
out. The C# compiler understands.
 
Back
Top