NetTalk Central

Author Topic: Global values  (Read 5782 times)

rupertvz

  • Sr. Member
  • ****
  • Posts: 323
    • View Profile
    • Email
Global values
« on: February 20, 2012, 12:58:45 AM »
Hi Guys,

From other discussions I gathered that it is not good practice to use global variables for global values.
I do have some values that should be available to all procedures (web and non-web).
Can I use global variables for these?

What are the do's and don'ts re global-variables?

Regards
Rupert


« Last Edit: February 20, 2012, 01:01:22 AM by rupertvz »

kevin plummer

  • Hero Member
  • *****
  • Posts: 1195
    • View Profile
    • Production Accounting and Software Payroll
Re: Global values
« Reply #1 on: February 20, 2012, 03:33:19 AM »
Global Variables are not Thread safe - so if the values don't change then they are fine to use.

There are alternatives like creating a table and storing the values against the sessionid/userlogin

Rene Simons

  • Hero Member
  • *****
  • Posts: 650
    • View Profile
Re: Global values
« Reply #2 on: February 20, 2012, 03:38:15 AM »
Hi Rupert,

As Kevin notes, global varables are practically a DON'T in NTWS.

I would use sessionvariables if I were you.
To create or assign a value to a sessionvar is done by:

 p_web.ssv('<yourVariableName>', <yourVariableValue>)

Where <yourVariableValue> (without the quotes) can be the name of the vaiable or a value. e.g.:

p_web.ssv('myCustomerCode',5) or p_web.ssv('myCustomerCode',CUS:Code)

retrieving the customer from the sessionvar code would then be:

 CUS:Code = p_web.gsv('myCustomerCode')

To make sure that p_web methods like ssv and gsv can be used in your non-web procedures you have to add prototype-  NetWebServerWorker p_web  -to the non-web procedures you use.

Maybe Bruce has some additional info on this.

Cheers,
Rene
« Last Edit: February 20, 2012, 03:41:52 AM by Rene Simons »
Rene Simons
NT14.14

rupertvz

  • Sr. Member
  • ****
  • Posts: 323
    • View Profile
    • Email
Re: Global values
« Reply #3 on: February 20, 2012, 03:47:33 AM »
Thanks Rene,

I had difficulty using the session-variables in my "non-web" procedures, but the prototype syntax will surely help :-)

Just to have clarity;
All non-web procedures called from/after a web-page, should have the value for the session variable when using the "prototype" syntax?

All non-web procedures called before a web-page, as for example the "Web-Server" procedure, will not have access to the session values?  As the value could vary from session-to-session?

Regards
Rupert

rupertvz

  • Sr. Member
  • ****
  • Posts: 323
    • View Profile
    • Email
Re: Global values
« Reply #4 on: February 20, 2012, 03:49:52 AM »
Thanks Kevin,

So global variables should be safe to use, if the value stays the same throughout my application?
All sessions and threads.
For example:  Company Name?

Rene Simons

  • Hero Member
  • *****
  • Posts: 650
    • View Profile
Re: Global values
« Reply #5 on: February 20, 2012, 05:14:18 AM »
Hi Rupert,

Session variables are specific for the session that creates them.
When you start two browsers on the same machine , they each will maintain their own session and thus their own set of sessionvars.
I will not be able to "see" your sessionvars.

In  the "Web-Server" procedure you don't use the p_web.ssv/gsv but self.ssv/gsv .

In answer to your question to Kevin about CompanyName:
According to the place you want to set it: p_web.ssv('CompanyName','My Company Ltd.') or
self.ssv('CompanyName','My Company Ltd.')

The names you pick for the session variables are not restricted to names in the dictionary. However, when you use dictionary-like names they might be easier to recall. ;)

Sessionvar names are just part of the key which is used to get the values from the sessionvalue-queue.

Cheers,
Rene


Rene Simons
NT14.14

rupertvz

  • Sr. Member
  • ****
  • Posts: 323
    • View Profile
    • Email
Re: Global values
« Reply #6 on: February 20, 2012, 01:40:43 PM »
Hi Rene,

>> To make sure that p_web methods like ssv and gsv can be used in your non-web
>> procedures you have to add prototype-  NetWebServerWorker p_web  -to the non-web
>> procedures you use.

I have added the "prototype" (NetWebServerWorker p_Web) to my non-web procedure.
I have added the same to Parameters line of the non-web procedure?

The non-procedure is called on a new thread START(proc,10000)
Do I have to add the parameter when calling the non-web procedure?



Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11244
    • View Profile
Re: Global values
« Reply #7 on: February 20, 2012, 10:25:12 PM »
>> I have added the "prototype" (NetWebServerWorker p_Web) to my non-web procedure.
>> The non-procedure is called on a new thread START(proc,10000)

these two statements conflict - since in the call (line 2) you are not passing the parameter.
But since you can only pass a string, not an object, to a new thread, you _can't_ pass the parameter here.

If you want to pass p_web, then you must call the procedure, not start it. This is ok to do, as long as you do it _after_ you have sent everything you're planning to send, back to the browser.

Calling
p_web.Replycomplete()
tells the browser "that's all", so is a good thing to call before calling your other procedure.

cheers
Bruce

rupertvz

  • Sr. Member
  • ****
  • Posts: 323
    • View Profile
    • Email
Re: Global values
« Reply #8 on: February 21, 2012, 12:00:42 AM »
Thanks Bruce,

It is getting a bit more trickier;

In a previous post, I asked about triggering or "calling" a procedure on the "server-side" and you advised me to START a new procedure in a new thread.  I am currently starting the new procedure from a form, after INSERT.

This actually worked fine :-)  But how do I get a value across to the new STARTED procedure?
If I cannot pass parameters during START?

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11244
    • View Profile
Re: Global values
« Reply #9 on: February 21, 2012, 02:06:25 AM »
you can pass up to 3 _string_ parameters to the new thread.

you can't pass p_web, because p_web is an object on this thread (and this thread is about to die.) If you need to call it with p_web, then you mustn't start a thread. You must wait until the whole reply has been formed and send, and then call your procedure.

If this is the case you'll need to call it from the WebHandler procedure, after the class has completed sending the reply.

Perhaps you don't need to pass it p_web, maybe there are just some values inside p_web that you can pass as strings?

Cheers
Bruce

kevin plummer

  • Hero Member
  • *****
  • Posts: 1195
    • View Profile
    • Production Accounting and Software Payroll
Re: Global values
« Reply #10 on: February 21, 2012, 02:10:30 PM »
1 of the parameters passed can also be a group.

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11244
    • View Profile
Re: Global values
« Reply #11 on: February 22, 2012, 12:14:49 AM »
yes, but passing the p_web would not work here, because the "host thread" of p_web is about to end, so the object will be disposed. Passing a pointer to the object to a thread, and then killing the original object would cause a gpf.

cheers
Bruce

kevin plummer

  • Hero Member
  • *****
  • Posts: 1195
    • View Profile
    • Production Accounting and Software Payroll
Re: Global values
« Reply #12 on: February 22, 2012, 03:37:02 AM »
yes so I load what I need from p_web into a group and then work with these values in the new thread and not p_web.