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

How do I declare a "public" array? 2

Status
Not open for further replies.

james0264

Programmer
Joined
Mar 1, 2005
Messages
52
Location
GB
I have been searching the forum for methods on creating an array that can be used throughout the program, once declared after the initial load. I found the thread thread222-264326 but his method has not been explained thoroughly, so please, if you may, explain how he created a "public" array?

I have the following code to retrieve the data from a text file that contains 20-odd lines of versions:
Code:
Dim VersionVariable() As String
ReDim VersionVariable(0)
Open "C:\Documents and Settings\James\My Documents\BackupCD\Programs\version.txt" For Input As #1
  Do Until EOF(1)
    Line Input #1, VersionVariable(UBound(VersionVariable))
    ReDim Preserve VersionVariable(UBound(VersionVariable) + 1)
  Loop
Close #1
I would like to make the array (VersionVariable) available to the whole program. Putting it within the many places had produced an error message: "Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public of object modules" or "Sub or Function not defined". Many thanks, James.
 
Try declaring it in the declaration section of a standard module, just below the Option <blah> statement(s). Use the keyword Public.

Roy-Vidar
 
Thanks for the post RoyVidar, but I have never used a module before. Could you give me step-by-step instructions on how to achieve your solution? Many thanks, James.
 
I don't program VB, so I don't know the menu structure, but in VBA one would hit the insert menu and chose "Module", which should add a standard module to the project.

Roy-Vidar
 
Sorry about the delay, but thaanks for the input.

RoyVidar: Thanks for contrubuting, i'll try that.

petermeachem:Ok, I'll try your suggestions.
 
I've just tried out the suggestions, but they don't seem to work.

RoyVidar: The method you've provided is causing a error message, "Invalid outside procedure".

petermeachem: I've just realised, what do you mean by Freefile? Many thanks again, James.

To anyone else, I will be willing to try out any furthur suggestions. Thanks for the rapid support!
 
The FreeFile issue...

Code:
Dim VersionVariable() As String
[red]dim iFile as Integer[/red]

ReDim VersionVariable(0)

[red]iFile = FreeFile[/red]
Open "C:\Documents and Settings\James\My Documents\BackupCD\Programs\version.txt" For Input As [red]#iFile[/red]
  Do Until EOF(1)
    Line Input [red]#iFile[/red], VersionVariable(UBound(VersionVariable))
    ReDim Preserve VersionVariable(UBound(VersionVariable) + 1)
  Loop
Close [red]#iFile[/red]

If you don't close the file properly, and try to open another file 'As #1', you will get an unexpected error. By using FreeFile, this will not happen.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
The declaration will go at the top of a standard module.

[tt]public VersionVariable() As String[/tt]

The code manipulating the array variable will need to be within a sub or function somewhere. That code does not need to be within the standard module, unless you need to call that too from the whole project.

I think also perhaps a read at faq222-2244 could be recommended. I believe some of the issues here could be found with samples in the documentation (help files).

Roy-Vidar
 
Start a new VB Project (Standard EXE).
Click Project -> Add Module

In the newly created module, put this...

Code:
Option Explicit

Public VersionVariable() As String

On the form, put 2 command buttons (with their default names). Then, add this code.

Code:
Option Explicit

Private Sub Command1_Click()
    
    Dim i As Long
    
    For i = 1 To 20
        ReDim Preserve VersionVariable(i - 1)
        VersionVariable(UBound(VersionVariable)) = "String " & CStr(i)
    Next
    

    
End Sub

Private Sub Command2_Click()
    
    Dim i As Long
    
    For i = LBound(VersionVariable) To UBound(VersionVariable)
        Debug.Print VersionVariable(i)
    Next

End Sub

Start the probject. Click Command 1 to load the array. Then, click the second button to use the array.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Wow, what a code! Many thanks guys, you've all helped me solved my problem. Have a star ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top