FreeBasic Type Keyword
last modified June 16, 2025
The FreeBasic Type keyword allows creating user-defined types (UDTs).
These are composite data types that group related variables together. UDTs help
organize complex data structures in your programs.
Basic Definition
In FreeBasic, Type defines a new data structure containing one or
more members. Each member can be of any valid FreeBasic data type, including
other UDTs. Types are similar to structs in C or records in Pascal.
UDTs provide better code organization, improved readability, and easier data
management. They are essential for object-oriented programming in FreeBasic.
The Type block must end with End Type.
Simple Type Declaration
This example shows how to declare a basic user-defined type.
Type Point
x As Integer
y As Integer
End Type
Dim p As Point
p.x = 10
p.y = 20
Print "Point coordinates: ("; p.x; ", "; p.y; ")"
Here we define a Point type with x and y coordinates. We then
declare a variable of this type and access its members using dot notation.
This creates a logical grouping for related data.
Type with Different Data Types
Types can contain members of various data types.
Type Employee
name As String
age As Integer
salary As Double
isActive As Boolean
End Type
Dim emp As Employee
emp.name = "John Smith"
emp.age = 42
emp.salary = 55000.75
emp.isActive = True
Print "Employee: "; emp.name
Print "Age: "; emp.age
Print "Salary: $"; emp.salary
Print "Active: "; emp.isActive
This Employee type combines string, integer, double, and Boolean
members. Each member stores different aspects of employee data. The example
shows how to initialize and access these members.
Type with Arrays
Types can contain array members for storing multiple values.
Type Student
name As String
grades(4) As Integer ' Array of 5 grades
End Type
Dim s As Student
s.name = "Alice"
s.grades(0) = 85
s.grades(1) = 90
s.grades(2) = 78
s.grades(3) = 92
s.grades(4) = 88
Print "Student: "; s.name
Print "Grades:";
For i As Integer = 0 To 4
Print " "; s.grades(i);
Next
Print
The Student type includes a string for the name and an integer
array for grades. This demonstrates how to work with array members within
a type, including initialization and access.
Nested Types
Types can contain other types as members, enabling complex data structures.
Type Address
street As String
city As String
zip As String
End Type
Type Person
name As String
age As Integer
home As Address
End Type
Dim p As Person
p.name = "Bob Johnson"
p.age = 35
p.home.street = "123 Main St"
p.home.city = "Springfield"
p.home.zip = "12345"
Print "Name: "; p.name
Print "Address: "; p.home.street; ", "; p.home.city; " "; p.home.zip
This example shows a Person type that contains an Address
type. We access nested members using multiple dot operators. This creates a
hierarchical data structure.
Type with Procedures
FreeBasic allows adding procedures (methods) to types for object-oriented programming.
Type Rectangle
width As Double
height As Double
Declare Function Area() As Double
Declare Sub Resize(newWidth As Double, newHeight As Double)
End Type
Function Rectangle.Area() As Double
Return width * height
End Function
Sub Rectangle.Resize(newWidth As Double, newHeight As Double)
width = newWidth
height = newHeight
End Sub
Dim rect As Rectangle
rect.width = 10.5
rect.height = 20.3
Print "Initial area: "; rect.Area()
rect.Resize(15.0, 25.0)
Print "New area: "; rect.Area()
Here we define a Rectangle type with data members and methods.
The Area function calculates the area, while Resize
modifies the dimensions. This demonstrates basic object-oriented capabilities.
Type Initialization
Types can be initialized using various methods in FreeBasic.
Type Book
title As String
author As String
pages As Integer
price As Double
End Type
' Method 1: Individual member assignment
Dim b1 As Book
b1.title = "The Hobbit"
b1.author = "J.R.R. Tolkien"
b1.pages = 310
b1.price = 12.99
' Method 2: Initializer list
Dim b2 As Book = ("Dune", "Frank Herbert", 412, 15.50)
' Method 3: With statement
Dim b3 As Book
With b3
.title = "1984"
.author = "George Orwell"
.pages = 328
.price = 9.99
End With
Print b1.title; " by "; b1.author
Print b2.title; " by "; b2.author
Print b3.title; " by "; b3.author
This example shows three ways to initialize type variables: individual member
assignment, initializer lists, and the With statement. Each method
has its advantages depending on the situation.
Type Pointers
Pointers to types allow efficient manipulation of type variables.
Type Car
make As String
model As String
year As Integer
End Type
Dim myCar As Car
myCar.make = "Toyota"
myCar.model = "Corolla"
myCar.year = 2020
Dim pCar As Car Ptr = @myCar
Print "Car details:"
Print "Make: "; pCar->make
Print "Model: "; pCar->model
Print "Year: "; pCar->year
Here we create a pointer to a Car type variable. The arrow
operator (->) accesses members through the pointer. This is
useful for passing large types efficiently.
Best Practices
- Naming: Use PascalCase for type names to distinguish them from variables.
- Organization: Group related data together logically in types.
- Initialization: Consider using constructors for complex types.
- Encapsulation: Use private members and methods when appropriate.
- Documentation: Comment type definitions for clarity.
This tutorial covered the FreeBasic Type keyword with practical
examples showing its usage in different scenarios. Types are powerful tools
for organizing complex data in your programs.
Author
List all FreeBasic Tutorials.