• Welcome to PlanetSquires Forums.
 

Dim/Redim

Started by Petrus Vorster, June 04, 2023, 03:45:08 PM

Previous topic - Next topic

Petrus Vorster

Hi All

I am trying to create a shared Dynamic Array in Freebasic.
It seems a bit different than Powerbasic.

In PB I used to do this:

Global MYlabelsarray()As String
This was declared outside of a procedure, then in a function I would assign values:

Dim MYlabelsarray (1 To myqty&)As String

I tried using COMMON shared MYlabelsarray() as string and redim it inside a procedure.

This works fine between all functions for e.g. Form1, but how do I make it accessable over the entire project?
If i use the same request e.g. print myarray(1) in form2 etc, it tells me the variable has not been declared.

How would you do this in Freebasic?

-Regards, Peter
-Regards
Peter

Paul Squires

Here is how you can do it:


dim shared MYlabelsarray() as string


function myfunction1() as long
   dim as long myqty = 100
   redim MYlabelsarray (1 To myqty) As String

   ' or use redim preserve to not destroy existing data in the array
   ' redim preserve MYlabelsarray (1 To myqty) As String
  
   for i as long = 1 to myqty
      MYlabelsarray(i) = "Label " & i
   next

   function = 0
end function


function myfunction2() as long
   ' Display the global array strings
   for i as long = lbound(MYlabelsarray) to ubound(MYlabelsarray)
      print MYlabelsarray(i)
   next

   function = 0
end function


myfunction1
myfunction2

print
print "Press any key to exit..."
sleep



Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Petrus Vorster

Hi Paul

Thank you, that was what i did eventually.
It works fine in all the procedures under form1, but as soon as i try and refer to the array for procedures on another form, I am getting that "variable not declared".

So i just moved all the functions that would use the Array to form1. -Problem solved.

I wonder if it doesn't have something to do with the fact that i am using child forms on a tab-control?

But, problem solved.

Thanks a million.

-Regards Peter
-Regards
Peter