Array Global across the desktop APP

Hello everyone,

I would like to share my doubt with you. I hope someone has the solution.

The result I want to get is this:
When opening the desktop app, a query on a Mysql database generates a multidimensional array (at least 12 columns with N rows).
And I would like to save it in a global property to use it later with other methods but basing my calculations always and only on the initial array created when opening.

This is because the initial query is a bit slow so I save it once and then calculate all the data.

How can I create this?

I state that I have already tried something but with little success.

Thank you all
Diego

Well, show us your code. Example project would be best.

1 Like

Create a Module in your project. In there, create a Property for your array. Make it Global.

Public Property Robots() As RobotClass

Here is my code:

IN Windows1> Events> open
I execute the methods that sti calls “SetMyArray” where you execute the initial query

Here is the code of the method:

Public Function SetMyArray() as String(,)
Dim sql as String, sqlcount as String, orderby as String, where as String
Dim arrayIMG(-1,-1) as String
Dim row As Integer
Dim rsc as RecordSet, numrec as integer, colrsc as Integer, dc as String, textpricedrop as String, textstockdrop as String

if DbConn then
try
sql = "SELECT DISTINCT id, code, brand, price, stock FROM products WHERE code is not null "
rsc = db.SQLSelect(sql)
row = 1
Redim arrayPRO(rsc.recordCount,rsc.fieldCount)
if rsc <> nil then
rsc.MoveFirst
colrsc = (rsc.FieldCount)
Do Until rsc.EOF

      For i as Integer =1 to colrsc
        arrayPRO(row,i) = rsc.IdxField(i).StringValue.DefineEncoding(Encodings.UTF8)
      Next
      row=row+1
      rsc.MoveNext
    Loop
    rsc.Close
  end
  rsc=nil
  db.Commit
  db.Close
  
Exception 
  msgbox dbLinky.ErrorMessage
end

else
MsgBox “Connessione al db fallita”
end if
End Function

Then below I declared the Property calling the method

You can see the screens if the methods and properties are set correctly. Maybe I’m wrong in the clarification of the returns?

Where is arrayPRO defined?

is inserted in the method.
Should I return and then declare it in the property?
What am I doing wrong?
Above all, does this process allow me to use an array throughout my desktop app and call it as many times as I need?

You should read the user guide and lear the basics about variables, in this case, the scope.

https://docs.xojo.com/UserGuide:Variables_and_Constants

I’m not sure what this means. In your screenshot of the MyArray property, you show some code in the comments portion of the window. That is a comment only, not code.

You should have a global property named arrayPRO of type String(,). Put it in a Module and set it’s scope to Global. That will make it accessible throughout your app.

Sorry Tim

but I don’t understand where I’m wrong.
As you said, I created a property called “arrayPRO ()”, with type String (,) default value empty and scope Public.

I created a method that returns a recordset from an sql query, with a loop for each row of this recordset except for the whole array “arrayPRO”.
When I run it has 2 errors

Syntax error on the property “arrayPRO ()”
And
Window1.SetMyArray, line 58
This array has fewer dimensions than you have provided

How do I declare the property as a multidimensional array?
is it right to declare it in the method and populate the arrayPRO right after the query?

Sorry, you’ve got the parens in the wrong place. Use

Name: arrayPRO(-1,-1)
Type: String
Scope: Global

The fact that you’ve got the scope set to Public makes me wonder where the property is. Is a property of the window? of App? or a Module?

Best to put it in a Module and set the scope to Global if you want it available everywhere in your app.

in Window1

Correct?

If you put it in Window1, it’s only available in Window1. If you have other windows, then it should go into a module. If you only have the one window, then that’s fine.

And yes, that is the correct settings.

Thank you Tim for your patience, very kind.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.