在 Visual Studio 2010 和 VB.NET 中声明全局变量 [英] Declare global variables in Visual Studio 2010 and VB.NET

查看:107
本文介绍了在 Visual Studio 2010 和 VB.NET 中声明全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Visual Basic 中声明全局变量?

How do I declare a global variable in Visual Basic?

这些变量需要可以从所有 Visual Basic 表单访问.我知道如何为特定表单声明公共变量,但如何为项目中的所有表单执行此操作?

These variables need to be accessible from all the Visual Basic forms. I know how to declare a public variable for a specific form, but how do I do this for all the forms in my project?

推荐答案

没有办法声明全局变量,因为您可能在 VB.NET 中想象它们.

可以做的(正如其他一些答案所建议的那样)是将您想要视为全局变量的所有内容声明为静态变量,而不是在一个特定类中:

What you can do (as some of the other answers have suggested) is declare everything that you want to treat as a global variable as static variables instead within one particular class:

Public Class GlobalVariables
    Public Shared UserName As String = "Tim Johnson"
    Public Shared UserAge As Integer = 39
End Class

但是,您需要完全限定对这些变量的所有引用,只要您想在代码中使用它们.从这个意义上说,它们不是您在其他语言中可能熟悉的全局变量类型,因为它们仍然与某个特定类相关联.

However, you'll need to fully-qualify all references to those variables anywhere you want to use them in your code. In this sense, they are not the type of global variables with which you may be familiar from other languages, because they are still associated with some particular class.

例如,如果你想在你的表单代码中显示一个带有用户名的消息框,你必须这样做:

For example, if you want to display a message box in your form's code with the user's name, you'll have to do something like this:

Public Class Form1: Inherits Form

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        MessageBox.Show("Hello, " & GlobalVariables.UserName)
    End Sub

End Class

您不能简单地通过在定义变量的类之外键入 UserName 来访问该变量——您还必须指定定义它的类的名称.

You can't simply access the variable by typing UserName outside of the class in which it is defined—you must also specify the name of the class in which it is defined.


如果完全限定变量的做法无论出于何种原因让您感到恐惧或不安,您始终可以在每个代码文件(或即使在项目级别,在项目的属性窗口中).然后,您可以简单地通过名称引用变量.


If the practice of fully-qualifying your variables horrifies or upsets you for whatever reason, you can always import the class that contains your global variable declarations (here, GlobalVariables) at the top of each code file (or even at the project level, in the project's Properties window). Then, you could simply reference the variables by their name.

Imports GlobalVariables

请注意,当您在 Module 中声明全局变量而不是一个.在 VB.NET 中,它提供了用于与以前版本的 VB 向后兼容的模块,Module 只是一个密封的静态类(或者,在 VB.NET 术语中,Shared NotInheritable Class).IDE 允许您从模块调用成员,而无需完全限定或导入对它们的引用.即使您决定走这条路,也值得了解在像 VB.NET 这样的面向对象语言的幕后发生的事情.我认为,作为一名程序员,重要的是要了解正在发生的事情以及您的工具究竟为您做什么,即使您决定使用它们.就其价值而言,我不建议将此作为最佳实践",因为我觉得它倾向于晦涩难懂和干净的面向对象的代码/设计.如果您的代码如上所示编写,则 C# 程序员更有可能理解您的代码,而不是将其塞入模块并让编译器处理所有内容.

Note that this is exactly the same thing that the compiler is doing for you behind-the-scenes when you declare your global variables in a Module, rather than a Class. In VB.NET, which offers modules for backward-compatibility purposes with previous versions of VB, a Module is simply a sealed static class (or, in VB.NET terms, Shared NotInheritable Class). The IDE allows you to call members from modules without fully-qualifying or importing a reference to them. Even if you decide to go this route, it's worth understanding what is happening behind the scenes in an object-oriented language like VB.NET. I think that as a programmer, it's important to understand what's going on and what exactly your tools are doing for you, even if you decide to use them. And for what it's worth, I do not recommend this as a "best practice" because I feel that it tends towards obscurity and clean object-oriented code/design. It's much more likely that a C# programmer will understand your code if it's written as shown above than if you cram it into a module and let the compiler handle everything.


请注意,至少像其他答案所提到的那样,VB.NET 是一种完全面向对象的语言.这意味着,除其他外,一切 都是一个对象.甚至全局"变量也必须在类的实例中定义,因为它们也是对象.任何时候您觉得需要在面向对象的语言中使用全局变量,这都表明您需要重新考虑您的设计.如果您只是转向面向对象编程,那么在进一步深入编写代码之前停下来学习一些基本模式是非常值得的.


Note that like at least one other answer has alluded to, VB.NET is a fully object-oriented language. That means, among other things, that everything is an object. Even "global" variables have to be defined within an instance of a class because they are objects as well. Any time you feel the need to use global variables in an object-oriented language, that a sign you need to rethink your design. If you're just making the switch to object-oriented programming, it's more than worth your while to stop and learn some of the basic patterns before entrenching yourself any further into writing code.

这篇关于在 Visual Studio 2010 和 VB.NET 中声明全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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