简体   繁体   中英

With statement not working

Why doesn't this work?

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

MSDN for With says:

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.

This means that With is used for instance methods (and properties) however WriteLine and ReadKey are static (or Shared ) methods and System.Console is a type, not an instance of an object. This is why you can't use With in this case.

WriteLine and ReadKey are Shared methods, you do not have an instance to use with the With

You need to instantiate a variable in order to use it with With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM