Defining a Setting
Project -> Properties -> Settings

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






MultiQuote





|