>I created global object g_server in default data session based on custom class.
>
>I call this object methods from prg files.
>I want that methods are executed in the caller datasession.
>However, FoxPro switches datasession to the session where g_server is created on call.
>
>I cannot pass datasession id as parameter since it is difficult to determine the datasession id (prg nesting is deep, this requires major re-write).
>
>Using
>
>SET DATASESSION TO (thisform.DataSessionId)
>
>causes error object is not contianed in form
>
>Using
>
>SET DATASESSION TO (_screen.activeform.DataSessionId)
>
>causes sometimes error _screen.activeform is not an object
>
>
>How to disable datasession switching when calling global object method ?
Only two ways about it:
- create a private instance of this object, as a member of your form. As long as it's created within the form, it will share the form's datasession ID.
- pass the current datasession ID as a parameter, or pass the form itself. This technique is commonly used in various frameworks, specially those that handle SPT.
Any object lives in the datasession in which it was created, i.e. its code switches the datasession when it executes. For global objects, this is DS 1. Since your form usually has a DS of 2, 3 etc - either pass the form or the DS ID.
Note: _screen.activeform may be null if the focus is somewhere else - you may be executing toolbar code, menu code, report code, OKL code (didn't really play much with these), or are between a deactivate of one thing and the activate of the next. That's why I either make sure the object that needs to be addressed has passed itself as a parameter, or that I really check whether _screen.activeform is null. Can't trust it, really.