Global Variable in VB.NET


How to carry a value from one form to another form in vb.net windows applications?

Web applications contains facilities to transfer the value from one form to another using server objects like Session, Application. Same way in windows applications we can use global variables able to solve this problem. For ex., In your windows application you want to display welcome “user name” after users logged in application, how can? Please follows below one.

Global Variable

In computer programming, a global variable is a variable that is accessible in every scope. They are contrasted with local variables

please go wikipedia for more..

http://en.wikipedia.org/wiki/Global_variables

In vb.net application you should declare the global variables in modules,but c# calss itself,first add module.vb file in your application using solution explorer.

Module Module1
Public strCustomerID As String ‘variable declared with public keyword
End Module

In our application have two forms first one is login form(frmLogin.vb) and another one mainform(frmMain.vb),our criteria show the username in mainform after logged in.show first assign the login form(frmLogin.vb) user name textbox value into global variable strCustomerID.

Assign the value to variable

frmLogin.vb login button event you can get the user name.

Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click

strCustomerID =me.txtUsername.text

End Sub

Display the Value

As per our requirement “How to display the username in main form(frmMain.vb)In frmMain.vb form load event you can able to get the username.

Me.Label1.Text = “Welcome ” & strCustomerID

After using this variable,you should clear this object memory using nothing keyword.

strCustomerID = nothing(object is free)

Happy Programming!

Advertisements

5 thoughts on “Global Variable in VB.NET

  1. Pingback: how to reset password on windows 7

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s