How do I declare a variable and assign the value once, and then reuse that variable in multiple procedures?

Ok, this one is really basic, but for some reason I can't figure it out or find the info on the net.  I want to declare a variable in one spot, say:
Dim strX as string = "X"
and then reference that variable in multiple procedures/functions without having to re-declare it.  The reason being I want to assign paths to the variables, and when the paths change I want to only have to update the variables in one location.
stewdaddyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
SameerJagdaleConnect With a Mentor Commented:
put it in main.
0
 
two_people_hkCommented:
You can set a Constants in Visual Basic.
For your reference:
http://www.go4expert.com/forums/showthread.php?t=3689
0
 
SameerJagdaleCommented:
try using shared keyword.

Shared strX As String = "X"
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TechTiger007Commented:
declare the variable as member variable in the class so that it is accessible from all functions
0
 
stewdaddyAuthor Commented:
Where do I declare the variable though? I thought you do it in the declarations section, but I keep getting a "Statement is not valid in a namespace." error.
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
You have several options:
- Declare in a module as Public
- Declare in a class as Public Shared
- Define on the settings (My Project - Settings Tab)
0
 
stewdaddyAuthor Commented:
Thanks.
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Some examples:
' **** CLASS ****
Public Class Class1
    Public Shared myVar As String = "some text"
 
End Class
 
' Then in the form
MessageBox.Show(Class1.myVar)
 
 
' **** MODULE ****
 
Public Module Module1
    Public myNewVar As String = "some text"
 
End Module
 
 
' Then in the form
MessageBox.Show(myNewVar)

Open in new window

0
 
stewdaddyAuthor Commented:
jpaulino...
If I go with the module approach, how do I use input from the form, such as:

Public Module Variables
    'Project Directory
    Public strProjectDirectory As String = "J:\" & txtProject.Text & "\"
End Module

VB doesn't let me use txtProject.text
0
 
Jorge PaulinoIT Pro/DeveloperCommented:

Not that way. You have to declare the variable in the module:

Public strProjectDirectory As String

Then in any place (like in form load event) you can assign the value:

strProjectDirectory  = "J:\" & txtProject.Text & "\"
 
After this you can use it in any place.

 
0
 
stewdaddyAuthor Commented:
oh ok thanks
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Glad I could help
0
All Courses

From novice to tech pro — start learning today.