ZetCode

FreeBasic As Keyword

last modified June 16, 2025

The FreeBasic As keyword is used for type declarations and type casting. It specifies the data type of variables, parameters, and function return values. The As keyword is fundamental for explicit typing in FreeBasic.

Basic Definition

In FreeBasic, As serves two main purposes: declaring the type of variables and performing type casting. It makes the code more readable and helps prevent type-related errors.

The As keyword is used with variable declarations, function parameters, and return types. It ensures strong typing which improves code safety and maintainability.

Variable Declaration with As

This example shows basic variable declarations using the As keyword.

as_declaration.bas
Dim langName As String = "FreeBasic"
Dim age As Integer = 25
Dim price As Double = 19.99
Dim isActive As Boolean = True

Print langName
Print age
Print price
Print isActive

Here we declare four variables with explicit types using As. Each variable is initialized with a value matching its declared type. The Print statements output the variable values.

Function Return Type with As

The As keyword specifies function return types.

as_function.bas
Function AddNumbers(a As Integer, b As Integer) As Integer
    Return a + b
End Function

Dim result As Integer = AddNumbers(5, 7)
Print "The sum is: "; result

This example defines a function that returns an integer. The return type is specified after the parameter list using As Integer. The function is called and its result is stored in a variable.

Type Casting with As

The As keyword can perform explicit type conversion.

as_casting.bas
Dim number As Double = 3.14159
Dim intPart As Integer = number As Integer
Dim strVersion As String = number As String

Print "Original: "; number
Print "Integer part: "; intPart
Print "String version: "; strVersion

Here we convert a double to an integer and a string using As. The decimal part is truncated when casting to integer. The string conversion preserves all digits. Note that some precision may be lost in conversions.

Array Declaration with As

Arrays can be declared with specific types using As.

as_array.bas
Dim numbers(1 To 5) As Integer
Dim names(0 To 2) As String = {"Alice", "Bob", "Charlie"}

For i As Integer = 1 To 5
    numbers(i) = i * 10
Next

For i As Integer = 0 To 2
    Print names(i)
Next

This code declares an integer array and a string array using As. The integer array is filled with values in a loop. The string array is initialized with names. Both arrays are processed using loops.

User-Defined Types with As

The As keyword is used with user-defined types (UDTs).

as_udt.bas
Type Person
    name As String
    age As Integer
End Type

Dim p As Person
p.name = "John"
p.age = 30

Print "Name: "; p.name
Print "Age: "; p.age

We define a Person type with name and age fields. A variable p is declared as this type using As Person. The fields are then assigned values and printed.

Pointer Types with As

Pointers are declared using As with the pointer specifier.

as_pointer.bas
Dim value As Integer = 42
Dim ptr As Integer Ptr = @value

Print "Value: "; value
Print "Pointer value: "; *ptr

*ptr = 100
Print "New value: "; value

This example shows pointer declaration using As. We create an integer pointer that points to value. The pointer is dereferenced to access and modify the original variable's value.

Procedure Parameters with As

Subroutine parameters use As for type specification.

as_parameters.bas
Sub PrintInfo(name As String, age As Integer)
    Print "Name: "; name
    Print "Age: "; age
End Sub

PrintInfo("Sarah", 28)

The PrintInfo subroutine takes two parameters with explicit types. The As keyword ensures type safety for these parameters. The subroutine is called with appropriate arguments.

Best Practices

This tutorial covered the FreeBasic As 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.