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

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

问题描述

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



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

解决方案

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



do(正如其他一些答案所建议的那样)将所有您希望作为全局变量处理的变量声明为一个特定类中的静态变量:

<$公共类GlobalVariables
Public Shared UserName As String =Tim Johnson
Public Shared UserAge As Integer = 39
End Class
code>

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



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

  Public Class Form1:Inherits Form 

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

End Class

您无法只需在定义它的类的外部键入 UserName 就可以访问该变量,您还必须指定定义类的名称。





如果您无论出于何种原因对变量进行完全限定的操作会让您感到恐慌或不安,您可以随时导入包含全局v在每个代码文件的顶部(或者甚至在项目级别,在项目的属性窗口中)的可变声明(这里, GlobalVariables )。然后,您可以简单地通过名称引用变量。

 导入GlobalVariables 

请注意,这正是 与编译器在声明全局变量时幕后为您做的事情完全相同的情况在 Module 中,而不是 Class 。在VB.NET中,一个 Module 是一个简单的密封静态类(或者在VB.NET中, code> Shared NotInheritable Class )。 IDE允许您从模块中调用成员,而无需完全限定或导入对其的引用。即使你决定走这条路,也应该理解VB.NET等面向对象语言背后发生的事情。我认为,作为一名程序员,即使您决定使用它们,了解正在发生的事情以及您的工具正在为您做些什么也很重要。对于它的价值,我不建议将其作为最佳实践,因为我认为它倾向于模糊和清晰的面向对象的代码/设计。如果C#程序员如上所述编写代码,那么它将更有可能理解您的代码,而不是将它塞进模块并让编译器处理所有事情。



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


How do I declare a global variable in Visual Basic?

These variable 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?

解决方案

There is no way to declare global variables as you're probably imagining them in 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

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

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 backwards-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.


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天全站免登陆