{"id":1671,"date":"2024-08-29T11:00:14","date_gmt":"2024-08-29T17:00:14","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=1671"},"modified":"2024-08-29T11:00:14","modified_gmt":"2024-08-29T17:00:14","slug":"xojo-and-nil-arrays","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2024\/08\/29\/xojo-and-nil-arrays\/","title":{"rendered":"Xojo and nil arrays"},"content":{"rendered":"\n<p>Xojo can return arrays from method calls like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function foo() as String()\n<\/code><\/pre>\n\n\n\n<p>Now what happens if you leave such a function without a return statement ?<br>Xojo will happily return a NIL array &#8211; not one that has no members but one that IS NIL<\/p>\n\n\n\n<p>For  instance, if you create the function as <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function foo() as string()\nEnd Function<\/code><\/pre>\n\n\n\n<p>and then call it later like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim arr() as string = foo\nbreak<\/code><\/pre>\n\n\n\n<p>The debugger will show you that arr is NIL; not a valid array with no elements (some <a href=\"https:\/\/documentation.xojo.com\/getting_started\/using_the_xojo_language\/collections_of_data.html#arrays\">Xojo documentation conflates these two things<\/a> but they are NOT the same)<\/p>\n\n\n\n<p> If your code was<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim arr() as string = foo\narr.append \"123\"<\/code><\/pre>\n\n\n\n<p>You will get a NilObjectException (yes internally arrays are objects)<\/p>\n\n\n\n<p>So how DO you check which it is ?<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>use Is Nil to test if you got a NIL array<\/li>\n\n\n\n<li>use the count property if the array IS NOT NIL<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>dim arr() as string = foo\nif arr is nil then\n  \/\/ ok now how do I deal with this ?\n  \/\/ how can I make it so I can add items ?\nelse if arr.count = 0 then\n  \/\/ ok we got back an array with no items in it\n  \/\/ appending items will work !\nelse \n  \/\/ ok we got back an array with items in it !\n  \/\/ appending items will work !\nend if<\/code><\/pre>\n\n\n\n<p>Notice that if we get back a NIL we cant do much since there is NO array. Trying to append will raise an exception. So what can I do to make it NOT be a nil array ?<\/p>\n\n\n\n<p>At first you might try the Redim function to create an array like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Redim arr(-1)<\/code><\/pre>\n\n\n\n<p>in API 2 this is arr.ResizeTo like <code>arr.ResizeTo(-1)<\/code> which will also fail<\/p>\n\n\n\n<p>The right thing to do is use the Array FUNCTION to create one with some elements then resize it to 0 like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>arr = Array(\"\")\nredim arr(-1)\n<\/code><\/pre>\n\n\n\n<p>The resulting code then looks like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim arr() as string = foo\nif arr is nil then\n  \/\/ ok got a NIL array\n  \/\/ but thanks to norm we know how to fix this\n  arr = Array(\"\")\n  redim arr(-1)\nelse if arr.count = 0 then\n  \/\/ ok we got back an array with no items in it\n  \/\/ appending items will work !\nelse \n  \/\/ ok we got back an array with items in it !\n  \/\/ appending items will work !\nend if\n\n\/\/ and now here you can append items\n\/\/ regardless of what the function gave you back <\/code><\/pre>\n\n\n\n<p>Have a great day<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Xojo can return arrays from method calls like Now what happens if you leave such a function without a return statement ?Xojo will happily return a NIL array &#8211; not one that has no members but one that IS NIL For instance, if you create the function as and then call it later like The &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2024\/08\/29\/xojo-and-nil-arrays\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Xojo and nil arrays&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1671","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=1671"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1671\/revisions"}],"predecessor-version":[{"id":1672,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1671\/revisions\/1672"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=1671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=1671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=1671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}