McBasic™Get McBasic™ ↗
Build 881
Hands-on tutorial · Language

Use With without losing object identity

Reduce repeated qualifiers while understanding the captured receiver.

2 Form / control designs2 complete BASIC filesGuided sample project

With captures its receiver when the block begins. Reassigning a variable inside a nested block does not retarget the already-captured receiver. The sample deliberately creates distinct Forms to make that rule observable; read the identity comparison before simplifying this code.

Step 1Prepare a safe practice project#

Extract the archive into a new writable folder, separate from your own application. Open WithBlocks.mcproj in McBasic™. Keep every declared source, layout, resource and migration file together.

To reconstruct it yourself, create a DesktopApp project and add the files declared below. In the complete project, follow these steps as a guided rebuild: replace each named source file once and Check after all its procedures are present.

Exact project configuration
WithBlocks.mcproj
{
  "schemaVersion": 1,
  "projectId": "099e2901-076d-45d1-9a8b-960147278d86",
  "name": "WithBlocks",
  "kind": "DesktopApp",
  "language": {
    "dialect": "McBasic1",
    "requireExplicit": true,
    "compare": "Binary",
    "defaultArrayBase": 0
  },
  "application": {
    "bundleIdentifier": "com.mcbasic.samples.withblocks",
    "version": "0.1.0",
    "buildNumber": "111",
    "startupFormId": "form-main",
    "shutdownMode": "OnStartupFormClosed"
  },
  "forms": [
    {
      "id": "form-main",
      "typeName": "frmMain",
      "layout": "Forms/frmMain.form.json",
      "code": "Forms/frmMain.bas"
    },
    {
      "id": "form-details",
      "typeName": "frmDetails",
      "layout": "Forms/frmDetails.form.json",
      "code": "Forms/frmDetails.bas"
    }
  ],
  "modules": [],
  "classes": [],
  "tests": [],
  "references": [],
  "resources": [],
  "targets": [
    "osx-arm64"
  ],
  "capabilities": []
}

Step 2Build the Form and set its properties#

frmMain

Select the Form first and set its properties. Add containers before their children. These are initial Designer values, not statements to paste into a procedure.

Form propertyValue
Caption"With Blocks - McBasic"
Width520
Height280
MinWidth500
MinHeight210
TypeNameParent
TextBoxtxtNameForm
CommandButtoncmdGreetForm
LabellblGreetingForm
CommandButtoncmdDetailsForm
txtName · exact Inspector values
PropertyValue
Left24
Top32
Width310
Height36
Text""
TabIndex0
Anchor edgesLeft, Top
cmdGreet · exact Inspector values
PropertyValue
Left354
Top32
Width130
Height36
Caption"Greet"
TabIndex1
Anchor edgesLeft, Top
lblGreeting · exact Inspector values
PropertyValue
Left24
Top98
Width460
Height50
Caption"Enter your name and click Greet."
Anchor edgesLeft, Top
cmdDetails · exact Inspector values
PropertyValue
Left24
Top166
Width180
Height36
Caption"Compare references"
TabIndex2
Anchor edgesLeft, Top

frmDetails

Select the Form first and set its properties. Add containers before their children. These are initial Designer values, not statements to paste into a procedure.

Form propertyValue
Caption"With reference details"
Width420
Height220
TypeNameParent
LabellblNameForm
CommandButtoncmdCloseForm
lblName · exact Inspector values
PropertyValue
Left24
Top32
Width370
Height44
Caption"Details"
Anchor edgesLeft, Top
cmdClose · exact Inspector values
PropertyValue
Left24
Top100
Width160
Height36
Caption"Close details"
TabIndex0
Anchor edgesLeft, Top

Step 3Connect the events explicitly#

frmMain

ObjectEventProcedureParameters
cmdGreetClickcmdGreet_ClickClick()
cmdDetailsClickcmdDetails_ClickClick()

frmDetails

ObjectEventProcedureParameters
cmdCloseClickcmdClose_ClickClick()
FormShownForm_Shown()

Step 4Enter the complete behavior#

Open the named source file. Enter declarations first, then every procedure. Expand a procedure for its full body, or copy the complete file. Helpers are required: do not stop after entering the first Click handler.

Forms/frmMain.bas

Enter these declarations at file scope, before the procedures.

Forms/frmMain.bas · declarations
Option Explicit
Private Sub cmdGreet_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdGreet_Click
Private Sub cmdGreet_Click()
    With Me
        With .txtName
            .Text = Trim(.Text)
        End With
        If Len(txtName.Text) = 0 Then
            .lblGreeting.Caption = "Enter a name."
            Exit Sub
        End If
        With .lblGreeting
            .Caption = "Hello, " & txtName.Text & "!"
        End With
    End With
End Sub
Private Sub cmdDetails_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdDetails_Click
Private Sub cmdDetails_Click()
    Dim original As frmDetails, current As frmDetails
    Set original = New frmDetails
    Set current = original
    With current
        .DisplayName = "first"
        With (current)
            Set current = New frmDetails
            .DisplayName = "captured"
        End With
        .DisplayName = txtName.Text
        .Show
    End With
    With lblGreeting
        .Caption = "Captured=" & original.DisplayName & "; distinct=" & Not (original Is current)
    End With
    Unload original
End Sub
Copy the complete Forms/frmMain.bas file

Replace the file once; do not append duplicate procedures.

Forms/frmMain.bas
Option Explicit

Private Sub cmdGreet_Click()
    With Me
        With .txtName
            .Text = Trim(.Text)
        End With
        If Len(txtName.Text) = 0 Then
            .lblGreeting.Caption = "Enter a name."
            Exit Sub
        End If
        With .lblGreeting
            .Caption = "Hello, " & txtName.Text & "!"
        End With
    End With
End Sub

Private Sub cmdDetails_Click()
    Dim original As frmDetails, current As frmDetails
    Set original = New frmDetails
    Set current = original
    With current
        .DisplayName = "first"
        With (current)
            Set current = New frmDetails
            .DisplayName = "captured"
        End With
        .DisplayName = txtName.Text
        .Show
    End With
    With lblGreeting
        .Caption = "Captured=" & original.DisplayName & "; distinct=" & Not (original Is current)
    End With
    Unload original
End Sub

Forms/frmDetails.bas

Enter these declarations at file scope, before the procedures.

Forms/frmDetails.bas · declarations
Option Explicit

Public DisplayName As String
Private Sub Form_Shown()

Initialize state before enabling dependent actions. Keep the shared declarations and startup dependencies with this procedure.

Forms/frmDetails.bas · Form_Shown
Private Sub Form_Shown()
    With Me
        With .lblName
            .Caption = "Details for " & DisplayName
        End With
    End With
End Sub
Private Sub cmdClose_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmDetails.bas · cmdClose_Click
Private Sub cmdClose_Click()
    With Me
        .Unload
    End With
End Sub
Copy the complete Forms/frmDetails.bas file

Replace the file once; do not append duplicate procedures.

Forms/frmDetails.bas
Option Explicit

Public DisplayName As String

Private Sub Form_Shown()
    With Me
        With .lblName
            .Caption = "Details for " & DisplayName
        End With
    End With
End Sub

Private Sub cmdClose_Click()
    With Me
        .Unload
    End With
End Sub

Step 5Keep resources and storage reproducible#

No external resource or migration files are declared. Any inline migration, seed data or local JSON is present in the code above. Runtime application data and private preferences are not included in the archive.

Step 6Check, run and test the result#

Save all files and run Check. Resolve diagnostics before Run. Developer CLI users can work from the extracted folder:

Developer CLI
mcbasic check WithBlocks.mcproj
mcbasic run WithBlocks.mcproj

Acceptance checklist

  • Greet with padded input and verify trimming.
  • Open details and inspect the captured/distinct status.
  • Trace which instance receives each DisplayName assignment.

Step 7Make one change and repeat the tests#

Rewrite the greeting without With and confirm the same output for empty and nonempty input.

Save a working Git checkpoint first. Change one behavior, Check again and repeat the acceptance checklist. Compare with the downloaded solution if a regression appears.

Source baseline 20f4bfed6b · Build manifest