HOWTO: Use the Session Class to Provide a Private Data Session

ID: Q248116


The information in this article applies to:
  • Microsoft Visual Studio 6.0 SP3
  • Microsoft Visual FoxPro for Windows, version 6.0


SUMMARY

Visual Studio 6.0 Service Pack 3 added the Session Class to Visual FoxPro. This class allows you to create a private data session without the use of a form. The benefit to this is that it allows you to create a lightweight (in terms of resource usage) custom object with encapsulated data. You previously would have needed a form, with all its associated overhead, or an empty formset to do this.

One of the best places to use this is in multithreaded DLLs (also added in Service Pack 3) since resource usage is at a premium in most cases where you would be using them.


MORE INFORMATION

An example of how to use this new class follows.


DEFINE CLASS myPrivateDSClass AS Custom
HIDDEN oSession    && You don't want people to be able to change this

PROCEDURE Init
    This.oSession = CREATEOBJECT("Session")
    This.oSession.Name = "MyPrivateDS"
    SET DATASESSION TO (This.oSession.DataSessionID)
ENDproc

PROCEDURE UseTable
    LPARAMETER tcTable
    
    IF NOT USED(tcTable)
        USE (tcTable) IN 0
    ENDif
ENDproc

PROCEDURE Destroy
    CLOSE DATA ALL

    This.oSession = .NULL.
ENDproc 

To see this class in action, save the above code into MyDS.prg and run the following code from the Command window.

CREATE TABLE myDSTest (cTest C(10))
USE DBF() SHARED
*!* CREATE TABLE makes an exclusively-used table. USE DBF() SHARED is
*!* a good method for not having to remember the path to the table

oDS = NEWOBJECT("myPrivateDSClass", "MyDS.prg")    
*!* NEWOBJECT allows you to specify a class in a program or other 
*!* location without the use of SET CLASSLIB TO ... ADDITIVE.

oDS.UseTable("myDSTest")
GO BOTTOM
SET 

SET opens the Data Session window. If you switch between the default data session and MyPrivateDS, you see myDSTest open in both, with the record pointer at the top of one and the bottom of the other.


REFERENCES

For more information, see the Session Object documentation in the MSDN Visual FoxPro documentation, or "Additional Language Elements" in the Vfpsp3.chm file which ships with Visual Studio 6.0 Service Pack 3.

(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Garrett Fitzgerald, Microsoft Corporation.

Additional query words: KBDSE

Keywords : kbOOP kbVFp600 kbVS600sp3 kbGrpFox kbDSupport kbCodeSnippet
Version : WINDOWS:6.0,6.0 SP3
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: December 30, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.