【发布时间】:2016-03-16 00:25:32
【问题描述】:
当您从其访问器中访问属性名称时,我正在尝试查找有关 VB 属性行为的文档。我希望在 Get the MyMinions 属性访问的第一行中是递归的(ish),但事实并非如此。 MyMinions 在其访问器中的值始终是 Nothing,为什么它总是什么都没有,并且在任何地方都有记录?
Public Class MyJob
Public Sub New()
MinionCount = 3
End Sub
Public Property MinionCount As Int32
Public Property MyMinions As List(Of Object)
Get
If MinionCount > 0 AndAlso MyMinions Is Nothing Then
_myMinions = New List(Of Object)() 'here would be DAL call
End If
Return _myMinions
End Get
Set(value As List(Of Object))
_myMinions = value
End Set
End Property
Private _myMinions As List(Of Object) = Nothing
End Class
【问题讨论】:
-
IDE 应该警告您
MyMinions在分配值之前已被使用 -
澄清一下,这是我发现的一个错误,我不想让这段代码工作,我想更好地理解行为
-
getter 就像一个函数,在 VB 中你可以将返回值分配给函数名。因此,它就像一个类型化的占位符变量,因此
MyMinions在(准)函数的开头总是什么都不是,直到你给它赋值。 -
就像 plutonix 说的那样。在这种情况下,MyMinions 就像一个隐藏变量。改用“Me.MyMinions”。
-
3. In the case of a Function, an implicit local variable is also initialized called the function return variable whose name is the function’s name, whose type is the return type of the function and whose initial value is the default of its type.VB 语言规范的 10.1.1.3
标签: vb.net properties