语句块允许隐式访问用户定义的类型变量中的字段
语法
With user_defined_var
statements
End With
说明
With...End With块允许在引用其字段时省略用户定义的
Type变量的名称。然后可以在它们之前只有一个周期(
.)访问这些字段,例如。如果
Type包含称为“
element ”的字段元素,则可以在
With块中访问“
.element ”。
它可以作为一种速记来保存打字,并避免杂乱的来源。
With也可以与取消引用的指针一起使用,如第二个例子所示。
With块可能嵌套。在这种情况下,只有最内层
With块是活动的,任何外层块都被忽略,直到内层被重新封闭为止。参见第三个例子来说明这一点。
在内部,在
With块的开始处引用变量,然后用于计算块内的任何元素访问。请注意,这意味着
Goto不应该用于跳转到
With块,否则引用将不会被设置,并且尝试访问它的结果将是未定义的。
注意使用块内使用的成员程序:
要访问Type之外定义的重复符号,请使用“..SomeSymbol”。
例子
Type rect_type
x As Single
y As Single
End Type
Dim the_rectangle As rect_type
Dim As Integer temp, t
With the_rectangle
temp = .x
.x = 234 * t + 48 + .y
.y = 321 * t + 2
End With
Type rect_type
x As Single
y As Single
End Type
Dim the_rectangle As rect_type Ptr
the_rectangle = CAllocate( 5 * Len( rect_type ) )
Dim As Integer loopvar, temp, t
For loopvar = 0 To 4
With the_rectangle[loopvar]
temp = .x
.x = 234 * t + 48 + .y
.y = 321 * t + 2
End With
Next
Type rect_type
x As Single
y As Single
End Type
Dim As rect_type rect1, rect2
''嵌套与块
With rect1
.x = 1
.y = 2
With rect2
.x = 3
.y = 4
End With
End With
Print rect1.x, rect1.y ''1, 2
Print rect2.x, rect2.y ''3, 4
方言差异
与QB差别
参考