Namespaces [examples]

Declare a namespace to which all public memory variable will be attached while the namespace is active
NAMESPACE
procedure proc1
namespace namespace1
messagebox("Namespace is " + namespace())
public pub1 = "Public variable declared in proc1"
return
 
procedure proc2
namespace namespace2
messagebox("Namespace is " + namespace())
public pub1 = "Public variable declared in proc2"
return
 
proc1()
proc2()
display memory
? namespace1.pub1
? namespace2.pub1
namespace
messagebox("Namespace is " + namespace())
public pub1 = "Public variable, no namespace active"
? pub1
?

output

Namespaces:                                                                     
------------------------                                                        
NAMESPACE1 Dynamic array (refcnt 2)                                             
.PUB1       Character 'Public variable declared in proc1'                       
NAMESPACE2 Dynamic array (refcnt 2)                                             
.PUB1       Character 'Public variable declared in proc2'                       
                                                                                
Memory Variables:                                                               
------------------------                                                        
                                                                                
** Total of ** 4 variables defined and 298 bytes used.                          
                                                                                
Public variable declared in proc1                                               
Public variable declared in proc2                                               
Public variable, no namespace active

https://www.lianja.com/doc/index.php/NAMESPACE


Return the currently active namespace, if any
NAMESPACE

 

procedure proc1
namespace namespace1
messagebox("Namespace is " + namespace())
public pub1 = "Public variable declared in proc1"
return
 
procedure proc2
namespace namespace2
messagebox("Namespace is " + namespace())
public pub1 = "Public variable declared in proc2"
return
 
proc1()
proc2()
display memory
? namespace1.pub1
? namespace2.pub1
namespace
messagebox("Namespace is " + namespace())
public pub1 = "Public variable, no namespace active"
? pub1
?

output

Namespaces:                                                                     
------------------------                                                        
NAMESPACE1 Dynamic array (refcnt 2)                                             
.PUB1       Character 'Public variable declared in proc1'                       
NAMESPACE2 Dynamic array (refcnt 2)                                             
.PUB1       Character 'Public variable declared in proc2'                       
                                                                                
Memory Variables:                                                               
------------------------                                                        
                                                                                
** Total of ** 4 variables defined and 298 bytes used.                          
                                                                                
Public variable declared in proc1                                               
Public variable declared in proc2                                               
Public variable, no namespace active

When developing custom sections in the Visual FoxPro scripting language it is a good practice to encapsulate the custom section or gadget code into a unique namespace.

You declare a namespace using the namespace command. e.g.

namespace mycustomsection
public state
// updating the "state" variable in the namespace
state = 1

Whenever any events are dispatched to an object e.g. a click event for a Commandbutton, Lianja will automatically activate the namespace that the object belongs to. This prevents any name clashes across individual custom section or gadget code.

You can treat a namespace just like any other object variable and reference property elements in it. e.g

namespace mycustomsection
public state
define class mybutton as commandbutton
 proc click()
 if mycustomsection.state = 1
 // handle state 1
 endif
 endproc
enddefine

https://www.lianja.com/doc/index.php/Developing_Lianja_Custom_Sections_in_Visual_FoxPro


Advertisements

Namespace

Q:
my commandbutton is created using code in custom section
May I know what property I should set using code as I do not have an attributes panel for commanbutton created in code?
A:
In the top of your custom section, public the variable you are going to use for the button – under where it says namespace.
For example.

Code:
/
// Lianja custom section for page "page1" section "section2"
//
namespace gap
public tekbutton1
define class page1_section2 as section
.......
.......

Then anywhere in the code, you can just use
tekbutton1.hide
and of course to show it, you use tekbutton1.show.

tekbutton1.hide is the same thing as tekbutton1.visible = .f.
tekbutton1.show is the same thing as tekbutton1.visible = .t.



 

Namespace

Namespaces allow you to declare public variables, which are only public within that namespace. So, I could have 2 separate public variables called m_var, providing at least one of them was in a namespace. A procedure or function with the same namespace declared would be able to access the m_var from that namespace or it could be accessed as namespace.m_var from anywhere. It’s useful if there is a risk of having conflicting public variable names – for example, if you are adding Page or Section templates or Pages from the Page Library to your App.


by specifying the namespace in a delegate or procedure, variables do not need to be prefixed by the namespace.

Code:
proc myDelegate()
    namespace PeopleKiosk 
    Public myvar
    // peoplekiosk.myvar and myvar reference the same variable
endproc

Tip #1: you can use the namespace() function to save the current namespace and set it back later in your code.
Tip #2: To disable the namespace just specify

Code:
namespace none
// or
namespace

Tip #3: how to inspect a namespace? Try this in the console.

Code:
namespace barry
public myvar = "hello world"
? barry

Surprise surprise, namespaces are just objects that encapsulate public variables and other things (more on that later).


Q:
I have a desktop app and I wish to make variable initialised in one page visible to all other pages
In my case, my initial page is a page with custom section
I have public some variables and initialised the values of these public variables but I am not able to see the variables value in other pages
May I know how I can expose the variables created in my custom section of my initial page to all other pages
A:
You probably have a namespace statement in your custom section code. That being the case you need to prefix the reference of your public variables by the name of the namespace e.g.

namespace myns
Public ui_message

in another page you must reference it like this
myns.ui_message