Member Avatar for Rahul47

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

Recommended Answers

All 6 Replies

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

Member Avatar for Rahul47

@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

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.

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
commented: This is how I would do it. Good Post. +3
Member Avatar for Rahul47

Declaring variables as Public Shared <var_name> also works .

Thanks to All of you !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.