As implied in my last post, I’ve been involved in developing some JScripts recently. Infact, nearly half a dozen scripts that are fairly large. In these scripts I created classes to handle data, and in some instances these scripts would return an array of these objects.
Infact, one of these scripts returned an array of these through three different methods like so
private function returnAnArray() : PotatoItTestClass[]
– this created an issue if I returned null. I’m sure that if you’ve read the JScript specification from start to end you’d already know this but I didn’t 🙂
Consider this piece of code. I have a called call PotatoITTestClass, I have a function which returns an array of these objects.
import System; import System.Windows; import System.Windows.Controls; import MForms; package MForms.JScript { class ExceptionTest { public function Init(element: Object, args: Object, controller : Object, debug : Object) { try { var myResults : PotatoItTestClass[] = returnAnArray(); if((null != myResults) && (myResults.length >= 1)) { debug.WriteLine("Array count: " + myResults.length); } } catch(ex) { debug.WriteLine(ex); } } private function returnAnArray() : PotatoItTestClass[] { var result = new PotatoItTestClass[2]; result[0] = new PotatoItTestClass(); result[1] = new PotatoItTestClass(); return(result); } } class PotatoItTestClass { var gstrMessage : String = "This is a message"; } }
This is all well and good and works exactly as we expect when we return a non-null value.
If we make this little change so returnAnArray() returns a null as follows
import System; import System.Windows; import System.Windows.Controls; import MForms; package MForms.JScript { class ExceptionTest { public function Init(element: Object, args: Object, controller : Object, debug : Object) { try { var myResults : PotatoItTestClass[] = returnAnArray(); if((null != myResults) && (myResults.length >= 1)) { debug.WriteLine("Array count: " + myResults.length); } } catch(ex) { debug.WriteLine(ex); } } private function returnAnArray() : PotatoItTestClass[] { var result = new PotatoItTestClass[2]; result[0] = new PotatoItTestClass(); result[1] = new PotatoItTestClass(); // return null result = null; return(result); } } class PotatoItTestClass { var gstrMessage : String = "This is a message"; } }
Then we end up getting an error “Error: Unable to cast object of type ‘System.DBNull’ to type ‘MForms.JScript.PotatoItTestClass[]’.” – not very kewl at all. I’ve done this sort of thing heaps of times in C# even done it in VB.Net without issue but in JScript…well…
Thankfully the solution to the issue is pretty straight forward, we don’t specify the type that holds the results or the type of the method
var myResults : PotatoItTestClass[] = returnAnArray();
becomes
var myResults = returnAnArray();
and
private function returnAnArray() : PotatoItTestClass[]
becomes
private function returnAnArray()
The complete code looks like this.
import System; import System.Windows; import System.Windows.Controls; import MForms; package MForms.JScript { class ExceptionTest { public function Init(element: Object, args: Object, controller : Object, debug : Object) { try { var myResults = returnAnArray(); if((null != myResults) && (myResults.length >= 1)) { debug.WriteLine("Array count: " + myResults.length); } } catch(ex) { debug.WriteLine(ex); } } private function returnAnArray() { var result = new PotatoItTestClass[2]; result[0] = new PotatoItTestClass(); result[1] = new PotatoItTestClass(); result = null; return(result); } } class PotatoItTestClass { var gstrMessage : String = "This is a message"; } }