NetTalk Central

Author Topic: Variable scoping in NetTalk  (Read 3438 times)

Devan

  • Full Member
  • ***
  • Posts: 230
    • View Profile
    • Email
Variable scoping in NetTalk
« on: November 09, 2011, 04:57:02 PM »
Hi all,

Ok, I know this isn't STRICTLY a NetTalk programming issue, and is something probably out of 'Programming for Beginners' that I have forgotten about - but I have a question about variable scoping in a threaded app such as NetTalk.

Of, here is the situation:  Say I have a NetWebPage or something, that is responsible for manipulating data from several files (e.g. MyFile1 and MyFile2 that contain fields such as F1:FirstField, F1:SecondField, F2:AnotherField etc.)

Ok, my NetWebPage may do something like this:

Code: [Select]
Do OpenFiles
F1:ID = 100
Access:MyFile1.Fetch(F1:PRIMARY)
F2:ID = F1:LinkedID
Access:MyFile2.Fetch(F2:PRIMARY)
... other stuff ...
ReturnString = DoSomeFunction(F1:Action)
packet = packet & ReturnString
Do SendPacket
Do CloseFiles

Ok, the issue here is when I call the DoSomeFunction() function.  This function will basically look at the F1:Action parameter, and do some string building based on the action requested.

The thing is, DoSomeFunction() needs to do some manipulation of the contents of fields in MyFile1 and MyFile2, an example snippet within this function might be:

Code: [Select]
Case PassedAction
Of 1
    RetVal = 'Hello ' & F1:FirstName & ' ' & F1:LastName & ' how are you?'
Of 2
    RetVal = 'There are ' & Records(MyFile1) & ' records to process'
Of 3
    RetVal = F1:FirstName & ', you are responsible for ' & F2:StudentName & ' until ' & Format(F2:EndDate, @D17)
End
Return(RetVal)

Basically, my question is: Can DoSomeFunction() SEE the variable contents that are passed from the calling threaded procedure?  Bear in mind, this function can be called from several different procedures - even the SAME procedure on different threads if there are a lot of users on the site.

If it CAN see the contents of the globally declared files, how safe is this method?

Is there a better way to do this, considering that the function will have to work with the contents of about 20 different files to generate the returned string?

Thanks,
Devan

kevin plummer

  • Hero Member
  • *****
  • Posts: 1195
    • View Profile
    • Production Accounting and Software Payroll
Re: Variable scoping in NetTalk
« Reply #1 on: November 09, 2011, 08:18:13 PM »
Hi Devan,

I would be inclined to pass a group to your function with the data needed. That way you can be sure what your function gets is what you passed and is then consistent with what you get back.

Cheers,

Kevin

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Variable scoping in NetTalk
« Reply #2 on: November 09, 2011, 10:18:01 PM »
Hi Devan,

Assuming your files have the "Threaded" option ticked in the dictionary (which they definitely should have)

>> Can DoSomeFunction() SEE the variable contents that are passed from the calling threaded procedure? 

yes. Because file records are "global" in scope, the caller, and callee, (in this case the NetWebWhatever and SomeFunction are seeing the same buffer.)

>> Bear in mind, this function can be called from several different procedures -

no problem.

>> even the SAME procedure on different threads if there are a lot of users on the site.

no problem (since the record buffers are threaded.)

>> If it CAN see the contents of the globally declared files, how safe is this method?

very. (because the record buffers are threaded).

Of course the normal Async nature of NetTalk applies. a call into say a Form only runs a small bit of code, then bugs out. You can't do something say when the form loads, and expect that to still be in the record buffer when it saves. But you know this I think.

cheers
Bruce



Devan

  • Full Member
  • ***
  • Posts: 230
    • View Profile
    • Email
Re: Variable scoping in NetTalk
« Reply #3 on: November 09, 2011, 11:41:53 PM »
Thanks for the clarification Bruce!

Tsk! over 2 decades of programming in Clarion, and I'm still trying to get a grip on mixing threaded and non threaded procedures...

I did some basic testing of this today, and it supports what you are saying... Cool !!

Cheers,
Devan