简体   繁体   English

intellisense-VB.NET visual Studio 2013中的自动生成的属性

[英]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: 我正在使用Visual Studio 2013,并尝试通过仅使用以下代码来跳过手动实现getter和setter的方法:

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." 如果我输入“ input”,则intellisense无法识别我的属性。 and press ctrl+space. 然后按Ctrl +空格键。

However, if i initialize my variable using 但是,如果我使用

Dim input As VerifiableText
input = New VerifiableText

intellisense is suggesting my properties properly. intellisense正在正确建议我的财产。

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. 在这种情况下,它可能与Scope和编译器选项有更多关系,而不仅仅是属性样式。 But it boils down to VS not knowing the actual Type of your object in order to provide the list items in Intellisense. 但是归结为VS不知道对象的实际类型,以便在Intellisense中提供列表项。

With Option Infer on, VB will infer the type of local variables. 启用Option Infer后,VB将推断局部变量的类型。 So for: 因此对于:

Sub SomeSub
   Dim v = New VerifiableText

...you havent declared a Type but VB infers it from the assignment. ...您尚未声明类型,但是VB从分配中推断出它。 With Option Infer Off, v is Type Object and the VS IDE cannot provide Intellisense help (which may be the case here). 如果禁用Option Infer ,则v是类型Object ,并且VS IDE无法提供Intellisense帮助(此处可能就是这种情况)。 Note that for this to compile, Option Strict must also be off or you'll get an error that the Type is not declared. 请注意,要对此进行编译,还必须关闭Option Strict ,否则会得到未声明Type的错误。

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. v仍为Type Object因为Option Infer仅适用于局部变量。 You will also get an error from Option Strict because the Type is not declared. 您还会从Option Strict得到一个错误,因为未声明Type。 The solution is include the Type in the declaration: 解决方案是在声明中包括Type:

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. 在这两种情况下,您都已为v声明了Type,因此现在Intellisense应该可以使用了。 You should turn on Option Strict however and the IDE/compiler will help with missed Type declarations (among other things). 但是,您应该打开Option Strict ,IDE /编译器将帮助您解决丢失的类型声明(以及其他问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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