McBasic™Get McBasic™ ↗
Build 881
Language reference

Values, types & conversion

Keep data in its useful type, and convert explicitly at the boundary between a user interface and your logic.

Need a structured record?#

Type records support value-copy assignment, nested fields and typed arrays. Read records versus class identity and run the Modern Workbench with its copy and class-initialization tests.

Declare first, assign second#

Put Option Explicit at the top of each source file. Declare each variable with its own type, then assign it on a separate line. In Dim left, right As Long, only right is a Long: left is a Variant. One declaration per line makes the intent unambiguous.

A complete standard-module function
Option Explicit

Public Function LineTotal(ByVal quantity As Long, ByVal unitPrice As Currency) As Currency
    Dim total As Currency
    total = quantity * unitPrice
    LineTotal = total
End Function

Choose the representation before the caption#

DataStart withReason
An on/off decisionBooleanThe value should not depend on the wording of a label.
User text, a path or identifier textStringPreserves text until you intentionally parse it.
A count or integer resultInteger, Long or LongLongChoose the range the API promises; ExecuteAsync returns LongLong.
A measurementSingle or DoubleRepresents fractional values.
Decimal or monetary arithmeticDecimal or CurrencyUse the type required by the calculation and API.
A date/timeDateFormat it only when displaying it.
A nullable or externally typed valueVariantInspect Null before conversion.
A database or FormIts named object typeCreate and assign references with Set.

Treat input conversion as a fallible operation#

A TextBox contains a String even if it displays digits. McBasic™ does not silently turn text into a number. Use a conversion such as CLng and handle invalid text or overflow. Error 13 identifies an incompatible value; error 6 identifies a value outside the target range. A successful conversion is not business validation: a negative quantity may parse correctly but still be unacceptable.

A database Null is not an empty string or zero. Test IsNull(value) before CStr or a numeric conversion; converting Null raises error 94. Preserve that distinction when “not supplied” has meaning.

Format at the display boundary#

Use CStr for a simple display conversion. For a stable date layout, use a supported FormatDate mask such as yyyy-MM-dd or yyyy-MM-dd HH:mm:ss. Date parsing with CDate accepts the documented ISO date/date-time forms; do not rely on an ambiguous regional date string.

The language does not promise every VBA formatting function. Look up the exact available overload in built-in functions. Named formats, arbitrary date masks and guessed aliases are not substitutes for a supported contract.

Read Timer as seconds, not a Date#

LanguageWorkbench / Main.bas · a complete function
Public Function TimerSeconds() As Double
    TimerSeconds = CDbl(Timer())
End Function

Timer and Timer() both return a Single containing seconds since local midnight. Build 831 corrects the compiler’s former Date classification of Timer(), so CDbl(Timer()) works as shown. The conversion widens the value; it does not make the clock more precise. The lesson checks that the result is at least zero and less than 86,400.

This is a time-of-day reading, not a monotonic stopwatch: it wraps at midnight and can change with the system clock. It is also separate from the Timer component, which raises events at an interval. Do not use repeated reads in a busy loop to wait.

Practice and verify#

Run LanguageWorkbench, then try valid text, blank text, an out-of-range integer and Null in a disposable exercise. For UI validation with complete handlers, use InputWorkbench. Keep conversion and domain validation as separate checks.

Source baseline 20f4bfed6b · Build manifest
Sources & maintenance

Original web explanation, reviewed against these repository sources. API tables and example files follow the implementation.

  • docs/manual/21-language-reference.md
  • docs/manual/11-language-toolkit.md
  • site/examples/LanguageWorkbench/Main.bas
  • src/McBasic.Compiler/Binder.cs
  • src/McBasic.Runtime/BasicRuntime.cs
  • site/examples/LanguageWorkbench/LanguageTests.bas