McBasic™Get McBasic™ ↗
Build 881
Practical guide

Use control references & object arrays

Choose a typed control reference, find controls deliberately and keep class instances in typed arrays rather than scalar collections.

Declare the capabilities you need#

A visual control type can be a variable, parameter, Function result or array element type. Use Dim field As TextBox and Set field = txtCell(4) to refer to an existing designer-created control. Use Control when you need only the shared surface. Neither declaration creates a visual object, and New TextBox is not supported.

Common Control membersUse
Name, Parent, IndexRead identity and hierarchy. Index is -1 outside a control array.
Enabled, VisibleInteraction and presentation state.
Left, Top, Width, HeightPosition and requested size.
MinWidth, MinHeight, MaxWidth, MaxHeightSize constraints.
ActualWidth, ActualHeightRealized dimensions.
Tag, TabIndex, Font, ForeColor, BackColorShared metadata, keyboard order and appearance.

A TextBox-specific member such as Text needs a TextBox reference. Test TypeOf candidate Is TextBox before narrowing with Set. An incompatible Control-to-TextBox assignment raises error 13; Nothing remains Nothing when assigned to the narrower reference.

Look up a control by an explicit identity#

ExpressionResult
Me.Controls.CountNumber of visual controls on the Form; nonvisual components are not included.
For Each candidate In Me.ControlsVisits controls in layout order, not control-array Index order. Declare candidate As Control.
Me.Controls("txtName")One ordinary named control.
Me.Controls("txtCell(4)") or Me.Controls("txtCell", 4)A particular array member.
Bare array nameError 5; specify a member.
Missing array memberError 340.
Unknown control nameError 730.

The name argument must be a String, not an arbitrary Variant. Read the complete Find cell 4 handler in ControlArrayBoard: it performs lookup, TypeOf and Set without assuming that every Control has Text.

An array of references is not a control array#

Dim fields(1) As TextBox creates two reference slots, initially Nothing. Assign existing controls to them with Set. This does not change their Name, Index, event bindings or parent and cannot add UI at runtime. In contrast, the Designer’s named txtCell array is the group generated from indexed layout members.

Allocate the container, then create each instance#

A typed class array separates its bounds from the objects in its slots. ReDim people(2) allocates three slots under Option Base 0; each remains Nothing until assigned, for example Set people(0) = New Customer. ReDim Preserve keeps existing references while growing the upper bound; newly added slots are Nothing.

Pass a class array as its declared element type and pass the array variable itself, without parentheses at a statement-call site. A Function can return Customer(); assign the returned array without Set, while assigning an individual Customer reference uses Set. For Each needs a Customer loop variable, not Variant. ReDim cannot switch the declared element class.

Follow the complete customer-array tutorial for the class, module, manifest and four executable tests. It covers reference identity, mutation through a parameter, Preserve and error 9 on an unallocated dynamic array.

Keep scalar collections and object ownership distinct#

Collection and Dictionary items remain scalar values. They cannot hold controls or class instances; a Variant is not a workaround. Use typed arrays for these references. Arrays of visual Forms remain unsupported: keep explicit Form variables or separate window data in class instances.

Do not treat a stored reference as proof that a Form is still open. For Each over a dynamic array before ReDim raises error 9. An allocated slot can still be Nothing; test it before calling instance members.

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
  • tests/McBasic.Compiler.Tests/ClassArrayChecks.cs
  • tests/McBasic.Compiler.Tests/ControlTypeChecks.cs
  • tests/McBasic.Compiler.Tests/ControlsCollectionChecks.cs