Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I clear Public Variables?

Status
Not open for further replies.

tekrobg

Programmer
Oct 12, 2004
42
US
I am new to programming and to VB.net. I have a 5-form program. On the last form, I have a button that will restart the program at Form1 again if the user wants to do it again. However, when it goes to the beginning, it still has the values stored as the Public Variables from the previous use. How can I clear those values of the Public Variables so the user can start fresh?

Here is my code for the button click on Form5:

vb
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If MsgBox("Are you sure you want to start again?", MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Critical) = MsgBoxResult.Yes Then
Dim f As New Form1()
f.Show()
Me.Hide()
Else
Exit Sub
End If
End Sub
/vb

Thanks for any help.
 
on the line before "Dim f As New Form1()" you can set all of your public variables to default values. you might want to assign strings to "" and numbers to 0, but since they're public you can clear or assign them from anywheres.

-Rick

----------------------
 
After a lot of searching in books, google, and this site, I am still unclear what command to use to clear variables. The only thing I could find is the "Set" command, but VB.net says it no longer supports it.

1. What is the command to clear variables?
2. Can I use it for an entire class of variables at one time, or do I have to clear each variable one by one?

Thanks for any help.
 
You have to reset all variables one by one. You just use syntax like the follwing:

intPublicInteger = 0
strPublicString = ""

You can set them to whatever you want, you don't have to use zeroes for integers, nor empty strings for string.
 
Would this work for the clear method?

vb
Dim f As New Form1()
f.Clear()
f.Show()
Me.Hide()
vb/

Opening a new form did not work when I tried it; however, do you have to declare that it is a new form or something like that?

What is the syntax for these two options?

Thanks for the advice.
 
If the user has closed it. an you reopen it while useing the new keyword then all variable in that form will be reset to therre default values.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I tried the following code for a button click, but it does not clear all the variables. My program has 5 forms and I need to clear all the public variables throughout the 5 forms from the previous use.

vb
Me.Close()
Dim f As New Form1()
f.Show()
vb/

Would the clear method clear all 5 forms at once? Am I using the correct syntax?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top