-
What would be the best way to populate an array through a function or sub, without having to declare it as a global variable ?
Especially, I have run into trouble when I already have some data in the array and don't want to touch that part of it...
Any ideas ?
Thanks
-
Not sure what you meant, you can pass an array to a sub of any datatype, or you could pass it as a variant, but you can't pass arrays of UDT's as variant
Also use redim to change the size or your array, preserve will keep your old data..
Code:
SUb Yoursub (Array())
REdim preserve array(X)
End sub
-
Thanks... It's a little more complex than that... The array I am talking about holds Vriables of a User defined type. For example:
Code:
Type UserType1
Var1 As String
Var2 As Long
End Type
And my array holds variables of that type. Some of which already have data in 'Var1' and the sub will populate Var2...
The only solution to this might be global declarations, but I don't like using them...
-
Your signioture says you are using VB6
Arrays are no problem with VB 6
this works:
is this the sort of thing you want to do?
Code:
Option Explicit
Private Type UserType1
Var1 As String
Var2 As Long
End Type
Private Sub Command1_Click()
Dim MyVarArray(10) As UserType1
Dim i As Integer
FillVar2 MyVarArray()
FillVar1 MyVarArray()
For i = 0 To 10
Debug.Print MyVarArray(i).Var2
Debug.Print MyVarArray(i).Var1
Next i
End Sub
Private Sub FillVar2(varArray() As UserType1)
Dim i As Integer
For i = 0 To 10
varArray(i).Var2 = i
Next i
End Sub
Private Sub FillVar1(varArray() As UserType1)
Dim i As Integer
For i = 0 To 10
varArray(i).Var1 = "var1, element " & i
Next i
End Sub
-
That will also work with Vb5, it's just that you can't pass an array of UDT's as a variant, but i guess if FrancisC is happy with that, it's ok
Fox said that it should work with VB6, if you pass public variables in classes to public functions in a standard module, not sure though
-
That works fine... thanks !