声明不起作用

With statement not working

为什么这不起作用?

Module Module1

Sub Main()
    With System.Console 'error BC30691: 'Console' is a type in 'System' and cannot be used as an expression.
        .WriteLine("here a text!")
        .ReadKey(True)
    End With
End Sub

End Module

WriteLine 和 ReadKey 是 Shared 方法,您没有可用于 With

的实例

您需要实例化变量才能将其用于 With

With 的 MSDN 说:

objectExpression Required. An expression that evaluates to an object. The expression may be arbitrarily complex and is evaluated only once. The expression can evaluate to any data type, including elementary types.

这意味着 With 用于实例方法(和属性),但是 WriteLineReadKey 是静态(或 Shared)方法并且 System.Console 是一种类型,而不是对象的实例。这就是在这种情况下不能使用 With 的原因。