As VB developers encounter .NET, they find both new and familiar things. What is familiar? Well, when applications
require the use of user-specific settings, they require as well that programs persist data to disk. What is new? With .NET, this can mean converting to XML via XML Serialization.
In a useful article in the recent edition of MSDN Magazine, Duncan Mackenzie, software design engineer, Microsoft, describes ways of "remembering" user information in VB.NET. There are some easier ways to make this happen with the upcoming VB 2005 release, but methods of XML serialization that Mackenzie outlines in some detail are of ongoing interest for various reasons, not the least of which is that they have application in a variety of other important tasks.
"With .NET, settings have actually been handled in a couple of different ways. One of the things people will find is that there has been a lot of support for application-level settings," said Mackenzie, in a conversation with SearchVB.com. But, he continued, nothing quite similar in the way of application-level support was available for user-level settings.
In any case, where you save settings is important, and mixing up application-level settings and user-level settings is not a good thing.
In the MSDN Magazine article, Mackenzie shows people where they should be saving their settings. "Sometimes people make the mistake of saving user-settings into the same directory as their applications," he told us in conversation. And that creates security rights problems, among other problems. An obvious but inappropriate workaround is to give everyone administrator rights.
In his article, Mackenzie writes: "After developers writing within .NET have used the application configuration file and its easy programmatic interface, they wonder why they can't just use the same file to store user settings ... shortly after, they discover that the System.Configuration namespace does not provide any method for writing to the app.config file." Developers discover workarounds in the form of direct loading and editing using XmlDocument, or regular file IO, he notes.
This is not a good idea, Mackenzie says, because .NET administrative permissions must be kept separate from user preferences, user isolation is required for good security, and the app.config file is part of the application's installation.
So, what about XML Serialization? In fact, XML serialization is a way to turn your objects into XML. It is used a lot in the .NET framework. In the case of Mackenzie's user-settings example, it is a useful means to handle classes that represent user-specific settings, and to file them on disk.
Some of the disjoint between user and app setting development goes away with the advent of VB 2005.
"In VB 2005 all of that built-in support that was in [the previous VB toolset] for application-level settings has been extended to user-level settings. So creating your own classes and saving your own settings isn't required in 2005," said Mackenzie. You use My.settings to accomplish this and more.
Simpler programming is always welcome. But knowledge of XML serialization such as that conveyed in Mackenzie's writing is valuable. Said Mackenzie: "XML serialization isn't just for user settings. You can use it anytime that you want to turn objects into XML files on disks. We focused here on the settings side, but XML serialization has many applications in your .NET programming."
Remembering User Information in Visual Basic .NET, by Duncan Mackenzie on MSDN online
Using My.Settings in Visual Basic 2005 by Emad Ibrahim on MSDN online
Duncan Mackenzie site
Duncan Mackenzie blog