Bill
Bill

Reputation: 123

VB.Net - Variables in Modules

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.

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)??

Upvotes: 0

Views: 6602

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39122

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

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.

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

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

You'd access that with AppData.UserName.

Upvotes: 3

Related Questions