none
Global Variables in VB.NET

    General discussion

  • Hello,

    Is there any way in VB.NET to declare a global variable in one form and be able to access it through other forms??

    Thank you.
    Wednesday, May 11, 2005 2:43 PM

All replies

  • Ok,

    You can declare a variable in a module (Create Module) and give the variable the public keyword, then u can access it from all the forms

    To create a variable in a form and access it in another, use the Shared keyword in the declaration


    Hope that help
    Wednesday, May 11, 2005 5:42 PM
  • Can you please give me an example?

    Thank you.
    Thursday, May 12, 2005 3:04 PM

  • Here is an example of a module:

    Module MyModule
        Public MyText As String = "Hello World!"
    End Module


    You can call "MyText" like so:

       MsgBox(MyText)

    -or-

       MsgBox(MyModule.MyText) ' this is more clear and preferred


    Or, in a form:

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Public Shared MyText As String = "Hello World!"

        ...

    End Class


    -Paul
    Thursday, May 12, 2005 4:10 PM
  • I'm glad someone else was wondering the same thing I was.

    One question though:  Where in the form should this code be inserted?
    I inserted it as a Declaration in the General area but I receive a message that tells me that Modules can only be declared at the file or namespace level.

    Any thoughts?

    Thanks,
    Jared
    Tuesday, June 7, 2005 8:29 PM

  • If you're using a module then yes, you'll have to declare it at the namespace level. You can do this simply by adding a new item of type Module to your Visual Studio project.

    -Paul
    Wednesday, June 8, 2005 8:48 AM

  • Here is an example of a module:

    Module MyModule
        Public MyText As String = "Hello World!"
    End Module


    You can call "MyText" like so:

       MsgBox(MyText)

    -or-

       MsgBox(MyModule.MyText) ' this is more clear and preferred


    Or, in a form:

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Public Shared MyText As String = "Hello World!"

        ...

    End Class


    -Paul
    what if the string contains two values?

    -kelvin

    Sunday, January 5, 2014 1:48 AM
  • Kelvin,

    If you have a question, then don't hijack an old one. 

    I was surprised that I saw a question from 2005 in this forum coming back. 

    However, mostly those are seldom answered.

    Create your own question, you don't have to pay for it.


    Success
    Cor


    Sunday, January 5, 2014 9:54 AM
  • In regards to more than one value we can use a Singleton pattern as shown in this project (coded in VS2012 and is VS2010 compliant). In this case I have a class setup in a class project and accessed in a forms project.

    Let's say I want to read a string in form1

    MyLibrary.Example.GetInstance.Comment

    Then in form2 set the Comment property

    MyLibrary.Example.GetInstance.Comment = "Hello"

    Then close Form2, back in form1 read it we get "Hello".


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Sunday, January 5, 2014 4:36 PM
    Moderator
  • Ok, so I am confused about global variables.  Can someone please explain what the global variable is?

    Thanks.

    Saturday, February 22, 2014 12:58 AM
  • Create then your own question instead of hijacking one from 2005, this one is meant to be a source of the knowledge base.

    (Although some of the replies are really awful and probably from that time)


    Success
    Cor

    Saturday, February 22, 2014 9:30 AM
  • <g class="gr_ gr_31 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="31" id="31">Ohhh</g>, shut up. Instead of whining why don't you be useful to the form and put in a reply.
    Friday, January 11, 2019 7:45 PM
  • Ohhh, shut up. Instead of whining why don't you be useful to the form and put in a reply.

    Do you really think that making that response to someone's post from five
    years ago contributes anything positive to the discussion? Regurgitating
    old threads is seldom helpful, especially since so many things change in
    the compilers, tools, languages, etc. over time. Creating a new thread is
    almost always the better choice, although one can certainly include links
    in their posts to older discussions or questions.

    - Wayne

    Sunday, January 13, 2019 12:20 AM