简体   繁体   中英

intellisense - autogenerated properties in VB.NET visual Studio 2013

I am using Visual Studio 2013 and was trying to skip implementing getters and setters manually by using just the following code:

Public Class VerifiableText
    Public Property verifier() As IVerifier
    Public Property text() As String

    Function verify() As Boolean
        Return verifier.verify(text)
    End Function
End Class

This seems to work out fine so far, but i was wondering about the following behaviour:

If i initialize my class using

Dim input = New VerifiableText

intellisense does not recognize my properties if i type in "input." and press ctrl+space.

However, if i initialize my variable using

Dim input As VerifiableText
input = New VerifiableText

intellisense is suggesting my properties properly.

This isn't really a problem, but i would like to understand this behaviour.

Thanks!

In this case it probably has more to do with Scope and compiler options than the property style. But it boils down to VS not knowing the actual Type of your object in order to provide the list items in Intellisense.

With Option Infer on, VB will infer the type of local variables. So for:

Sub SomeSub
   Dim v = New VerifiableText

...you havent declared a Type but VB infers it from the assignment. With Option Infer Off, v is Type Object and the VS IDE cannot provide Intellisense help (which may be the case here). Note that for this to compile, Option Strict must also be off or you'll get an error that the Type is not declared.

If this was a module level variable, things are slightly different:

Private v = New VerifiableText

Sub SomeSub...
    ' ...

v is still of Type Object because Option Infer only works on local variables. You will also get an error from Option Strict because the Type is not declared. The solution is include the Type in the declaration:

Private v As New VerifiableText         ' As... is required for Option Strict

Dim v As New VerifiableText             ' As... is optional under Option Infer

In both cases, you have declared a Type for v so now Intellisense should work. You should turn on Option Strict however and the IDE/compiler will help with missed Type declarations (among other things).

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