enumeration help

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

Can someone explain, or point me to a good link that explains enumerations
for a newbie.

Thats
 
I always think of enumerations as a logical grouping of numerical constants.
A bonus of enumerations is that you can assign multiple values to a single
variable.

enum fileaccess
faReadLock =0
faWriteLock=1
faReadShare=2
faWriteShareRead=4
faWriteShareWrite = 8
end enum

dim pintFileAccess as fileaccess = fileaccess.faWriteShareRead and
fileaccess.faWriteShareWrite

when you look at the properties of a control in the designer, many times you
will notice use of enumerations. Windows.WindowsState is an enumeration

FormWindowState.Normal = 0,
FormWindowState.Minimized = 1 ,
FormWindowState.Maximized = 2

Enumerations reduce ambiguity
 
Hello AMDRIT,

The code you propose below will give pintFileAccess a value of faReadLock
(0). I believe you were trying to give it a value that would express both
the faWriteShareRead and faWriteShareWrite values (12). You would produce
this value by using a bitwise OR instead of an AND.

Dim pintFileAccess As FileAccess = FileAccess.faWriteShareRead OR FileAccess.faWriteShareWrite

-Boo
 
AMDRIT said:
enum fileaccess
faReadLock =0
faWriteLock=1
faReadShare=2
faWriteShareRead=4
faWriteShareWrite = 8
end enum

dim pintFileAccess as fileaccess = fileaccess.faWriteShareRead and
fileaccess.faWriteShareWrite

The assignment above does /not/ do what you want:
Dim pintFileAccess As fileaccess _
= fileaccess.faWriteShareRead And fileaccess.faWriteShareWrite

this works out to

pintFileAccess = 2 And 8
= 0 !!

I think you meant to say

Dim pintFileAccess as fileaccess _
= fileaccess.faWriteShareRead Or fileaccess.faWriteShareWrite

Also, if you're going to do this sort of "bit-wise" combination, you
should add the Flags() attribute to your Enumeration.

Contrast ...

Enum FileAccess
.. . .
Dim a As FileAccess _
= FileAccess.faWriteShareRead Or FileAccess.faReadShare
? a.ToString()
12

.... with ...

<Flags()> Enum FileAccess
.. . .
Dim a As FileAccess _
= FileAccess.faWriteShareRead Or FileAccess.faReadShare
? a.ToString()
"faWriteShareRead, faWriteShareWrite"

Note the inherent portability of the latter.

HTH,
Phill W.
 
Thank you GhostInAk and Phil W. for pointing out my error. Despite that, I
hope the explaination answers Jason's question.
 
Back
Top