Datasheet

26
CHAPTER 1 VISUAL STUDIO 2010
Figure 1 - 12 illustrates several elements related to the application settings capabilities of Visual Basic.
The fi rst setting is of type String. Under .NET 1.x, all application settings were seen as strings, and this
was considered a weakness. Accordingly, the second setting, LastLocation, exposes the Type drop - down,
illustrating that under Visual Studio 2010 you can create a setting that has a well - defi ned type.
However, strongly typed settings are not the most signi cant set of changes related to application settings.
The very next column defi nes the scope of a setting. There are two possible options: application wide or user
speci c. The settings defi ned with application scope are available to all users of the application. As shown in
Figure 1 - 12, this example creates a sample connection string to store for the application.
The alternative is a user - speci c setting. Such settings have a default value; in this case, the last location
defaults to 0,0. However, once a user has read that default setting, the application generally updates and
saves the user - speci c value for that setting. As indicated by the LastLocation setting, each user of the
application might close it after having moved it to a new location on the screen; and the goal of such a
setting would be to reopen the application where it was last located. Thus, the application would update
this setting value, and Visual Basic makes it easy to do this, as shown in the following code:
My.Settings.LastLocation = Me.Location
My.Settings.Save()
That s right Visual Basic requires only two lines of code that leverage the My namespace in order for
you to update a user s application setting and save the new value. Meanwhile, let s take a look at what is
occurring within the newly generated
app.config le. The following XML settings demonstrate how the
app.config le defi nes the setting values that you manipulate from within Visual Studio:
< ?xml version="1.0" encoding="utf-8" ? >
< configuration >
< configSections >
< sectionGroup name="userSettings" type="System.Configuration.
UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" >
FIGURE 1 - 12
CH001.indd 26CH001.indd 26 4/5/10 11:56:44 AM4/5/10 11:56:44 AM