隐式定义的变量抛出 运行 时间错误,而显式定义则不会
Implicitly defined variable throws run-time error while explicitly defined does not
使用 VB.NET,我正在尝试按照 ReSharper 的指南清理代码库。我目前有以下代码:
'oSearchInput is defined outside this question
Dim oSearchRoutines As New SearchClient
Dim oSearchResults As List(Of SearchResult)
oSearchRoutines = 'WcfCallThatReturnsSearchClient
oSearchResults = oSearchRoutines.getSearchResults(oSearchInput).ToList
现在这完全可以正常工作,但 ReSharper 警告说 As New SearchClient
有 'Value assigned is not used in any execution path'。所以我删除了那部分以获得此代码:
'oSearchInput is defined outside this question
Dim oSearchRoutines
Dim oSearchResults As List(Of SearchResult)
oSearchRoutines = 'WcfCallThatReturnsSearchClient
oSearchResults = oSearchRoutines.getSearchResults(oSearchInput).ToList
如果我的理解正确,一切都应该完全一样。但是,调用 ToList
:
时会抛出错误
Public member 'ToList' on type 'SearchResult()' not found.
我不确定为什么我这里的两个片段之间有任何区别。
因为您在第二个示例中没有指定 SearchClient
类型,所以 oSearchRoutines
将自动成为 Object
.
类型
Object
类型的表达式主要是不允许使用Extension methods, like for example the ToList
-method. For further information see here
以下示例说明了此行为:
Dim x As Object
Dim y As String = "ABC"
x = y
Dim a As List(Of Char) = y.ToList() 'This will work
Dim b As List(Of Char) = x.ToList() 'This will throw a System.MissingMemberException
出现消息分配的值未用于任何执行路径,因为您使用[声明oSearchRoutines
=19=] 在你的第一个例子中。
这是不必要的,因为您在线上为其分配了一个新值...
oSearchRoutines = 'WcfCallThatReturnsSearchClient
...在任何地方使用它之前。
因此您可以不使用关键字 New
来声明它
Dim oSearchRoutines As SearchClient
相关问题:VB.NET: impossible to use Extension method on System.Object instance
使用 VB.NET,我正在尝试按照 ReSharper 的指南清理代码库。我目前有以下代码:
'oSearchInput is defined outside this question
Dim oSearchRoutines As New SearchClient
Dim oSearchResults As List(Of SearchResult)
oSearchRoutines = 'WcfCallThatReturnsSearchClient
oSearchResults = oSearchRoutines.getSearchResults(oSearchInput).ToList
现在这完全可以正常工作,但 ReSharper 警告说 As New SearchClient
有 'Value assigned is not used in any execution path'。所以我删除了那部分以获得此代码:
'oSearchInput is defined outside this question
Dim oSearchRoutines
Dim oSearchResults As List(Of SearchResult)
oSearchRoutines = 'WcfCallThatReturnsSearchClient
oSearchResults = oSearchRoutines.getSearchResults(oSearchInput).ToList
如果我的理解正确,一切都应该完全一样。但是,调用 ToList
:
Public member 'ToList' on type 'SearchResult()' not found.
我不确定为什么我这里的两个片段之间有任何区别。
因为您在第二个示例中没有指定 SearchClient
类型,所以 oSearchRoutines
将自动成为 Object
.
Object
类型的表达式主要是不允许使用Extension methods, like for example the ToList
-method. For further information see here
以下示例说明了此行为:
Dim x As Object
Dim y As String = "ABC"
x = y
Dim a As List(Of Char) = y.ToList() 'This will work
Dim b As List(Of Char) = x.ToList() 'This will throw a System.MissingMemberException
出现消息分配的值未用于任何执行路径,因为您使用[声明oSearchRoutines
=19=] 在你的第一个例子中。
这是不必要的,因为您在线上为其分配了一个新值...
oSearchRoutines = 'WcfCallThatReturnsSearchClient
...在任何地方使用它之前。
因此您可以不使用关键字 New
Dim oSearchRoutines As SearchClient
相关问题:VB.NET: impossible to use Extension method on System.Object instance