Use With without losing object identity
Reduce repeated qualifiers while understanding the captured receiver.
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
{
"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 property | Value |
|---|---|
Caption | "With Blocks - McBasic" |
Width | 520 |
Height | 280 |
MinWidth | 500 |
MinHeight | 210 |
| Type | Name | Parent |
|---|---|---|
| TextBox | txtName | Form |
| CommandButton | cmdGreet | Form |
| Label | lblGreeting | Form |
| CommandButton | cmdDetails | Form |
txtName · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 32 |
Width | 310 |
Height | 36 |
Text | "" |
TabIndex | 0 |
| Anchor edges | Left, Top |
cmdGreet · exact Inspector values
| Property | Value |
|---|---|
Left | 354 |
Top | 32 |
Width | 130 |
Height | 36 |
Caption | "Greet" |
TabIndex | 1 |
| Anchor edges | Left, Top |
lblGreeting · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 98 |
Width | 460 |
Height | 50 |
Caption | "Enter your name and click Greet." |
| Anchor edges | Left, Top |
cmdDetails · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 166 |
Width | 180 |
Height | 36 |
Caption | "Compare references" |
TabIndex | 2 |
| Anchor edges | Left, 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 property | Value |
|---|---|
Caption | "With reference details" |
Width | 420 |
Height | 220 |
| Type | Name | Parent |
|---|---|---|
| Label | lblName | Form |
| CommandButton | cmdClose | Form |
lblName · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 32 |
Width | 370 |
Height | 44 |
Caption | "Details" |
| Anchor edges | Left, Top |
cmdClose · exact Inspector values
| Property | Value |
|---|---|
Left | 24 |
Top | 100 |
Width | 160 |
Height | 36 |
Caption | "Close details" |
TabIndex | 0 |
| Anchor edges | Left, Top |
Step 3Connect the events explicitly#
frmMain
| Object | Event | Procedure | Parameters |
|---|---|---|---|
cmdGreet | Click | cmdGreet_Click | Click() |
cmdDetails | Click | cmdDetails_Click | Click() |
frmDetails
| Object | Event | Procedure | Parameters |
|---|---|---|---|
cmdClose | Click | cmdClose_Click | Click() |
| Form | Shown | Form_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.
Option ExplicitPrivate Sub cmdGreet_Click()
Enter the complete action, including its input checks and calls to helper procedures.
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 SubPrivate Sub cmdDetails_Click()
Enter the complete action, including its input checks and calls to helper procedures.
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 SubCopy the complete Forms/frmMain.bas file
Replace the file once; do not append duplicate procedures.
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 SubForms/frmDetails.bas
Enter these declarations at file scope, before the procedures.
Option Explicit
Public DisplayName As StringPrivate Sub Form_Shown()
Initialize state before enabling dependent actions. Keep the shared declarations and startup dependencies with this procedure.
Private Sub Form_Shown()
With Me
With .lblName
.Caption = "Details for " & DisplayName
End With
End With
End SubPrivate Sub cmdClose_Click()
Enter the complete action, including its input checks and calls to helper procedures.
Private Sub cmdClose_Click()
With Me
.Unload
End With
End SubCopy the complete Forms/frmDetails.bas file
Replace the file once; do not append duplicate procedures.
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 SubStep 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:
mcbasic check WithBlocks.mcproj
mcbasic run WithBlocks.mcprojAcceptance 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.
20f4bfed6b · Build manifest