gpt4 book ai didi

vb.net - 在VB.NET错误BC42025中,短语 "qualifying expression will not be evaluated"是什么意思?

转载 作者:行者123 更新时间:2023-12-04 20:46:07 25 4
gpt4 key购买 nike

不重复 "Access of shared member, constant member, enum member or nested type through an instance"

VB.NET 有警告消息

(BC42025): "Access of shared member through an instance; qualifying expression will not be evaluated" as documented

here .

我将“通过实例访问共享成员”这句话的含义理解为:你的代码声明了一个 Dog 对象名称 Fido,然后调用 Fido.Bark(),但在 Dog 类中,Bark 函数是使用 Shared 关键字声明的。

但我不知道该消息的第二部分是什么意思。我一直认为这听起来像是说 VB 会跳过那行代码,但事实并非如此。

文档对该短语的解释为:

Furthermore, if such access is part of an expression that performs other actions, such as a Function procedure that returns an instance of the shared member, Visual Basic bypasses the expression and any other actions it would otherwise perform.

但是我不知道这意味着什么,Visual Basic 正在绕过什么?能用代码解释一下吗?

上下文代码

Sub Main
Dim dog1 As Dog = New Dog()
dog1.Bark() ' this line has the warning
End Sub

Public Class Dog
Public Shared Sub Bark()
End Sub
End Class

编辑

我知道编译器希望我通过类名访问静态方法,我想了解的是“Visual Basic 绕过表达式和它本来会执行的任何其他操作”是什么意思。有哪些可能被绕过的“其他操作”?

最佳答案

想象一下你有这样的东西:

Function GetADog() As Dog
MessageBox.Show("Getting a dog!")
Return New Dog()
End Function

现在,在您的主函数中,您不再使用 dog1.Bark(),而是使用 GetADog().Bark()。该警告告诉您 GetADog() 不会在 Dog.Bark() 之前调用,因此您不会看到消息框。

在文档的上下文中,GetADog() 是被绕过的表达式,消息框是“否则它将执行的其他操作”之一。

关于vb.net - 在VB.NET错误BC42025中,短语 "qualifying expression will not be evaluated"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48308704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com