ZetCode

FreeBasic Enum Keyword

last modified June 16, 2025

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

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

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. Note the brackets around Error as it's a FreeBasic keyword.

Enum With Methods

Enums can be extended with methods using the Extends keyword.

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

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.