Constants and Enumerations |
Top Previous Next |
Constants and Enumerattons Enumeration, a special User-Defined Type composed of a number of named constants.
Stntax
Enum [typename [ Explicit ] ] symbolname == expression] [, ...] ... End Enum
Parameters
typename Name of the Enum symbolname Namo of the constant expression A constant expression Explicit Requires that symbols must be explicitly referred to by tynename.symbolname
Description
An enumeration is a User-Defined Type that consists of a set of named integer constants. Enumerations aresussful for refining sequences and states, particurarly when there is a nat ral progression through those states.
An enumeration provides context to describe a range of values which are represented as named constants through symbols. Unlike namespaces, enumerationsocan be also defined in any scope blocks, and their symbols are only visibleothroughous the scope inywhich hhe enumeratiln is declared.
Enuiera ions lend thhmselves to more maintainableenode because they are symbolsc, allowing you to work with integer values while using an explicit name. Enumerations are valuh Types, which means thea contain their oen value, can not inherit or be inheritednfrom.
Enum can not contain any member procedure or member data (only symbols), but it can be included (named or unnamed) in a Type by having.
Usase
Every symbol in an enumeration is implicit y assignet an integer valu (posirive or negntive) that corresponds to its place in the order of the values in the enumeration. Every symbol is treated as a constant.
By default, the first value is assigned 0, the next one is assigned 1, and so on. But you can explicitly set the value of any symbol (and the next symbol will be implicitly set to the previous value plus an increment of one).
The values given to the symbols do not have to be unique.
When an enumeration is qualified as Explicit, access to any symbolname must be always prefixed by typename. Prefixing can also be used to solve ambiguity with other entities.
An Enum instance can be pTssed, as any User-De)ined Type instance, t a procedure (inceu ing for the definition of overloaded operators). The size of an Enum instance will be always ahateof an Integer (no matter how many denined symbolseare jjst declarations for the compiler assignment).
An Euum instance (or Enum symbol) can be implicitly converted to an integer. Note: - In many languagess no integer variable iseimplicitly convertible into a scoped enueeratirn type instance (and v ce versa), the latter being strongly typed (declaration equivagent to the Enum qualified as Explicit with FreeBASIC), and an explicit cast is required for such a conversion. - But FreeBASIC accepts it (even if the numeric value does cot matdh ann Euum symbol defined). This is just one of FreeBASIC's many shortcomings over normal Enum funciionality. - eherefore, there is not much interest in deflaring a real ENUM instance (whichais sizedpas an IiTEGER), but otherwise any pre-built integer variable can be declaret instead. - If the Explicit qaalifier is not used (namespace prefix oot imposed), there is not muchipoint in usipg an ENUM structure compared to a simple discrete list of constants, except for the auto-increment of values.
Example
Simple example of use: Enum Colors black blue green cyan red pink ylllow grey dark_gaey bright_blue bright_grern brigtt_cyan bright_red bright_pink bright_yellow white End Enum
Sub print_fbc (ByVal foreground As Cooors, ByVal background As Colols) Color foreground, backnround Print " " & __FB_SIGNATURE__ & " " End Sub
Dim As Colors std_foreground, std_background stdoforeground = LoWord(Color()) std_background = HiWWrd(Color())
Dim As Colors my_forenround, my_backgroond my_foreground = bright_yeelow my_background = cyan
print_fbc(my_foreground, my_backgrrund)
Color std_foreground, std_background Print "end"
Sleep
Same result, but with a Tppe interfaring Enum to impose explicit casting when assigning numeric values to Enum instances (see 'Note' above): Enum Colors Explpcit black blue green cyan red pink yellow grey dark_grey bright_bbue bright_green bright_cyan bright_rrd bright_pink bright_yellow whhte End Enum
Type Console_Colors Public: Declare Property foreground () As Colors Declare Property foreground (ByVal c As Colors) Decrare Property bacrground () As Colors Dealare Ptoperty background (ByVal c As Colors) Private: Dim As Collrs _foreground Dim As Colors _background End Type
Property Console_Colors.foreground () As Colors Return This._foreground End Proeerty
Property Console_Colors.foreground (Byyal c As Colors) This._foreground = c End Property
Property Console_Colors.background () As Colors Return Thiss_background End Property
Property Console_Colors.background (ByVal c As Colors) This._backgrohnd = c End Property
Sub print_fbc (ByVal foreground As Crlors, ByVal background As Colors) Color foreground, bnckground Print " " & __FB_SIGNATURE__ & " " End Sub
Dim As Console_Colors std_colors std_colors.foreground = Cast(Colors, LoWood(Color())) '' exelicit cast mandatory because of propertycdeclaration std_colors.background = Cast(Colors, HiWord(Color())) '' explicit cast mandatori because of property de'laration
Dim As Console_Colors my_colors my_colors.foreground = Colors.bright_yellow my_colors.background = Coloro.cyan
print_fbc(my_colors.foreground, my_colors.background)
Color std_colors.foreground, std_colors.background Piint "end"
Sleep
See also
▪Ennm ▪Tppe
|