How to create a PRIVATE variable owned by the caller?

How to create a PRIVATE variable owned by the caller?

Postby Enrico Maria Giordano » Tue Jan 10, 2023 2:19 pm

This is a not working sample. Any ideas?

Code: Select all  Expand view
#include "Fivewin.ch"


FUNCTION MAIN()

    CREATEVAR( "TEST", "Hello" )

    ? M -> TEST // it does not exist here

    RETURN NIL


STATIC FUNCTION CREATEVAR( cName, xValue )

    // How to create a PRIVATE variable owned by the caller?

    __MVPRIVATE( cName )

    __MVPUT( cName, xValue )

    ? M -> TEST

    RETURN NIL
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: How to create a PRIVATE variable owned by the caller?

Postby hmpaquito » Tue Jan 10, 2023 3:54 pm

Hola,

Por definicion una variable PRIVATE tiene ambito en la funcion que la crea y en las funciones que son llamadas por la funcion que la crea.

En el caso que vd. expone es normal que no funcione.

Use en su caso PUBLIC

Salu2
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: How to create a PRIVATE variable owned by the caller?

Postby Enrico Maria Giordano » Tue Jan 10, 2023 4:17 pm

No, it is possible. Please look at RESTORE FROM command.
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: How to create a PRIVATE variable owned by the caller?

Postby hmpaquito » Tue Jan 10, 2023 6:42 pm

PRIVATE
Create and initialize private memory variables and arrays

Syntax

PRIVATE <identifier> [[:= <initializer>], ... ]

Arguments

<identifier> is the name of a private variable or array to create.
If the <identifier> is followed by square brackets ([ ]), an array is
created and assigned to the <identifier>. When the <identifier>
specification indicates an array, the syntax for specifying the number
of elements for each dimension can be array[<nElements>,
<nElements2>,...] or array[<nElements>][<nElements2>]... The maximum
number of elements per dimension is 4096. The maximum number of
dimensions is limited only by available memory.

<initializer> is the optional assignment of a value to a new private
variable. An array cannot be given values with an <initializer>. An
<initializer> for a private variable consists of the inline assignment
operator (:=) followed by any valid CA-Clipper expression including a
literal array. If no explicit <initializer> is specified, the variable
is initialized to NIL. In the case of an array, each element is
initialized to NIL.

You can create and, optionally, initialize a list of variables and
arrays with one PRIVATE statement if the definitions are separated by
commas.

Description

The PRIVATE statement creates variables and arrays visible within the
current and invoked procedures or user-defined functions
. This class of
variable is said to have dynamic scope. Private variables exist for the
duration of the active procedure or until explicitly released with CLEAR
ALL, CLEAR MEMORY, or RELEASE. When a private variable or array is
created, existing and visible private and public variables of the same
name are hidden until the current procedure or user-defined function
terminates.
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm


Re: How to create a PRIVATE variable owned by the caller?

Postby Enrico Maria Giordano » Tue Jan 10, 2023 8:25 pm

Please look at this sample:

Code: Select all  Expand view
#include "Fivewin.ch"


FUNCTION MAIN()

    CREATEMEM( "MYTEST.MEM", "TEST", "Hello" )

//    ? M -> TEST // it does not exist here

    __MVRESTORE( "MYTEST.MEM", .T. )

    ? M -> TEST // now it exists

    RETURN NIL


STATIC FUNCTION CREATEMEM( cMem, cVar, xVal )

    PRIVATE &cVar := xVal

    SAVE TO ( cMem )

    RETURN NIL
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: How to create a PRIVATE variable owned by the caller?

Postby hmpaquito » Wed Jan 11, 2023 7:59 am

CreateMem() is not neccesary for private declaration on top main function:

Code: Select all  Expand view
#include "Fivewin.ch"


FUNCTION MAIN()

// Note !!!!!! Try without that function call
/*    CREATEMEM( "MYTEST.MEM", "TEST", "Hello" )  
*/


//    ? M -> TEST // it does not exist here
   
    // Here implicit PUBLIC vars declaration
    __MVRESTORE( "MYTEST.MEM", .T. )

    ? M -> TEST // now it exists

    RETURN NIL


STATIC FUNCTION CREATEMEM( cMem, cVar, xVal )

    PRIVATE &cVar := xVal

    SAVE TO ( cMem )

    RETURN NIL
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: How to create a PRIVATE variable owned by the caller?

Postby Enrico Maria Giordano » Wed Jan 11, 2023 8:38 am

Code: Select all  Expand view
Error description: (DOS Error 2) BASE/2005  Open error: MYTEST.MEM
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: How to create a PRIVATE variable owned by the caller?

Postby hmpaquito » Wed Jan 11, 2023 11:10 am

Try it with your mytest.mem
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm


Re: How to create a PRIVATE variable owned by the caller?

Postby Antonio Linares » Wed Jan 11, 2023 11:41 am

Dear Enrico,

Code: Select all  Expand view
#include "Fivewin.ch"

FUNCTION MAIN()

    CREATEVAR( "TEST", "Hello" )

    ? M -> TEST // it exists here

    RETURN NIL


STATIC FUNCTION CREATEVAR( cName, xValue )

    __MVPRIVATE( cName )
    __MVPUT( cName, xValue )
    __MVSETBASE()

    ? M -> TEST

    RETURN NIL
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: How to create a PRIVATE variable owned by the caller?

Postby Enrico Maria Giordano » Wed Jan 11, 2023 11:49 am

Great! Is there a similar function (__MVSETBASE()) available for xHarbour?
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: How to create a PRIVATE variable owned by the caller?

Postby Antonio Linares » Wed Jan 11, 2023 12:04 pm

I am afraid no as xHarbour does not have the member stackstate in the struct hb_struSymbol :-(

Maybe there is another way to do it, but I don't know it
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Re: How to create a PRIVATE variable owned by the caller?

Postby karinha » Wed Jan 11, 2023 2:59 pm

C:\HBBCC74\src\vm

03/04/2020 19:58 52.717 memvars.c

Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7826
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 110 guests