VB.Net 形式的非共享方法被引用为好像它们被共享一样? [英] VB.Net form non-shared methods referenced as if they were shared?

查看:18
本文介绍了VB.Net 形式的非共享方法被引用为好像它们被共享一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我主要是 C# 开发人员,但我正在为 VB.Net 应用程序提供支持.我遇到过我认为是奇怪"的代码,因为我的 C# 心态告诉我代码是错误的,不应该编译,但它确实如此.我在特定表单上执行了 Find all references 以查看它被使用的位置,并找到了对表单的引用,其中直接引用了类的非碎片方法,而没有实例化类.换句话说,我看到这样的代码:

I am doing support on a VB.Net application although I am primarily a C# developer. I have encountered what I consider to be "strange" code, in that my C# mindset tells me that the code is wrong and shouldn't compile, yet it does. I did a Find all references on a particular form to see where it was being used and found references to the form where non-shred methods of the class were being directly referenced without the class being instantiated. In other words, I see code like this:

FrmCareer.ShowDialog(careerId)
FrmCareer = Nothing

现在ShowDialog不是共享成员,它引用实例成员变量,所以我不希望intellisense列出ShowDialog方法根本.但它确实可以编译.谁能解释这里发生了什么?

Now ShowDialog is not a shared member, and it references instance member variables, so I would not expect intellisense to list the ShowDialog method at all. But it does and it compiles. Can anyone explain what is going on here?

推荐答案

VB.NET 具有(非常好)的特性,即在 My.Forms 对象.那里的对象在您第一次访问它们时被实例化.通过这种方式,您可以获得每个表单的默认实例,供您参考.

VB.NET has the (very nice) feature of keeping a collection of all forms as properties under the My.Forms object. The objects there are instantiated when you first access them. In this way, you get a default instance of each form that you can refer to.

在不太好的一面,VB 坚持导入 My.Forms 对象的属性全局,你无法改变它.

On the not so nice side, VB insists on importing the My.Forms object’s properties globally, and there’s nothing you can do to change that.

换句话说,您的应用程序中任何表单的类名都兼作该表单的全局实例.

In other words, the class name of any form in your application doubles as a global instance of that form.

这篇关于VB.Net 形式的非共享方法被引用为好像它们被共享一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆