Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public Array disappears

Status
Not open for further replies.

k2a

Programmer
Jun 26, 2012
133
DE
On my form there are command buttons to create a public array from an Excel table and another button to scan that array for any interesting pattern. Scanning that array works fine at the first time. However, when closing the form and start it again to create a fresh array and enter the scan mode the new public array disappeared. This happens alternatingly between success and failure. In the exit button there are only two commands RELEASE Thisform and CLOSE TABLES.

The reason to use a public array is because it takes a few minutes to create it, for the Excel tables I use are quite large.

In the creation button is code like this:
PUBLIC gaAPOBank[1,1]
loExcel = CREATEOBJECT("excel.application")
loBook = loExcel.Workbooks.Open(lcSourceFile)
loExcel.Visible = .T.
lnUsedRows = loBook.ActiveSheet.UsedRange.Rows.Count
lnUsedCol = loBook.ActiveSheet.UsedRange.Columns.Count
DIMENSION gaAPOBank(lnExRow,lnArrayCol)

Then fill the array ..

The code at the end looks now like this:
loBook.Close(.F.)
loExcel.Quit()
thisform.cmdSearch.Enabled = .T. (<<< here sits a breakpoint)

In the Debugger I can see that the array is filled with data each time. In the click method of the cmdSearch button the first command line is:
lnBankRows = ALEN(gaAPOBank,1) (<<< that is the place to check for success or failure)

So that means between my break and the click on the cmdSearch button, the public array got lost. This mysterious problem really annoys me, for I wasted a lot of time to find out what is going on here, without success.

However, in the meantime I developed a workaround wherewith I can live with. Moving the array into a from property, then this array never disappears. Therefore I create a form array and a public array at the same time. After entering the search mode and find that the public array disappears, then it can be created again.

Well, I leave up to the experts on this side to add their comments. May be someone might have a bright idea to uncover this strange behavior?

Regards, Klaus
 
Have you searched your project with CodeReferences Tool, eg for gaAPIBank. Maybe you find a RELEASE ALL or RELEASE ALL LIKE g* or anything not realated to gaAPOBank directly. The array could be released anywhere.

What's OK is the declaration and redimensioning, DIMENSION does not change the variable type from public to private, I checked that out.

As public variables are to be avoided anyway, your solution to put it as a form array is a good solution anyway.

You could already start with a form array. Simply add a property of the form in the property dialog with a name APOBank[1,1] and you have added an array property and can redimension it by DIMENSION THISFORM.APOBank(lnExRow,lnArrayCol), you never need a public array this way. Rather adapt all code using gaAPOBank[x,y] to Thisform.APOBank[x,y] and you've got rid of that public variable.

Bye, olaf.
 
Olaf said:
DIMENSION does not change the variable type from public to private, I checked that out.

That's right. It's the first thing I checked as well.

Klaus, I don't know whether this suggestion will be of any help:

1. Keep the array in a form property, as per your current solution. When the user launches the form for the first time in the session, populate the array from Excel as usual.

2. But, when the user goes to close the form, don't actually close it. Just just make it invisible.

3. The next time the user goes to launch the form (in the same session), make it visible again.

That way, you won't have the time-consuming task of re-populating the array each time.

I realise that this might be difficult, especially if you are doing other stuff in the form's Init that must be done every time the user opens the form. But at least consider it.

I do this sort of thing sometimes in my own projects. Not only does the form in question open instantly, but it also saves the user some work because all the settings and entries they made last time are still there.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
If the form isn't relevant to the task at hand, why not just create an object with an array property? This would presumably be the beginning of building an application object.

Declare the object private in the main program and it'll last for the life of your application.

Tamar
 
Oh, one other thought. To figure out what's going on, put:

TYPE(gaAPOBank[1])

in the Watch window and set a breakpoint on it. You'll find out when it disappears.

Tamar
 
Thank you all for your knowledgeable contribution.

Now, I have good news and bad news.

The good news is, that the strange behavior of the alternate disappearing of my public array simply vaporized. But the bad news is, I really have no clue what finally fixed it.
I made a lot of changes stimulated by what you suggested, but did not keep track of all those modifications and enhancement made. So what I have now is the old version which still fails and a faultless new version.

Finally my array sits now in a form property and also create a public array but made it selectable when using it in the development stage.

Thank again for all your help.

Regards, Klaus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top