in operator

(Scenario: I have a global function that shows a list in dialog. The user can filter or scroll to the record s/he wants, select it, and have the form refresh to that record. I plan to create a layout for each relevant table, with a strict and predictable naming convention, but if the layout hasn’t been created for whatever reason, I don’t want to subject the user to a whole list of error messages. This worked beautifully yesterday, when I’d been looking at the forms in question, but then not today, when I’d just opened the solution.)

I’ve noticed (after a dramatic panic attack and personal meltdown) that using ‘in’ to determine whether a form exists only seems to work right if the form has been loaded on screen first. Forms that haven’t been loaded always seem to return ‘false’.

F’rinstance, if I have a form called companies and I’ve seen that form in Ready mode since I opened,

'companies' in forms
``` will return 'true', while ```
'NotAForm' in forms
``` will return 'false'. However, if I haven't loaded the companies form since I opened (or since I was last in Design mode), both statements return 'false'.

Question 1: Is there an easy way to 'preload' all the forms, so that I can use this 'look before you leap' method? I could loop through and populate an array, but it seems more straightforward to refer to the actual objects in the SOM.

Question 2: Are there similar caveats for using 'in' with Servoy objects? Will, for instance, Servoy recognize an occurrence of a method in a form object if the method hasn't yet been run? How about elements? Etc.
  1. Servoy has a recent used list of forms which can hold about 30 forms which it keeps in memory, so forms can get loaded when used and unloaded when not used lately. If you access a form with scripting or via window menu it gets loaded if not done yet.

  2. Many things are dynamic loading or lazy loading in Servoy to keep memory use acceptable. Best thing to do is to use “in” only on your own arrays/objects.

  1. Which objects count as “your own”?

  2. Is there any straightforward way to load the form names into an array at startup?

  3. Do the names of methods, elements and dataproviders load when the relevant form loads?

I appreciate the overhead saved by not loading all the forms. I just need to see that the objects exist, though (by name). Perhaps there’s a way that I can load just the names, into an array or something?

Scenario:

I really have high hopes for this function. It allows me to check whether a form, method, etc. is there before continuing. It’s sort of “look before you leap”. The way I would use it is similar to the reasoning behind “cascading” in cascading style sheets. Methods could check for exception methods or specific forms, and branch accordingly.

This would allow me, for instance, to check whether there’s a form called ‘ocala_branch_customers’. If there is, I could use that one for the ocala branch, but otherwise use the standard ‘customers’ form.

For methods, this allows me to always trigger a global method. That method then checks to see if there’s an “exception” method at the form level, in which case it can perform that one instead.

  1. arrays you made yourselfs
  2. no, currently you only can define an hardcoded array with your form names (if developers think this is useful we could add such a method to retrieve all form names)
  3. yes

Thanks, Jan! That clears it up.

I’ll hard code for now. I’ll be interested to see if anyone else would benefit from this, though.

I’m especially glad to see that methods, etc. load with the form. At least once I’m on the form, I can look within it without any special coding. :)

about point 3 we might misunderstood each other, if you loop through(not sure how you whould do that) the methods will get loaded and be compiled! (which might result in more memory use as normally needed)

Hmmm.

I’m not sure if we’re on the same page or not. :?

It appears that once I’ve loaded a form, I can use something like

if ("actRecNew" in forms[currentcontroller.getName()]) 

If “actRecNew” is a method on the current form, this returns true, else false.

I don’t need to ‘compile’ the methods, just check whether they’re there or not. Here’s the full code of the global method “g_actRecNew”, which I call on the onNewRecordCmd of all my forms:

// Run the record validation sub-routine
globals.g_subRecValidate();
// if there aren't any validation errors (or if there wasn't a Validate method), proceed.
if ( globals.error != 1 )
{
	//if the current form has its own specific method, run that one.
	if ( "actRecNew" in forms [currentcontroller.getName () ] ) //****Insert form method name
	{
		forms [currentcontroller.getName () ].actRecNew (); //****Insert form method name
	}
	//otherwise, just follow the default step(s) below.
	else
	{
			currentcontroller.newRecord();
	}
}

So far, this seems pretty reliable. Is there reason to believe that the ‘in’ operator, in this context, could return an incorrect ‘false’?

Cain:
…I don’t need to ‘compile’ the methods…

correct but looking for a method will get it loaded and compiled internally, this is no problem other than more mem. is used (if never called)

IC. That’s a good point to remember. The way I’m using it though, is that if it’s there, I call it. If not, I don’t.