The My Namespace in Sedna New to Sedna, Visual FoxPro emulates the My namespace first introduced in Visual Basic 2005. The My namespace makes .NET Framework classes more discoverable and allows you to write less code. Sedna, the next version of Visual FoxPro (VFP), includes a My namespace as well, for the same reasons. In this article, I’ll look at how Sedna implements My. In his MDSN article “Navigate the .NET Framework and Your Projects with My” (http://msdn.microsoft.com/msdnmag/issues/04/05/VisualBasic2005/default.aspx), Duncan Mackenzie provides an example of why My is a great addition to Visual Basic (VB). Instead of writing the following to read the contents of a text file: Dim sr As New IO.StreamReader("c:\file.txt") contents = sr.ReadToEnd sr.Close()
you can write this: contents = _ My.Computer.FileSystem.ReadAllText("c:\file.txt")
Thanks to IntelliSense on the My namespace, not only is it easier to figure out how to do this task, it’s also less code to write and debug. | " | The My help file, My.CHM, documents the My namespaces and their properties and methods in detail, including sample code.
| " |
Sedna includes a My namespace as well, for the same reasons that VB 2005 does. Many of the My classes are wrappers for SYS() functions, Windows API functions, Windows Script Host properties and methods, and so on. For example, the Play method of Audio, which plays an audio file, is a wrapper for the sndPlaySound Windows API function. So, without having to DECLARE this function or even know it exists, your VFP application can play a sound file. You can replace this code: #define SND_SYNC 0 declare integer sndPlaySound in WinMM.dll ; string lpszSoundName, integer uFlags sndPlaySound(SoundFile, SND_ASYNC)
with this: My.Computer.Audio.Play(SoundFile)
In the article, I’ll introduce My in Sedna, showing you some of the namespaces available. I’ll also describe in detail how My controls IntelliSense to display just the members of the namespace you want to see, and how it dynamically instantiates a class hierarchy at run time. Finally, I’ll show you how to extend My to add your own classes as namespaces so they’re easily accessible. Introduction to My My is included with the Sedna Community Technology Preview (CTP) available from the VFP Web site (http://msdn.microsoft.com/vfoxpro). You must register My with IntelliSense before you can use it; to do so, run My.APP. You can then type “LOCAL My as” in a code window and choose My from the list of types that appears. The following code is automatically inserted: local My as My My = newobject('My', 'my.vcx')
Type “My.” to see a list of the namespaces available within My. They are: - App: provides application methods and properties, including Execute to open a file such as an HTML document.
- Computer: provides access to various components of the computer system, including the file system, audio, printers, and Registry.
- Data: provides data-handling features, such as methods to close all cursors opened by some code.
- Settings: provides methods to save and restore application settings, such as form size and position and user configuration settings. Interestingly, this class saves settings in an XML file using the same schema as VB’s My.
- User: provides information about the current user, such as their full name and domain.
The My help file, My.CHM, documents these namespaces and their properties and methods in detail, including sample code. To use My in a development environment, be sure to SET PATH to the directory containing My.VCX. Examples The sample form for this article (see the Download sidebar) demonstrates some of the My classes. For example, the following code in Init restores the former size and position of the form: local My as My This.oMy = newobject('My', 'my.vcx') My = This.oMy if file('sample.xml') My.Settings.Load('sample.xml') if My.Settings.Exists('FormTop') This.Top = My.Settings.FormTop This.Left = My.Settings.FormLeft This.Height = My.Settings.FormHeight This.Width = My.Settings.FormWidth endif My.Settings.Exists('FormTop') endif file('sample.xml')
Note this code instantiates My into a form property so it’s available in any method requiring My but the code declares the local variable My of type My and stores the form property into that variable so IntelliSense works properly. The code in Destroy saves the form size and position: local My as My My = This.oMy My.Settings.Add('FormTop', This.Top) My.Settings.Add('FormLeft', This.Left) My.Settings.Add('FormHeight', This.Height) My.Settings.Add('FormWidth', This.Width) My.Settings.Save('sample.xml')
Init also uses properties of My.Computer.FileSystem.SpecialDirectories, such as Desktop and MyDocuments, to populate a list of the locations of certain directories on your system. The Click method of the Download File button downloads and displays an HTML document: local My as My My = Thisform.oMy lnResult = ; My.Computer.Network.DownloadFile('http://' + ; 'downloads.stonefield.com/pub/repobj.html', ; 'repobj.html') if lnResult = 0 My.App.Execute('repobj.html') else messagebox('File download failed.') endif lnResult = 0
Note the simplicity of this code: you don’t have to know what Windows API function to call to download a file from a Web site or to display an HTML document in a browser. | & | | 
By: Doug Hennig
Doug Hennig is the author of the award-winning Stonefield Database Toolkit (SDT), the award-winning Stonefield Query, the MemberData Editor, Anchor Editor, and CursorAdapter and DataEnvironment builders that come with Microsoft Visual FoxPro, and the My namespace and Upsizing Wizard in Sedna. Doug is co-author of the What’s New in Visual FoxPro series (the latest being What’s New in Nine) and The Hacker’s Guide to Visual FoxPro 7.0, available from Hentzenwerke Publishing (http://www.hentzenwerke.com).
Doug has spoken at every Microsoft FoxPro Developers Conference (DevCon) since 1997 and at user groups and developer conferences all over the world. He is one of the administrators for the VFPX community extensions Web site (http://www.codeplex.com/Wiki/View/aspx?ProjectName=VFPX).
Doug has been a Microsoft Most Valuable Professional (MVP) since 1996 and was named the 2006 winner of the FoxPro Community Lifetime Achievement Award. His Web sites are http://www.stonefield.com and http://www.stonefieldquery.com, and his blog is at http://doughennig.blogspot.com.
dhennig@stonefield.com | Fast Facts | | New to Sedna, Visual FoxPro emulates the My namespace first introduced in Visual Basic 2005. It makes complex tasks, such as downloading files from Web sites or determining the location of a user’s MyDocuments folder, both discoverable and easy. Even better, it’s data-driven and extensible so you can add your own classes. | |
|