user2075764
user2075764

Reputation: 51

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

Upvotes: 1

Views: 97

Answers (2)

Motti
Motti

Reputation: 114695

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.

Upvotes: 4

fdomn-m
fdomn-m

Reputation: 28611

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

Upvotes: 2

Related Questions