Skip to content
User-Defined Types
step 1/5

Reading — step 1 of 5

Learn

~1 min readSubs, Functions, and Types

BASIC's records (called UDTs — User-Defined Types):

TYPE Person
    name AS STRING * 50      ' fixed-length string
    age AS INTEGER
END TYPE

DIM ada AS Person
ada.name = "Ada"
ada.age = 36
PRINT ada.name; ada.age

STRING * N is a fixed-length string. Variable-length strings work too with just AS STRING.

Constructors — none built in; use a helper:

Function NewPerson(n AS STRING, a AS INTEGER) AS Person
    DIM p AS Person
    p.name = n
    p.age = a
    Return p
End Function

DIM ada AS Person = NewPerson("Ada", 36)

Arrays of records:

DIM people(1 TO 10) AS Person
people(1).name = "Ada"
people(1).age = 36

Member functions (FreeBASIC has a bit of OOP):

TYPE Counter
    count AS INTEGER
    DECLARE Sub Increment()
END TYPE

Sub Counter.Increment()
    this.count += 1     ' 'this' is the implicit instance
End Sub

DIM c AS Counter
c.count = 0
c.Increment()
c.Increment()
PRINT c.count   ' 2

FreeBASIC supports full classes with inheritance, but this is enough for most tasks.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…