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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public Arrays 1

Status
Not open for further replies.

bdfigler

Programmer
Oct 12, 2001
58
US
Hi. I am pulling some values from a .ini on load, and I want to put these values into an array that can be read by the rest of the project. But I guess you can't declare Public arrays in VB... Any thoughts on the best way to do this? Thanks as always. -Brad
 
declare the array in a module. That will give it a global scope.

jason
 
Dim myArray()
ReDim myArray(someVariable)
---
This works in the procedure (in a module) where I declare it, but it doesn't work in another procedure in the same module. What am I doing wrong? Does it have something to do with the 'ReDim' statement? -Brad
 
Declare the array in ther General Declarations section (i.e. outside of any procedure) of a .bas module. Use the Public keyword instead of Dim.
Also, when you use ReDim, use Preserve to keep your data from getting destroyed.
Example:
Code:
Option Explicit

Public MyArray() as MyDataType

public MySub()
' Do some stuff
ReDim Preserve MyArray(size)
' Do some more stuff
end sub
 
if you declare it inside of a procedure, the variable is local to that procedure. Declare the array in the general declarations section of the module. Redim inside procedures if needs be.

General_Declarations
Public myArray() As Integer


Private Sub DoThis()

ReDim myArray(someVar)

End Sub

Private Sub DoThat()

ReDim myArray(otherVar)

End Sub






 
For a more "OO" solution and to avoid using a global variable, declare the array as private in the form that it belongs to, and use property pairs to access the array from another form:
----------------------------------
'In Form1:
Private m_MyArr() As Integer

Property Get MyArr(Index As Integer) As Integer
MyArr = m_MyArr(Index)
End Property
Property Let MyArr(Index As Integer, NewVal As Integer)
m_MyArr(Index) = NewVal
End Property

'In Form2:
Dim Test As Integer
Test = Form1.MyArr(3)
--------------------------

If the array truly belongs to the application, not any particular form, then I would say it's best to assign it to the form that quits the app when unloaded.

Hope that helps!

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Can you declare arrays in the general declaration section? I'm pretty sure I got errors when I tried to do that.
 
Yes you can, but the array must be declared as private. That means that other forms can't access the array directly, but you can get around that with the property pairs.

Good luck!

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Thanks for the replies. I think I have a better grip on the problem now...

I am only using one form in this project, so I have the luxury of putting all the code in one form module.

The problem would be solved if I could declare the arrrays in the declarations section:

Option Explicit
Dim Array1(27) As Integer

But I can't do that because I don't know the size of the array until I have run some code. I tried the following:

Option Explicit
Dim Array1() As Integer

Sub AProcedure
intArraySize = textbox.text
ReDim Array1(intArraySize)
End Sub

Sub AnotherProcedure
textbox2.text = Array1(5)
End Sub

This didn't work because (I think) the scope of the 'redim' statement is limited to 'AProcedure'. In 'AnotherProcedure', 'Array1' is null.

So am I missing something simple? It seems like this should be a pretty easy thing to do. One solution is to load the ini from a splash screen. I would then know the size of Array1 when the second form is loaded. I guess that would be Mike's "OO" approach. Any thoughts on the best way to do this? Thanks. -Brad
 
Yes, you could initialize the array from a splash screen, or resize the array at the end of your ReadIni code
-------------------------------------------
'General Declarations
Private Array1() As Integer

Private Sub Form_Load()
ReadIniSettings
End Sub

Private Sub ReadIniSettings()
'Read from the file and initialize other stuff
ReDim Array1(WhateverNumber) As Integer
End Sub
-------------------------------------------

If the array is declared at the module level, any procedure within the form should be able to access it. In your example above, as long as AProcedure is called before AnotherProcedure is, the array should be resized and ready to use.

Does that help?

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Resized, yes. But the values I put into it are lost.
 
I populated the array _after_ the redim statement, so preserve wasn't necessary, but I did just realize that I made a silly mistake. Declaring at module level and then 'redim'ing at procedure level will work. Thanks for all the help. -Brad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top