1. Tracy Lamb
  2. PowerBuilder
  3. Friday, 21 January 2022 14:51 PM UTC

Hi all,

This is probably a rookie question, but I can't seem to re-set a variable-length array... 

Here's what I'm doing:

for ll_ThisRow = 1 to ll_RowCount
   li_doc = 1
   ... other stuff ...
   istr_documents[li_doc] = ls_filename
   li_doc++
   // If Include Attachment checked, add doc name to array
   if cbx_attachments.checked then
      ... other stuff ...
      li_doc++
   end if
  ls_mergeFiles = '"' + istr_documents[1] + '"'
  li_docCount = UpperBound(istr_documents)
  for li_doc = 2 to li_docCount
     ls_mergeFiles = ls_mergeFiles + ' "' + istr_documents[li_doc] +'"'
  next
  ... other stuff ...

  SetNull(istr_documents[])
next

Everything works fine the first time through the loop, but the second time through I get the following error:

Error Number 46: Null object reference cannot be be assigned or passed to a variable of this type.

The offending line is in bold above.

I'm thinking I'm resetting the array wrong?

TIA, ~~~Tracy

Accepted Answer
John Fauss Accepted Answer Pending Moderation
  1. Friday, 21 January 2022 16:11 PM UTC
  2. PowerBuilder
  3. # Permalink

You ask a good question, Tracy -

It may surprise you to learn that the SetNull() PowerScript function actually makes NO content changes to the memory allocated to a variable, array or object!

PB keeps track of the null/not null status of every variable, array and object separately from its contents (PB keeps track of what I like to think of as "descriptor blocks"). Note that an individual structure cannot be set to null, but an array of structures can. When the SetNull function runs, it turns the "Hey, I'm null!" indicator in the descriptor block on but makes no change to the memory occupied by the variable, array, or object. If there existed a "SetNotNull" PowerScript function, you'd be able to access the contents as if nothing had happened after using it.

What really makes the difference when resetting an unbounded array is having another, empty array, such as the istr_Null array in Chris' code snippet. Assigning an empty array to the "real" array is what actually resets the size/memory/bounds of the real array.

When coding a situation such as you have, I like to name the empty array "xxxx_empty_array[]" when it's declared, then the code to reset/clear the array becomes quite obvious. For example:

s_document lstr_empty_document_array[]

istr_documents = lstr_empty_document_array

Regards, John

Comment
  1. Miguel Leeuwe
  2. Friday, 21 January 2022 17:19 PM UTC
Exactly!
  1. Helpful
  1. Tracy Lamb
  2. Friday, 21 January 2022 17:32 PM UTC
Thank you so much for the solution and the education! Works like a charm!

~~~Tracy
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Friday, 21 January 2022 15:07 PM UTC
  2. PowerBuilder
  3. # 1

Hi Tracy;

  The correct coding would be ...

MyStructure  istr_documents[]

MyStructure  istr_null[]

// Your loop code here - at end of loop ....

istr_documents  = istr_null

HTH

Regards ... Chris

Comment
  1. Tracy Lamb
  2. Friday, 21 January 2022 17:35 PM UTC
Setting istr_documents = ls_nullarray IS the last line before NEXT. See correct answer above.

  1. Helpful
  1. Chris Pollach @Appeon
  2. Friday, 21 January 2022 19:05 PM UTC
It's the same as mine. ;-)
  1. Helpful
  1. John Fauss
  2. Friday, 21 January 2022 20:22 PM UTC
Yes, it is, so I called attention to that in my response, Chris. Setting the array of structures to Null is not absolutely necessary, however, so I wanted Tracy (and others) to understand that SetNull never clears/resets anything... it only prevents you from referencing the contents of the variable, array, or object.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.