Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Asking about global variable using VB.NET Window Form Application

Asking about global variable using VB.NET Window Form Application

Scheduled Pinned Locked Moved Visual Basic
csharpdatabasemysqlquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D drexler_kk

    I'm new to VB.NET, currently I'm learning programming here. I have a Window Form(e.g. FrmLogin.vb) and a module which used to run the program (e.g. Module1.vb). I have declared a few public string variable as follow:

    Option Explicit On
    Imports System.Data
    Imports System.Data.Odbc
    Imports System.Configuration

    Module Module1
    'User detail variable
    Public UserID As String
    Public UserName As String
    Public UserPassword As String
    End Module

    And I used the FrmLogin to verify the user by connecting to MySQL database. After the user successful login,I will set the UserID,UserName and UserPassword data load from the database which need to be used in my program for the future identification. But when I open another new form I found that these public variable is no value. Can anyone tell me how should I share a public variable inside VB.NET? This data will be used by alot of Form inside this application. Thank you for reading. Regards Drexler

    A Offline
    A Offline
    Ashfield
    wrote on last edited by
    #2

    Sorry, we cannot see your question. Please reformat your post so all the text is visible.

    Bob Ashfield Consultants Ltd

    D 1 Reply Last reply
    0
    • D drexler_kk

      I'm new to VB.NET, currently I'm learning programming here. I have a Window Form(e.g. FrmLogin.vb) and a module which used to run the program (e.g. Module1.vb). I have declared a few public string variable as follow:

      Option Explicit On
      Imports System.Data
      Imports System.Data.Odbc
      Imports System.Configuration

      Module Module1
      'User detail variable
      Public UserID As String
      Public UserName As String
      Public UserPassword As String
      End Module

      And I used the FrmLogin to verify the user by connecting to MySQL database. After the user successful login,I will set the UserID,UserName and UserPassword data load from the database which need to be used in my program for the future identification. But when I open another new form I found that these public variable is no value. Can anyone tell me how should I share a public variable inside VB.NET? This data will be used by alot of Form inside this application. Thank you for reading. Regards Drexler

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #3

      From version 7 (VB.NET 2003) VB is an object oriented language, which means that there are no longer any global variables. All variables are either members of a class or local in a method. To set up variables that you can use like you did use global variables, you can use static variables. You use the Shared keyword to do this, and those variables are members of a class rather than members of an instance of the class.

      Public Class UserDetails
      Public Shared UserId As String
      Public Shared UserName As String
      Public Shared UserPassword As String
      End Class

      You can access the variables from anywhere by specifying the class name and the variable name: UserDetails.UserId = "42"

      Despite everything, the person most likely to be fooling you next is yourself.

      D 1 Reply Last reply
      0
      • G Guffa

        From version 7 (VB.NET 2003) VB is an object oriented language, which means that there are no longer any global variables. All variables are either members of a class or local in a method. To set up variables that you can use like you did use global variables, you can use static variables. You use the Shared keyword to do this, and those variables are members of a class rather than members of an instance of the class.

        Public Class UserDetails
        Public Shared UserId As String
        Public Shared UserName As String
        Public Shared UserPassword As String
        End Class

        You can access the variables from anywhere by specifying the class name and the variable name: UserDetails.UserId = "42"

        Despite everything, the person most likely to be fooling you next is yourself.

        D Offline
        D Offline
        drexler_kk
        wrote on last edited by
        #4

        Hello Guffa, Do you mean that I should create all the public share variable into one main module,so all the form can use all the data inside this main module? And how can I insert or retrieve the data of UserID when I need it in other form from this class? Hope to get some solution about this from you all. Thank you~!

        G 1 Reply Last reply
        0
        • A Ashfield

          Sorry, we cannot see your question. Please reformat your post so all the text is visible.

          Bob Ashfield Consultants Ltd

          D Offline
          D Offline
          drexler_kk
          wrote on last edited by
          #5

          Sorry Ashfield, What I need is declaring a variable for global used in my Vb.NET Window Form Application. I am trying to declare the global variable inside a Module1.vb. I have a few forms that using this global varaiable and it doesn't really works. Anyone could tell me how to declare a global variable which can share the variable used in all other different forms or class? Regards Drex

          1 Reply Last reply
          0
          • D drexler_kk

            Hello Guffa, Do you mean that I should create all the public share variable into one main module,so all the form can use all the data inside this main module? And how can I insert or retrieve the data of UserID when I need it in other form from this class? Hope to get some solution about this from you all. Thank you~!

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #6

            Modules is something that for some reason survived from VB6. A module is compiled into a class where all the members are static, so if you declare variables in a module, you should be able to access them from anywhere.

            Despite everything, the person most likely to be fooling you next is yourself.

            D 2 Replies Last reply
            0
            • G Guffa

              Modules is something that for some reason survived from VB6. A module is compiled into a class where all the members are static, so if you declare variables in a module, you should be able to access them from anywhere.

              Despite everything, the person most likely to be fooling you next is yourself.

              D Offline
              D Offline
              drexler_kk
              wrote on last edited by
              #7

              Yeah,thanks Guffa. Can you give me some idea how should I declare the variable of my Window Form Application? Let me further explain my situation below:

              Option Explicit On
              Imports System.Data
              Imports System.Data.Odbc
              Imports System.Configuration

              Module Module1
              'User detail variable
              Public UserID As String
              Public UserName As String
              Public UserPassword As String
              End Module

              This is how I test my retrieve data using Form from the varaiable in Module1.vb:

              Messagebox.show(UserID)
              Messagebox.show(UserName)
              Messagebox.show(UserPassword)

              But all the messagebox prompup is Nothing. So,anyone can tell me how should I declare my variable so I can used anywhere in my project? The UserID,Username and Password is used for verification when working with certain function or task. Thank you for reading. Regards Drexler

              J 1 Reply Last reply
              0
              • D drexler_kk

                Yeah,thanks Guffa. Can you give me some idea how should I declare the variable of my Window Form Application? Let me further explain my situation below:

                Option Explicit On
                Imports System.Data
                Imports System.Data.Odbc
                Imports System.Configuration

                Module Module1
                'User detail variable
                Public UserID As String
                Public UserName As String
                Public UserPassword As String
                End Module

                This is how I test my retrieve data using Form from the varaiable in Module1.vb:

                Messagebox.show(UserID)
                Messagebox.show(UserName)
                Messagebox.show(UserPassword)

                But all the messagebox prompup is Nothing. So,anyone can tell me how should I declare my variable so I can used anywhere in my project? The UserID,Username and Password is used for verification when working with certain function or task. Thank you for reading. Regards Drexler

                J Offline
                J Offline
                Jay Royall
                wrote on last edited by
                #8

                It looks like you have declared your variables correctly, the reason the messagebox is showing nothing is becasue you haven't assigned them a value. Try UserID = "12345" Messagebox.Show(UserID)

                D 1 Reply Last reply
                0
                • J Jay Royall

                  It looks like you have declared your variables correctly, the reason the messagebox is showing nothing is becasue you haven't assigned them a value. Try UserID = "12345" Messagebox.Show(UserID)

                  D Offline
                  D Offline
                  drexler_kk
                  wrote on last edited by
                  #9

                  Yeah,thanks Liqs. I have solve it by converting the data to string instead of boolean when I assign into the variable. This solve my problem,thank you my friend. Regards :) Drex

                  1 Reply Last reply
                  0
                  • G Guffa

                    Modules is something that for some reason survived from VB6. A module is compiled into a class where all the members are static, so if you declare variables in a module, you should be able to access them from anywhere.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    D Offline
                    D Offline
                    drexler_kk
                    wrote on last edited by
                    #10

                    Yeah,thanks Guffa. I have solve it by converting the data to string instead of boolean when I assign into the variable. This solve my problem,thank you my friend. Regards Drex

                    G 1 Reply Last reply
                    0
                    • D drexler_kk

                      Yeah,thanks Guffa. I have solve it by converting the data to string instead of boolean when I assign into the variable. This solve my problem,thank you my friend. Regards Drex

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #11

                      So the problem was not at all in the code that you posted, but in some other code that you didn't post... Assigning a boolean value to a string variable is such an obvious error. I wonder why I didn't see that by looking at the code that you posted? ;)

                      Despite everything, the person most likely to be fooling you next is yourself.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups