Page 1 of 1

Using Settings How to use settings to preserve information between runs. Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Posted 19 November 2008 - 04:29 PM

Example: How many times has the user run this application.

Defining a Setting

Project -> Properties -> Settings

Attached Image

Now in the grid where is says Setting
Rename it to NumberOfTimesRun
This is the name of the setting. We'll be using this like it is variable later
Change the type to Long
This determines the type for this particular setting. More on this later.
Change the scope to User
  • Application - Specific to the application regardless of which user and are READ ONLY
  • User - Allow applies to the current user.

Set value to 0
This is the default value the setting will have.

Module Module1

	Sub Main()
	   Console.WriteLine("Number of times run  {0} ", My.Settings.NumberOfTimesRun)
		My.Settings.NumberOfTimesRun += 1
		Console.WriteLine("Number of times run  {0} ", My.Settings.NumberOfTimesRun)

	End Sub

End Module	   


Run it, see what happens.

Saving Them
Saving them isn't automatic
Module Module1

	Sub Main()
	   Console.WriteLine("Number of times run  {0} ", My.Settings.NumberOfTimesRun)
		My.Settings.NumberOfTimesRun += 1
		Console.WriteLine("Number of times run  {0} ", My.Settings.NumberOfTimesRun)
 ' Save the settings
		My.Settings.Save()
	End Sub

End Module	   



Resetting Them
Module Module1

	Sub Main()
		Console.WriteLine("Number of times run  {0} ", My.Settings.NumberOfTimesRun)
		My.Settings.NumberOfTimesRun += 1
		Console.WriteLine("Number of times run  {0} ", My.Settings.NumberOfTimesRun)
		' Save the settings
		My.Settings.Save()
		Console.WriteLine("Reset to original (Y/N)")

		Select Case Console.ReadKey.KeyChar
			Case "Y", "y"
				'Reset them back to default values
				My.Settings.Reset()

		End Select

	End Sub

End Module




Advance Settings
Now you can save simple setting, I hear some of you say, "but what if I want store a list of things in the setting?"

Saving a List
Project -> Properties -> Settings
Add a setting call MyList
Type -> Browse -> System.Collections.ArrayList

Module Module1
	   Sub Main()
		' We need to check that the Settings.MyList is not Nothing, ie it is Empty
		If My.Settings.MyList Is Nothing Then
			' If it is Nothing then create a new instance
			My.Settings.MyList = New ArrayList
		End If


		Console.WriteLine("MyLIst contains {0} items ", My.Settings.MyList.Count)
		My.Settings.MyList.Add("Dream In Code")
		Console.WriteLine("MyLIst contains {0} items ", My.Settings.MyList.Count)
		' Save the settings
		My.Settings.Save()
		Console.WriteLine("Reset to original (Y/N)")
		Select Case Console.ReadKey.KeyChar
			Case "Y", "y"
				'Reset them back to default values
				My.Settings.Reset()
		End Select

	End Sub

End Module


Note: That we need to check that the Setting isn't Nothing

Now you're Settings using Code Ninja :ph34r:

Is This A Good Question/Topic? 0
  • +

Replies To: Using Settings

#2 Rickster090  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 100
  • Joined: 10-January 08

Posted 18 December 2008 - 07:12 AM

Nice 've been wondering about those settings...thanks for this cool Tut
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Posted 30 December 2008 - 03:15 PM

Additional Info

Be aware of this complication to using settings.

Premise of example: Checking to see if the application as been run more than once.
  • My.Settings.FirstRun = True.
  • You run the program.
  • A Conditional in the code tests to see if My.Settings.FirstRun equates to true.
  • It does and sets and saves My.Settings.FirstRun as False
  • You run the program again.
  • The conditional equate to false.
Anyone spot the problem?


Nope. The problem is that the setting persists between runs. So now if copy the exe it thinks it still been run more than once.
So a quick solution to is this
#Const DEBUG_KeepFirstRunAsTrue = True
#Const DEBUG_ResetSetting = True
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
#If DEBUG_KeepFirstRunAsTrue Then
  If My.Settings.FirstRun Then
   My.Settings.FirstRun = False
   My.Settings.Save()
   Beep()
  End If
#End If
#If DEBUG_ResetSetting Then
  My.Settings.Reset()
  My.Settings.Save()
#End If


By toggling the constants you can test the code and importantly reset the setting.

This post has been edited by AdamSpeight2008: 30 December 2008 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 SpenzeR92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-August 12

Posted 28 August 2012 - 01:19 AM

I know this is an old thread, but I had the same issue, and the easiest way to check if it isn't empty is by doing so:

I got 3 examples on how you can check if My.Settings.MyList is not empty.

Module Module1
	Sub Example1()
		If My.Settings.MyList Is Nothing Then
			MsgBox("Your list is empty!")
		ElseIf My.Settings.MyList Is Nothing = False Then
			MsgBox("Your list is not empty!")
		End If
	End Sub

	Sub Example2()
		If My.Settings.MyList Is Nothing Then
			MsgBox("Your list is empty!")
		Else
			MsgBox("Your list is not empty!")
		End If
	End Sub

	Sub Example3()
		If My.Settings.MyList Is Nothing = False Then
			MsgBox("Your list is not empty!")
		End If
	End Sub
End Module


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1