Add a module to your project, call it something like "globals" which
contains your variable. If you set the variable with the "Private" keyword,
you will not be able to read that variable in ANY of your forms. But if you
set it with the "Public" keyword, it will be accessible to all your forms --
from anywhere in your application.
BUT. that goes without saying: "Don't expose what you don't have to." which
basically means, you SHOULD set your variable with the "Private" keyword,
and then add either a "ReadOnly" accessor to read the variable.
For example:
Private _MyVariable As String 'this is a private variable: note the "_"
before the variable name.
'the accessor to read the value
Public ReadOnly Property MyVariable() As String 'note the readonly keyword
Get
Return _MyVariable 'Returns the value of the variable
End Get
End Property
Now if you want to read & write to the variable:
Private _MyVariable As String 'this is a private variable: note the "_"
before the variable name.
'the accessor to read the value
Public ReadOnly Property MyVariable() As String 'note the readonly keyword
Get
Return _MyVariable 'Returns as string value
End Get
Set (ByVal Value As String)
_MyVariable = Value 'accepts a string value
End Set
End Property
By Adding a module that is separate from your forms, you can contain any
number of variables with different accessors. You are not limited to
accessors to read/write to variables, you can use Subroutines "Sub" or
Functions. Example:
Public Sub SetMyVariable(ByVal Str As String)
'set the variable
_MyVariable = Str 'note Str is the parameter that in the routine.
End Sub
Public Funtion MyVariable(ByVal Str As String)
Try
'check to see if you're setting the variable
'or wanting to read from it
Select Case Str
Case "" 'nothing is being passed to the function
Case Else 'something is being passed to the function
'handle the case here
End Select
Catch (Ex As Exception)
Console.WriteLine("Exception: " & Ex.toString())
Exit Function
End Try
End Function
These are just a few examples of how you can deal with variables over
multiple forms. There are other, more efficient ways, but you'll discover
those on your own.
--
Message posted via
http://www.dotnetmonster.com