Results 1 to 6 of 6

Thread: Passing arrays in and out of functions or subs

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Posts
    161

    Question

    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

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Posts
    161

    Arrow

    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...

  4. #4
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    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
    Mark
    -------------------

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Posts
    161

    Thumbs up

    That works fine... thanks !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width