variable issue

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I have this code to work with the My.Settings variables that are kept when the program is ended, and restart. This is giving me problems, because if you add and delete zip codes, even if you don't have three in use, it says they're all being used, and I can't figure out why.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] AddFavLocation_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] AddFavLocation.Click
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ToolStripComboBox1.Text <> [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipOne = [/SIZE][SIZE=2][COLOR=#800000]"FavoriteZipOneEmpty"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipOne = ToolStripComboBox1.Text
ToolStripComboBox1.Items.Add(ToolStripComboBox1.Text)
ToolStripComboBox1.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipTwo = [/SIZE][SIZE=2][COLOR=#800000]"FavoriteZipTwoEmpty"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipTwo = ToolStripComboBox1.Text
ToolStripComboBox1.Items.Add(ToolStripComboBox1.Text)
ToolStripComboBox1.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipThree = [/SIZE][SIZE=2][COLOR=#800000]"FavoriteZipThreeEmpty"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipThree = ToolStripComboBox1.Text
ToolStripComboBox1.Items.Add(ToolStripComboBox1.Text)
ToolStripComboBox1.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE][SIZE=2] : MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"All three of your Favorites are in use. Please delete one before adding a new ZIP."[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE][SIZE=2] : MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"Please enter a ZIP code in the Favorite ZIP Codes text box to add it to your favorites."[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
 
 
 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DeleteFavLocation_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] DeleteFavLocation.Click
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ToolStripComboBox1.Text <> [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipOne = ToolStripComboBox1.Text [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipOne = [/SIZE][SIZE=2][COLOR=#800000]"FavoriteZipOneEmpty"
[/COLOR][/SIZE][SIZE=2]ToolStripComboBox1.Items.Remove(ToolStripComboBox1.SelectedItem)
ToolStripComboBox1.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipTwo = ZIP.Text [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipTwo = [/SIZE][SIZE=2][COLOR=#800000]"FavoriteZipTwoEmpty"
[/COLOR][/SIZE][SIZE=2]ToolStripComboBox1.Items.Remove(ToolStripComboBox1.SelectedItem)
ToolStripComboBox1.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipThree = ZIP.Text [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.FavoriteZipThree = [/SIZE][SIZE=2][COLOR=#800000]"FavoriteZipThreeEmpty"
[/COLOR][/SIZE][SIZE=2]ToolStripComboBox1.Items.Remove(ToolStripComboBox1.SelectedItem)
ToolStripComboBox1.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
I tried the code, and it seems in itself, the code is good, but you may probably need to set the default value of the my.settings variable to your empty strings, "FavoriteZipTwoEmpty", etc. at the place where you added the variables. Personally, because you already check for empty strings, I would use an empty string, "", rather than "FavoriteZipOneEmpty", so you would not need to change anything.
 
I tried it with "" first and still had problems, so I decided to try it with "FavoriteZipOneEmpty", etc. It's still not working. I changed all three variables in the application settings to empty strings, but when I debug it, it still shows "FavoriteZipOneEmpty" as the value, and even sometimes has a zip code stored from when I tested before. It also doesnt let me put all three zip codes in for my favorites. I really have no idea why it is doing this.
 
Have you tried changing the strings to "" in the code and the settings and deleting the file the settings are stored in (somewhere in Application Data in Documents and Settings)
 
I just tried changing the settings, and the log files, but it still has problems. Sometimes I get "" on my zip code list, and it still has problems with not letting me have three zip codes.
 
I figured out the problem. In the application settings where I had the variables set for it to remember, I typed "" because I thought that would be an empty string. I think that "" works as an empty string in the code, but in the application settings I think it ended up being a string literally equal to "". It works now. Thanks for your help.
 
Back
Top