I'm looking for a way to eliminate many unwanted global variables. Any suggestions? I'm relatively familiar with the basics of VB but not to a distant extent.
The question is, Why are they global? Are they only used in one place? Are they used in several modules, forms, etc? If you have to maintain a value to be used in several places then it needs to be a public variable. If used in one place then where you use it declare it there as private.
Give us an example so we can give you some clues.
They are global so that multiple procedures can modify their contents. However, I would like for other forms to be able to modify as well. The problem is they are only global throughout the form, and I would like for other forms to be able to modify the contents of the variables.
If they are global to a form then they are declared as private in the form and are not global.
Private x As String = ""
If you have them defined as:
Public x As String = ""
then they are global to the application.
I would control these variables and keep them private but make properties to pass data to and from the variable. This way you can check the input data for correctness(?).
Private x As String Public Property newX() As String Get Return x End Get Set(ByVal value As String) If value.Substring(0, 1) = "z" Then messagebox.Show("Don't use a string starting with a 'z'" Else x = value End If End Set End Property
Then you can use:
Me.newX = "now is the time" Me.newX = "zone"
Thanks, that solved a few problems. I still can't get the variables to be global throughout the application. Here I declared the variables as:
Public Class Form1 Public strFileName1 As String Public strFileName2 As String Public strFileName3 As String
But when I reference to those variables in another form, I get a syntax saying the variable "strFileName1" is not declared.