0

Hey fellas,
Is there any method by which I can declare a Variable that can be used in multiple forms in VB.NET ?

4
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by Rahul47
Featured Replies
  • Public Module Module_Global_Variables Friend Global_Emp_CD As String Friend Global_User_Type As String End Module Now you can use Variables as normal variables Read More

0

Declare the variable with the Public keywaord in the top most class of your form, i.e.

Public Class Form1
    Public MyVariable As String = "Test String"
    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Do stuff
    End Sub
End Class

Access it as a member of Form1, i.e. Label1.Text = Form1.MyVariable

0

@tinstaafl: Uhuh.... Earth Didn't Moved !!

I actually want to assign a value to variable x upon close button click which is in form2 but want to use x in form1.

There was no respose upon

0

As long as you declare the variable public in the topmost class you can access it anywhere and assign it any value of the right type.

1
Public Module Module_Global_Variables
    Friend Global_Emp_CD As String
    Friend Global_User_Type As String
End Module

Now you can use Variables as normal variables
Votes + Comments
This is how I would do it. Good Post.
0

Declaring variables as Public Shared <var_name> also works .

Thanks to All of you !

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.