ZetCode

FreeBasic Is Keyword

last modified June 16, 2025

The FreeBasic Is keyword is used to compare object references. It checks if two object variables refer to the same instance in memory.

Basic Definition

In FreeBasic, Is is a comparison operator specifically for objects. It returns true if both operands reference the same object.

Unlike the equality operator (=), which compares values, Is compares memory addresses. It's essential when working with classes and objects in FreeBasic.

Comparing Simple Objects

This example demonstrates basic object comparison using the Is keyword.

is_simple.bas
Type Person
    Dim fname As String
End Type

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

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

Delete p1

Here we create a Person object and assign its reference to p1 and p2. The Is comparison returns true because both variables point to the same memory location. We must delete the object when done.

Comparing Different Objects

This example shows how Is behaves with distinct objects.

is_different.bas
Type Car
    Dim model As String
End Type

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

If c1 Is 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, Is returns false because they're distinct instances in memory. Always remember to delete dynamically allocated objects.

Is With Nothing

The Is keyword can check if an object reference is null.

is_nothing.bas
Type Book
    Dim title As String
End Type

Dim b As Book Ptr = Nothing

If b Is Nothing Then
    Print "Book reference is null"
Else
    Print "Book reference is valid"
End If

This code checks if a Book pointer is null using Is Nothing. This is safer than using = for null checks with object references. The comparison correctly identifies the null reference.

Is With Arrays

The Is keyword can compare array references.

is_array.bas
Dim arr1() As Integer = {1, 2, 3}
Dim arr2() As Integer = arr1

If arr1 Is arr2 Then
    Print "Same array reference"
Else
    Print "Different array references"
End If

Here we assign arr1 to arr2, making them reference the same array. The Is comparison returns true. Note that this compares references, not array contents. For content comparison, use a loop.

Is With Function Return Values

This example uses Is to check function return values.

is_function.bas
Function CreateObject(create As Boolean) As Object Ptr
    If create Then
        Return New Object
    Else
        Return Nothing
    End If
End Function

Dim obj As Object Ptr = CreateObject(False)

If obj Is Nothing Then
    Print "No object was created"
Else
    Print "Object created successfully"
End If

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

Is With Class Methods

Class methods can use Is to compare object instances.

is_method.bas
Type Node
    Dim value As Integer
    
    Function IsSame(other As Node Ptr) As Boolean
        Return @This Is other
    End Function
End Type

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

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

Delete n1

The Node class includes a method that uses Is to compare the current instance with another. The @ operator gets the object's address. The method correctly identifies when two references are equal.

Is With Inheritance

The Is keyword works with inherited classes.

is_inheritance.bas
Type Animal
    Dim fname As String
End Type

Type Dog Extends Animal
    Dim breed As String
End Type

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

If a Is 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), Is can compare them. It returns false here because they're separate instances. The keyword works across inheritance hierarchies.

Best Practices

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