Last modified April 20, 2026
The FreeBasic Enum keyword defines an enumeration type, which
contains a set of named constants. Enums make code more readable by replacing
magic numbers with meaningful names.
In FreeBasic, Enum creates a new type consisting of named integer
constants. Each constant in the enumeration has an underlying integer value.
By default, the first enumerator has value 0, and each subsequent enumerator is incremented by 1. You can explicitly assign values to enumerators. Enums improve code clarity and maintainability.
This example shows a basic enum declaration and usage.
Enum Days Sunday Monday Tuesday Wednesday Thursday Friday Saturday End Enum Dim today As Days = Wednesday Print "Today is day number: "; today
Here we define a Days enum with seven constants representing
weekdays. The first value (Sunday) gets 0, Monday gets 1, and so on. We
declare a variable of type Days and print its numeric value.
You can assign specific values to enum constants.
Enum HttpStatus OK = 200 Created = 201 Accepted = 202 BadRequest = 400 Unauthorized = 401 NotFound = 404 End Enum Dim response As HttpStatus = NotFound Print "HTTP Status: "; response
This enum defines common HTTP status codes with their standard numeric values.
We assign the NotFound value to a variable and print it. Using
named constants makes the code more understandable than using raw numbers.
Enums can be used to create bit flags for combining options.
Enum FilePermissions Read = 1 Write = 2 Execute = 4 End Enum Dim myPermissions As FilePermissions = Read Or Write If (myPermissions And Read) = Read Then Print "Read permission granted" End If
This example shows how to use enums for bitwise operations. Each permission is a power of two, allowing combination with OR operations. We check for specific permissions using AND operations. This is common in system programming.
Enums work well with Select Case statements for clear branching.
Enum TrafficLight Red Yellow Green End Enum Dim light As TrafficLight = Yellow Select Case light Case Red Print "Stop" Case Yellow Print "Caution" Case Green Print "Go" End Select
We define a traffic light enum and use it in a Select Case
statement. The code becomes very readable as each case clearly describes
the action for each light state. Enums eliminate magic numbers in such logic.
FreeBasic allows specifying the underlying type for an enum.
Enum SmallEnum As Byte Value1 Value2 Value3 End Enum Enum LargeEnum As Integer BigValue1 = 100000 BigValue2 BigValue3 End Enum Print "Size of SmallEnum: "; SizeOf(SmallEnum) Print "Size of LargeEnum: "; SizeOf(LargeEnum)
Here we declare two enums with different underlying types. SmallEnum
uses Byte (1 byte), while LargeEnum uses Integer (4 bytes). This
can be useful for memory optimization or when needing larger enum values.
Enums can be used as function parameters for type safety.
Enum LogLevel Debug Info Warning [Error] Critical End Enum Sub LogMessage(level As LogLevel, message As String) Print "["; level; "] "; message End Sub LogMessage(Info, "Application started") LogMessage([Error], "File not found")
This example demonstrates using an enum as a function parameter. The
LogMessage function accepts a LogLevel enum,
ensuring only valid log levels can be passed. Note the brackets around
Error as it is a FreeBasic keyword.
Enums can be extended with methods using the Extends keyword.
Enum Colors Red Green Blue End Enum Function Colors.ToString(c As Colors) As String Select Case c Case Red: Return "Red" Case Green: Return "Green" Case Blue: Return "Blue" End Select End Function Dim favorite As Colors = Green Print "Favorite color: "; Colors.ToString(favorite)
Here we extend the Colors enum with a ToString
method. This allows converting enum values to human-readable strings. The
method is called using the enum type name followed by the method name.
This tutorial covered the FreeBasic Enum keyword with practical
examples showing its usage in different scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all FreeBasic Tutorials.