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

Rediming a Variable array

Status
Not open for further replies.

Hawkeye123456

Programmer
May 5, 2000
24
0
0
US
Is there a way to redim a variable array and assign values to it in one sub, and then use the values in a diffrent sub?

This is my code below.
Private Sub Form_Click()

Dim Equ As Long
Dim temp As String

Dim Lines() As String

Dim c As Integer
Dim w As String
Dim U As Integer

SetB 'Sets up the B variable


'Debug.Print B Used for Testing

Open "C:\Program Files\Serv-U\serv-u.ini" For Binary As #1
Do While Not EOF(1)
ReDim Preserve Lines(1 To B) As String
Debug.Print UBound(Lines)
c = c + 1
Line Input #1, temp
Lines(c) = temp

'[GLOBAL] Format of the Desired input
'
If Left(temp, 1) = "[" Then
Equ = InStr(1, temp, "=")
If Equ = o Then GoTo blah
U = U + 1
ReDim Preserve Users(1 To U) As String
'turns [USER=Pub] into Pub
Users(1) = Mid(temp, Equ + 1, Len(temp) - Equ - 1)
lstUsers.AddItem Users(1)

'fso.CreateFolder
End If
blah:

Loop

Close #1

'Used for Debug Purpuses
'For r = 1 To B
' txtOpen = r & " : " & Lines(r) & vbNewLine & txtOpen
'Next


End Sub

Private Sub lstUsers_Click()
Dim a As Integer
For a = 1 To B
If lstUsers.Text = Mid(Lines(a), Equ + 1, Len(Lines(a)) - Equ - 1) Then
txtOpen = Lines(a) & vbNewLine & txtOpen
End If
Next
'lstUsers.Text
End Sub

--------------------------------------------------------
In a module
--------------------------------------------------------

Public Users() As String
Public Lines() As String
Public B As Integer

Sub SetB()
Open "C:\Program Files\Serv-U\serv-u.ini" For Binary As #1

Do While Not EOF(1)
B = B + 1 'Counting the lines in the File
Line Input #1, w
Loop
Close #1
End Sub [sig][/sig]
 
Here is a demo of a couple ways do what you are suggesting.
The code is setting behind a form with a listbox and command
button as the only controls.

Hope this helps.
-------------------
Code:
Option Explicit

Dim myArray() As String  'declare as an instance variable
'
Public Sub sub1()
  Dim i As Integer
  ReDim myArray(10)  'set the bounds
  For i = 0 To 9     'populate it with something
     myArray(i) = "element " & Str(i)
  Next
  readit myArray  'call the sub passing it the array
End Sub

Public Sub readit(a() As String)
  Dim i As Integer
  For i = LBound(a) To UBound(a)
    Debug.Print myArray(i)    'operates on the instance 
                              ' (form) variable

    List1.AddItem a(i)        ' uses the passed pointer
  Next
End Sub

Private Sub Command1_Click()
   sub1
End Sub
[sig][/sig]
 
That would sure, but I am not going to pass it to a diffrent sub. I am going call it from a diffrent sub. Like the code shows. [sig][/sig]
 
You're right, but the concept is the same. You need to declare your array in some place that will not go out of scope when you exit the routine that fills it. If you have a module with sub main() that starts your program, you can declare it there as a global, then redim any where you like. remember that arrays pretty much follow the rules of scope like any other variable. [sig][/sig]
 
Well thanks, I got it working last night. I was redimming it twice and the second one was killing all of the data. [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top