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!

Dynamic arrays

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
GB
I have a dynameic array: Array() which is public and available to everything.

I ReDim this in a sub and read values into it.

I thne try and read the same values from the Array in another sub but I cant seem to be able to do it. No error messages it just doesn't happen. I seems to get blank lines out of the array instead of what I KNOW is in there!

Is what I'm trying to do not possible (VB5)????

elziko
 
Did you declare it as public in a module? I declared PUBLIC in a module, redimed in a sub on form1 and loaded values into the array and can access values in form2.
David Paulson


 
Yeah I declared it just after "Option Explicit" but I cant read values written in another sub. I guess its something I've done but I cant work it out!

elziko
 
elziko,

I think David is trying to say it NEEDS to be public in a general module, not in a forms module. The code in a forms Module is sort of like code in a class module. Public - here - means public to the "Form". These declarations need to be in a module-module, not a forms-module.



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
I think you are missing the point - if the array is not public and available in the second routine, the project will not compile.

elziko, are you assiging values into the array and then doing ReDim??

If you are, if you don't use the keyword Preserve the contents of the array will be wiped.

So, if you do:

Dim arrData() As Long
ReDim arrData(4)
arrData(0) = 1
arrData(1) = 2
arrData(2) = 3
arrData(3) = 4
arrData(4) = 5

MsgBox arrData(3)

ReDim arrData(4)

MsgBox arrData(3)


the first message box will say "4", but the second message box will say "0" - data is lost on the ReDim statement.

However, if you do:

Dim arrData() As Long
ReDim arrData(4)
arrData(0) = 1
arrData(1) = 2
arrData(2) = 3
arrData(3) = 4
arrData(4) = 5

MsgBox arrData(3)

ReDim
Preserve arrData(4)

MsgBox arrData(3)


the first message box will say "4" AND the second message box will say "4" - data is preserved on the ReDim statement.

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top