Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Can any one tell where i put variables that can be accessed from both outside and insite a button
e.g i have,
--------------Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Saturday Results
Dim satStart As Double = ComboBox1.Text
Dim satFinish As Double = ComboBox2.Text
Dim satStartMins As Double = ComboBox3.Text
Dim satFinishMins As Double = ComboBox4.TextEnd Sub
--------------------where can i put these variables so that i can acces them from both outside and inside this sub?
Can anyone help?Dave

Define them outside of any function the form you wish to use themin the General Delarations section of the form.
You can then use them anywhere in the form. If you wish to use them elsewhere in the project, define them as Public. They are Private by default and are only availabel while that module is active.
Stuart

thanks, but I tried putting them outside any function but i get an error
"An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object."
I put this outside:
Dim sunStartHours As Double = ComboBox5.Text

You define the variable first outside the function.
Then allocate it a value within the function you want to use it.
Dim sunStartHours As Double
outside the function
sunStartHours = ComboBox5.Text
inside the function.
It is never good programming practice to both define a variable and allocate it a value both in the same statememnt. Although it can be done, it can lead to also of problems later if you want to change anything.
Stuart

![]() |
Batch File Directory List...
|
Prime Number problem in j...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |