VB.Net-模块中的变量 [英] VB.Net - Variables in Modules

查看:88
本文介绍了VB.Net-模块中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现一个事实,即模块中的Public变量可以由我的主窗体访问(如果不能由其他类/模块访问).

I just stumbled upon the fact the Public variables in a Module can be accessed by my main Form (If not by other classes/Modules??).

我只是创建一个模块来存储我的所有全局变量以供组织使用,并计划完全限定变量名称出现在其他任何地方的位置.

I was just creating a Module to store all of my Global Variables for organization, and planned to fully qualify the variable names where ever they appeared elsewhere.

然后,当我从主窗体复制变量,注释掉它们并将它们粘贴到模块中以获取变量时,没有发生错误.我为哈哈跑了我的程序,它运行良好.我不必使用[ModuleName].[VariableName].

Then no errors occurred as I copied the variables from my main form, commented them out, and pasted them in my Module for variables. I ran my program for ha-ha's, and it worked perfectly. I did not have to use [ModuleName].[VariableName].

为什么这项工作会从我的表单转到模块,但是如果我试图从某个模块访问表单中的变量,为什么不行呢?

Why does this work going FROM my Form to a Module, yet not if I was trying to access a variable in my Form FROM a Module?

即使它确实有效,我是否仍应使用全名来访问变量(最佳实践)?

Even though it does work, should I still use the full name to access the variables (best practice)??

推荐答案

为什么这项工作会从我的表格转到模块,而如果我是 试图从模块访问表单中的变量?

Why does this work going FROM my Form to a Module, yet not if I was trying to access a variable in my Form FROM a Module?

除了上面的评论...

In addition to the comments above...

VB.Net在幕后为您为您完全修饰了那些模块变量名称.但这仅在模块中的变量名称为唯一时有效.考虑以下情况:

VB.Net is magically fully qualifying those Module Variables Names for you behind the scenes. But this will only work if the variable names in your Modules are unique. Consider the case below:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show(User) ' ?...which one!?
    End Sub

End Class

Public Module Module1
    Public User As String = "Bob"
End Module

Public Module Module2
    Public User As String = "Joe"
End Module

它不知道您真正想要的是哪个User,因此,除非您完全限定了它,否则将导致编译时错误,例如Module1.User.

It can't know which User you actually wanted, so this will result in a compile time error unless you fully qualified it, such as Module1.User.

因此,是的,使用变量名访问它们没有任何问题,因为它们在所有模块中都是唯一命名的(否则就行不通了).这样做的一个缺点是,您无法立即知道正在访问的值是子/函数的局部变量,还是来自模块.实际上,如果您声明的本地变量与模块中的名称相同,则该本地变量将隐藏模块一,而您只会获得该本地变量.

So, yes, there is nothing wrong with using just the variable names to access them, given that they are uniquely named across all modules (otherwise it just won't work). One drawback to this is that you can't immediately tell if the value being accessed is a local variable to the sub/function, or if it comes from a Module. In fact, if you declare a local variable with the same name as that in the Module, the local variable will hide the Module one and you will only get back the local variable.

变量在内部转换为ClassPublic Shared成员,这意味着它们存在而无需创建Module的实例.这就是为什么您可以简单地访问它们而无需使用New关键字来创建Module的实例的原因.这也是为什么您不能相反,而是仅通过其名称访问Forms中的变量的原因……因为您需要Form的一个实例,并且它不知道您想要的是哪个实例(请考虑是否要打开了表单的多个实例).

As Jens pointed out, the variables are internally converted to Public Shared members of a Class, which means they exist without an instance of the Module needing to be created; which is why you can simply access them without using the New keyword to create an instance of the Module. This is also why you can't do the reverse, however, and access the variables in your Forms by just their names...because you need an instance of your Form and it can't know which one you want (consider if you had multiple instances of the Form open).

所以仅仅因为您可以,并不意味着您应该这样做.更好的做法是创建一个恰当命名的Class并将Public Shared成员放入其中.这将迫使您完全有资格让这些成员访问它们,并完全清楚地表明这些值的来源:

So just because you can, doesn't mean you should. It's a better practice to create an aptly named Class and put Public Shared members in it. This will force you to fully qualify those members to access them and make it perfectly clear where those values are coming from:

Public Class AppData

    Public Shared UserName As String

End Class

您可以通过AppData.UserName进行访问.

这篇关于VB.Net-模块中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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