what type would this array be?

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?
 
T

Truong Hong Thi

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.
 
J

John Salerno

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.
 
T

Truong Hong Thi

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top