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.
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.
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.
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.
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.
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.
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.
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
- Object Comparison: Always use
Iswhen comparing object references. - Null Checks: Use
Is Nothingfor null reference checks. - Value Types: Don't use
Iswith non-object types. - Readability: Prefer
Isover pointer equality for clarity. - Memory Management: Combine with proper object cleanup.
This tutorial covered the FreeBasic Is keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.