I’m developing inn 2.2, but with 3.0 in mind (for some reasons I cannot move the development to 3.0 yet).
To make things work properly in 2.2, I want to test if a certain function exists and based on the result of the evaluation, take the appropriate action.
In this case it’s the .setValue() function of a JSDataSet. I’m trying to test with the following code:
if (!dataset.setValue)
{
//must me 2.2
}
else
{
//must be 3.0
}
But, unfortunally, this doesn’t work. The “if (!dataset.setValue)” part causes an error. The only errormessage given is:
-2
In the console I get the following error:
java.lang.ArrayIndexOutOfBoundsException: -2
java.lang.ArrayIndexOutOfBoundsException: -2
at org.mozilla.javascript.IdScriptable.get(IdScriptable.java:99)
at com.servoy.j2db.dataprocessing.JSDataSet.get(Unknown Source)
at org.mozilla.javascript.ScriptRuntime.getProp(ScriptRuntime.java:726)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:1904)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:94)
at com.servoy.j2db.scripting.e.call(Unknown Source)
at org.mozilla.javascript.ScriptRuntime.call(ScriptRuntime.java:1254)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2031)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:94)
at com.servoy.j2db.scripting.e.call(Unknown Source)
at org.mozilla.javascript.ScriptRuntime.call(ScriptRuntime.java:1254)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2031)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:94)
at com.servoy.j2db.scripting.e.call(Unknown Source)
at com.servoy.j2db.develop.debugger.k.a(Unknown Source)
at com.servoy.j2db.develop.debugger.k.executeFunction(Unknown Source)
at com.servoy.j2db.FormPanel.a(Unknown Source)
at com.servoy.j2db.FormPanel.a(Unknown Source)
at com.servoy.j2db.FormPanel$b.actionPerformed(Unknown Source)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
If I check dataset.setValue6512354716204376, I get the same error, and I think that really doesn’t exist.
I might be able to find another, first level function that was also added at the same time, to determine the version of Servoy approximatly, but that is not 100% safe and might work in this occassion, not in others.
Also, it soudns like a workaround… And I don’t like those…
Hi Scott, that is exactly what I was hinting at but I didn’t know the name by heart. However, if I understand paul correct he doesn’t want to use it because it is a workaround (like his first suggestion btw)…
Coding methods is a work around in itself. The developer’s environment is giving us a tool to work around the fact that the methods we want aren’t in the standard installation.
What about creating a method for every function that needs to be checked in application for v2 vs v3. Using the dataset.setValue example here, create a method called datasetSetValue that accounts for the different programming required and call that everywhere in the solution instead of dataset.setValue?
Then when you move to 3.x permanently you can either change the method or do find/replace in the code. Even if there were a way to test for function existence, it wouldn’t remove the need for the workaround because each function would require a different “else” code.
Testing for a method no longer seems to work in 3.5.5
if (forms[globals.temp_form].edit_return) {
forms[globals.temp_form].edit_return();
}
While testing a new portion of our application, this no longer works.
First, see if the method exists…then, if true, execute the method.
If the method does exist, all works fine…
If not, then this error shows up:
ConversionError: The undefined value has no properties calling edit_return.
Is there a different way to check if a method exists before executing it?
The above works for the if exists condition, but throws an error on the false (method does not exist) condition.
The situation: A global method that on returning, executes a standard method on each form if it exists - ie, form.originalform.edit_return(). This keeps the logic in a global, with form-specific methods picking up anything leftover if needed.
Haven’t noticed any problems with it in 3.5.5 or 3.5.6.
I test that the form is valid first. It looks to me from the error like you’re trying to use the test on a form that doesn’t exist (the “undefined” part).
The form does exist, I can reference elements and methods that exist fine on it.
The only issue is the error that appears using the if(method) syntax. If the method does not exist, Servoy/Java throws the conversion error.
Your typeof method looks good.
I just wanted to comment that an “official” way to test for a method doesn’t exist, and the one mentioned in this thread earlier, doesn’t work (under certain circumstances?).
Are you sure about the form existing and the form name being in the global like you want?
I’ve just tested this in 3.5.6/Win/Java6, using your code, you get the “ConversionError” only if the form you are trying to access doesn’t exist…
The condition evaluates to true in the cases were the form exists and the method exists. It evaluated to false if the form exists but the method does not, and it errors out if the form does not exist.
You could prevent that in your code by doing this:
if (forms[globals.temp_form] && forms[globals.temp_form].edit_return) {
forms[globals.temp_form].edit_return();
}
Note that in my form test, I added this line at the beginning:
eval('forms.'+arguments[0]);
This is because when running in the client, the form may not be loaded and when accessed dynamically (ie, via array notation) will not be loaded automatically, to my understanding.