-2

Guys is it possible to declare a variable globally on one form and be able to call that variable on the another form? Or should I say: how do you declare a variable GLOBALLY?

  • 1
    Google this doubt and there are plenty of examples. – Incredible Oct 15 '13 at 11:25
  • A good learning starting point [Classes vs Modules](http://stackoverflow.com/questions/881570/classes-vs-modules-in-vb-net), then you could refine your knowldege with some basic search – Steve Oct 15 '13 at 11:29

2 Answers2

0

Create a Class that has a Shared Property that you can then access from any Form regardless or whether a Form has been instantiated or not.

Here is a Declare global variables in Visual Studio 2010 and VB.NET describing what to do.

Community
  • 1
  • 1
Ric
  • 12,855
  • 3
  • 30
  • 36
0

Simply make the variable Public. Better still, make it a public property, e.g.:

Private _data As AccountCharacteristics
Public Property Data() As AccountCharacteristics
    Get
        If _data Is Nothing Then
            Data = New AccountCharacteristics()
        End If

        Return _data
    End Get
    Set(ByVal value As AccountCharacteristics)
        _data = value

        If value IsNot Nothing Then
            AccountNumber.Text = value.AccountNumber
        End If
    End Set
End Property

If you want the variable to be shared across all copes of the form, make it Public Shared instead

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • @Shaw, I thank You man. I think you the one who can help me here... I'm developing ordering system where I add a new customer then "customer form" goes to Order form, it like a process flow... So after adding a new customer going to Order Form, I want that customerID we just added to be shared on the Order Form. I don't know if I make sense... So I'm struggling with putting it down in code. – Tshepo T-Man Machabaphala Oct 15 '13 at 15:22