Skip to content

Constructors & Destructors

Classes in CrossBasic support:

  • Constructor overloads via Sub Constructor(...).
  • Resource cleanup via built-in Close (aka Destroy) methods.

Constructors

Define one or more Sub Constructor blocks in your class:

Class Pair
  Sub Constructor()                      // default
  End Sub

  Sub Constructor(left As Variant, right As Variant) // overloaded
    Self.LeftValue  = left
    Self.RightValue = right
  End Sub
End Class

When you New Pair(...), the appropriate constructor is selected by matching parameter count and types .

Destructors / Cleanup

  • Built-in classes often provide a Destroy Close() method to free native resources.
  • Call Close() when you’re done with a resource to free its memory immediately. ARC will handle them otherwise:
Dim dt As New DateTime
dt.Initialize()
' … use dt …
dt.Close()
  • User-defined classes can implement a Sub Destructor() if necessary; it will be called automatically when the instance is collected.