How To Use Project Settings in Visual Basic

Introduction: This tutorial is on how to use Settings in Visual Basic. Settings: Project Settings in Visual Basic project solutions are simple ways to carry data over from one running instance of the application to another. Instead of variables and objects where the data gets cleared once the process is stopped or files which holding data between running processes which can be accessed by the users of the machine, project settings are unique to the application and stored permanently until modified within the application itself (or an attached process). Creating Settings: We are going to create an example setting name 'user' to hold the fake username of the logged in user to the application. So first we must create the setting. To do this, open your Visual Studio/Basic application project solution, then go to the following... 'Solution Explorer', right click on '{Your Project Name}', then go to 'Properties'. Now, enter a string (the setting/variable/object name) in the 'Name' column of the 'Settings' tab. Set the type to the appropriate type (for this setting it is already a String, so don't interact with the first dropdown box column 'Type'), leave the 'Scope' as 'User' and you may set a default 'Value' if you wish. Save the settings using Ctrl + S keyboard shortcut. Reading Settings: To read a setting from within the application, we simple type...
  1. My.Settings.{Name}
So to output our 'User' setting, we type...
  1. MsgBox(My.Settings.User)
Modifying Settings: To modify the value of a setting we use the same code as reading it, except with an assignment operator ('=') and a new value following the operator, like so...
  1. My.Settings.{Name} = {New Value}
So to change the 'User' Setting value to 'Yorkiebar', we would type...
  1. My.Settings.User = "Yorkiebar"

Add new comment