ZetCode

FreeBasic Enum Keyword

Last modified October 18, 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.

Basic Definition

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.

Simple Enum Declaration

This example shows a basic enum declaration and usage.

enum_simple.bas
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.

Enum With Explicit Values

You can assign specific values to enum constants.

enum_explicit.bas
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.

Enum With Bit Flags

Enums can be used to create bit flags for combining options.

enum_flags.bas
Enum FilePermissions
    fpRead = 1
    fpWrite = 2
    fpExecute = 4
End Enum

Dim myPermissions As Integer = fpRead Or fpWrite

If (myPermissions And fpRead) = fpRead Then
    Print "Read permission granted"
End If
If (myPermissions And fpExecute) = 0 Then
    Print "Execute permission not granted"
End If

This example shows how to use enum constants for bitwise operations. Each permission is a power of two, allowing combination with OR operations. We check for specific permissions using AND operations. Names such as Read, Write and Execute are FreeBasic keywords, so the constants are prefixed with fp. The combined value is stored in an Integer variable because bit-combined values are not members of the enum itself.

Enum In Switch Statement

Enums work well with Select Case statements for clear branching.

enum_switch.bas
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.

Enum With Custom Base Type

FreeBasic allows specifying the underlying type for an enum.

enum_type.bas
Enum SmallEnum
    Value1
    Value2
    Value3
End Enum

Enum LargeEnum
    BigValue1 = 100000
    BigValue2
    BigValue3
End Enum

Dim As Byte small = Value1
Dim large As LargeEnum = BigValue3

Print "Size of SmallEnum: "; SizeOf(SmallEnum)
Print "Size of Byte: "; SizeOf(Byte)
Print "LargeEnum value: "; large

FreeBasic does not allow an underlying type to be specified in an Enum declaration (Enum X As Byte is a syntax error). An enum instance always has the size of an Integer, so SmallEnum occupies the same storage as any other enum. If a smaller storage size is needed, store the value in a Byte variable, as shown above. The output on a 64-bit build shows SizeOf(SmallEnum) as 8, because Integer is 8 bytes there.

Enum As Function Parameters

Enums can be used as function parameters for type safety.

enum_function.bas
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. The member is named Error_ because Error is a FreeBasic keyword and cannot be used as an enum member name; square brackets do not escape it in an enum declaration. The program prints the numeric value of the level, so the output is [ 1] Application started and [ 3] File not found.

Enum With A Helper Function

Enums cannot contain member procedures and FreeBasic has no Extends keyword for enums. A regular function that takes the enum as a parameter provides the same functionality.

enum_methods.bas
Enum Colors
    Red
    Green
    Blue
End Enum

Function ColorName(c As Colors) As String
    Select Case c
        Case Red: Return "Red"
        Case Green: Return "Green"
        Case Blue: Return "Blue"
    End Select
    Return ""
End Function

Dim favorite As Colors = Green
Print "Favorite color: "; ColorName(favorite)

Here we use a helper function ColorName to convert enum values to human-readable strings. The function is called with the enum value as its argument. The output is Favorite color: Green.

Best Practices

This tutorial covered the FreeBasic Enum keyword with practical examples showing its usage in different scenarios.

Author

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.