ZetCode

FreeBasic Is Keyword

Last modified October 18, 2026

In FreeBasic, the Is keyword is used in the Case Is clause of a Select Case statement to compare the selector with a value using a relational operator. Comparing object references is done with the equality operator (=) on pointers.

Basic Definition

Case Is is followed by a relational operator (>, >=, <, <=, =, <>) and a value. The case matches when the Select Case expression satisfies that comparison.

Object references created with New are pointers. Two pointers refer to the same object when their addresses are equal, which is tested with the = operator. The binary Is operator is not accepted between pointers in FreeBasic 1.10.1.

Case Is in Select Case

The Is keyword appears in the Case Is clause, which compares the Select Case expression with a value using a relational operator.

case_is.bas
Dim As Integer score = 85

Select Case score
    Case Is >= 90
        Print "A"
    Case Is >= 80
        Print "B"
    Case Else
        Print "C or lower"
End Select

Here score is 85, so the first case fails and Case Is >= 80 matches, printing "B".

Comparing Simple Objects

This example compares object references (pointers) with the = operator.

is_simple.bas
Type Person
    As String fname
End Type

Dim As Person Ptr p1 = New Person
Dim As Person Ptr p2 = p1
Dim As Person Ptr p3 = New Person

If p1 = p2 Then
    Print "p1 and p2 reference the same object"
Else
    Print "p1 and p2 reference different objects"
End If

If p1 = p3 Then
    Print "p1 and p3 reference the same object"
Else
    Print "p1 and p3 reference different objects"
End If

Delete p1
Delete p3

Here we create a Person object and assign its reference to p1 and p2. The = comparison returns true because both variables point to the same memory location. p3 refers to a separate object, so that comparison returns false. We must delete each object when done.

Comparing Different Objects

This example shows how = behaves with distinct objects.

is_different.bas
Type Car
    As String model
End Type

Dim As Car Ptr c1 = New Car
Dim As Car Ptr c2 = New Car

If c1 = c2 Then
    Print "Same car object"
Else
    Print "Different car objects"
End If

Delete c1
Delete c2

We create two separate Car objects. Even if their contents were identical, the = comparison returns false because the pointers refer to distinct instances in memory. Always remember to delete dynamically allocated objects.

Is With Nothing

A null object reference is tested by comparing the pointer with 0.

is_nothing.bas
Type Book
    As String title
End Type

Dim As Book Ptr b = 0

If b = 0 Then
    Print "Book reference is null"
Else
    Print "Book reference is valid"
End If

b = New Book

If b <> 0 Then
    Print "Book reference is valid"
End If

Delete b

This code checks if a Book pointer is null by comparing it with 0. FreeBASIC has no Nothing keyword, so 0 is used as the null reference. The comparison correctly identifies the null reference.

Is With Arrays

Pointers to array elements can be compared with the = operator.

is_array.bas
Dim As Integer arr1(1 To 3) = {1, 2, 3}
Dim As Integer Ptr p1 = @arr1(1)
Dim As Integer Ptr p2 = p1

If p1 = p2 Then
    Print "Same address"
End If

Print "Element via p2: "; *p2

Here p2 is assigned the address of the first element of arr1, so p1 and p2 point to the same location and the = comparison returns true. The comparison tests addresses, not array contents. For content comparison, use a loop.

Is With Function Return Values

This example uses = to check function return values.

is_function.bas
Type Object1
    As Integer value
End Type

Function CreateObject(create As Boolean) As Object1 Ptr
    If create Then
        Return New Object1
    Else
        Return 0
    End If
End Function

Dim As Object1 Ptr obj = CreateObject(False)

If obj = 0 Then
    Print "No object was created"
Else
    Print "Object created successfully"
    Delete obj
End If

The function returns either a new object or null. We use = to check which case occurred. This pattern is common in factory functions where creation might fail.

Is With Class Methods

Type methods can use = to compare object instances.

is_method.bas
Type Node
    As Integer value
    Declare Function IsSame(other As Node Ptr) As Boolean
End Type

Function Node.IsSame(other As Node Ptr) As Boolean
    Return @This = other
End Function

Dim As Node Ptr n1 = New Node
Dim As Node Ptr n2 = n1

Print "Same node: "; n1->IsSame(n2)

Delete n1

The Node type includes a method that uses = to compare the current instance with another. The @ operator gets the address of This, the current object. The method correctly identifies when two references are equal.

Is With Inheritance

Pointers to inherited types can be compared with the = operator after a cast to a common pointer type.

is_inheritance.bas
Type Animal
    As String fname
End Type

Type Dog Extends Animal
    As String breed
End Type

Dim As Animal Ptr a = New Dog
Dim As Dog Ptr d = New Dog

If Cast(Any Ptr, a) = Cast(Any Ptr, d) Then
    Print "Same animal instance"
Else
    Print "Different animal instances"
End If

Delete a
Delete d

Even though a and d are different types (Animal and Dog), the pointers can be compared after casting them to Any Ptr. The comparison returns false here because they are separate instances.

Best Practices

This tutorial covered the FreeBasic Is keyword in the Case Is clause and showed how to compare object pointers with =.

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.