How would I declare a global variable in Visual Basic?

前端 未结 3 1992
逝去的感伤
逝去的感伤 2021-01-04 23:08

I want to create a variable that can be used across multiple forms.

It\'s going to be a temporary storage place for integers.

3条回答
  •  独厮守ぢ
    2021-01-04 23:25

    You can just add it as PUBLIC to ANY Module

    Example:

    Module Module1 'Global variables Public glbtxtTemplateName As String 'GLOBAL VARIABLE FOR TEMPLATE

    VB loads the Modals first as a class and all PUBLIC items therein are shared directly. Think about it this way.

    Lets say we have a MODULE called "MY_PROCESSES"

    When you declare a SUB or a FUNCTION in "MY_PROCESSES" if you want it to be used OUTSIDE of "MY_PROCESSES" you declare as PUBLIC like this

    PUBLIC SUB LOAD_TEMPLATE() ....

    To get to LOAD_TEMPLATE you just call it in your code from anywhere:

    LOAD_TEMPLATE

    So if I need to set or use the global variable that I made public in my module I just refer to it by name:

    glbtxtTemplateName="TEMPLATE_NAME"

    IF glbtxtTemplateName="" then LoadTemplate

    I do like building the class as above because you can reference it faster without remembering the variable but if you only need 1 or 2 global variables you can name them like we used to with Hungarian Notation style name. This method is really quite simple and elegant. Old is new and New is Old.

提交回复
热议问题