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.
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 FunctionChoose the representation before the caption#
| Data | Start with | Reason |
|---|---|---|
| An on/off decision | Boolean | The value should not depend on the wording of a label. |
| User text, a path or identifier text | String | Preserves text until you intentionally parse it. |
| A count or integer result | Integer, Long or LongLong | Choose the range the API promises; ExecuteAsync returns LongLong. |
| A measurement | Single or Double | Represents fractional values. |
| Decimal or monetary arithmetic | Decimal or Currency | Use the type required by the calculation and API. |
| A date/time | Date | Format it only when displaying it. |
| A nullable or externally typed value | Variant | Inspect Null before conversion. |
| A database or Form | Its named object type | Create 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#
Public Function TimerSeconds() As Double
TimerSeconds = CDbl(Timer())
End FunctionTimer 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.
20f4bfed6b · Build manifestSources & maintenance
Original web explanation, reviewed against these repository sources. API tables and example files follow the implementation.
docs/manual/21-language-reference.mddocs/manual/11-language-toolkit.mdsite/examples/LanguageWorkbench/Main.bassrc/McBasic.Compiler/Binder.cssrc/McBasic.Runtime/BasicRuntime.cssite/examples/LanguageWorkbench/LanguageTests.bas