Example: How many times has the user run this application.
Defining a SettingProject -> Properties -> Settings

Now in the grid where is says
SettingRename it to
NumberOfTimesRunThis is the name of the setting. We'll be using this like it is variable laterChange the type to
LongThis 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
0This is the default value the setting will have.CODE
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 ThemSaving them isn't automatic
CODE
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 ThemCODE
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 SettingsNow 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 ListProject -> Properties -> Settings
Add a setting call
MyListType -> Browse -> System.Collections.ArrayList
CODE
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
NothingNow you're Settings using Code Ninja
