?
Solved

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

Posted on 2008-10-23
12
Medium Priority
?
238 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:stewdaddy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 5

Expert Comment

by:two_people_hk
ID: 22785760
You can set a Constants in Visual Basic.
For your reference:
http://www.go4expert.com/forums/showthread.php?t=3689
0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22785762
try using shared keyword.

Shared strX As String = "X"
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22785797
declare the variable as member variable in the class so that it is accessible from all functions
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:stewdaddy
ID: 22785810
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22785823
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
 
LVL 13

Accepted Solution

by:
SameerJagdale earned 2000 total points
ID: 22785893
put it in main.
0
 

Author Closing Comment

by:stewdaddy
ID: 31509185
Thanks.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22785948
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
 

Author Comment

by:stewdaddy
ID: 22786044
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22786068

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
 

Author Comment

by:stewdaddy
ID: 22786101
oh ok thanks
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22786121
Glad I could help
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question