VB.Net: 初始化变量同时使用like "with"?
VB.Net: Initiating variable and using like "with" at the same time?
我有一个 class(假设它叫做 Person)它有一个叫做 Age 的 属性 和一个叫做 LogAccess 的子项。
我想让我的代码尽可能小,我希望有类似...
Using frm As New Person With {.Age = 30}
.LogAccess()
End Using
或者也许...
With New Person With {.Age = 30}
.LogAccess()
End With
但这不起作用。
我真的需要输入更多代码吗...
Using p As New Person With {.Age = 30}
With p
.LogAccess()
End With
End Using
(使用"With p"因为我在实际项目中需要调用很多方法)
有什么建议吗?
您所追求的只是微优化,这并不是真正必要的,但如果您确实想要编写代码 "small" 只需使用:
Using p As New Person With {.Age = 30}
p.LogAccess()
...
End Using
或者
With New Person With {.Age = 30}
.LogAccess()
.AnotherMethod()
...
.Dispose()
End With
但是您失去了 Using
声明的好处。
我有一个 class(假设它叫做 Person)它有一个叫做 Age 的 属性 和一个叫做 LogAccess 的子项。
我想让我的代码尽可能小,我希望有类似...
Using frm As New Person With {.Age = 30}
.LogAccess()
End Using
或者也许...
With New Person With {.Age = 30}
.LogAccess()
End With
但这不起作用。
我真的需要输入更多代码吗...
Using p As New Person With {.Age = 30}
With p
.LogAccess()
End With
End Using
(使用"With p"因为我在实际项目中需要调用很多方法)
有什么建议吗?
您所追求的只是微优化,这并不是真正必要的,但如果您确实想要编写代码 "small" 只需使用:
Using p As New Person With {.Age = 30}
p.LogAccess()
...
End Using
或者
With New Person With {.Age = 30}
.LogAccess()
.AnotherMethod()
...
.Dispose()
End With
但是您失去了 Using
声明的好处。