Results 1 to 29 of 29

Thread: Function returning Array

  1. #1

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    Function returning Array

    I dont think that is posible..

    Is there a work around?

    Seahag

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Why not a disconnected / virtual ADO or DAO recordset ?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    Hmmmm

    Idea was to load from recordset into array .. than manipulate
    as I desire.
    Problem is I want to use class mods to handle all the calculating, and all the info returned is in groups of 6

    ??

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    I've not used them really, but what about a collection object, would that maybe be better ?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    I don`t know.

    I think I am getting what your saying.
    Use an object instead of Array>?

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    The way a couple of the guys explained it to me - that the collection was similar to an array, although the collection is, yes, an object so it'd take more memory up.

    You can't return an array from a function, but thinking about it you could just use a sub & declare the array outside of it (private or public) :
    Code:
    Option Explicit
    Dim aryNumbers() As Integer
    
    Private Sub Form_Load()
        Dim i As Integer
        
        For i = 0 To 11
            ReDim Preserve aryNumbers(i)
            aryNumbers(i) = i
        Next i
    End Sub
    
    Private Sub Command1_Click()
        Dim i As Integer, strTemp As String
        
        For i = 0 To UBound(aryNumbers) - 1
            strTemp = strTemp & aryNumbers(i) & vbCrLf
        Next i
        
        MsgBox strTemp
    End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    I guess

    I like your record set idea better. Makes more sense.
    (I hate Variables.)
    Thanks

    Seahag

  8. #8
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Halifax,UK
    Posts
    274
    you can return and array....

    [Highlight=VB]
    Private Sub mySub()
    Dim x

    x = myFunction

    msgbox x(0)
    msgbox x(1)
    msgbox x(2)
    msgbox x(3)
    End Sub
    '---------------------------------------------------------------------------

    Private Function myFunction()
    Dim y() As String

    ReDim y(3)
    y(0) = "One"
    y(1) = "Two"
    y(2) = "Three"
    y(3) = "Four"

    myFunction = y

    End Function
    VB6 VS2005

  9. #9
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    True, if you left it as a variant, then you could.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  10. #10
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489
    Why don't you send it as a paramter ByRef

    Private Sub ChangeArrayData(ByRef MyArray as String())
    'The code
    End Sub

  11. #11

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    Neet It worked

    Don't know why it work?
    But thanks..


    Whats the ByRef thing do?

  12. #12
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489
    Not positive about the specifics but it means
    By Reference as opposed to By Value.
    It basically make a variable local to the function.
    It's really useful with controls.

  13. #13
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Yep, well done there jcfowl, I didn't even think of that one!
    This is your best option seahag. There are 2 ways of passing a variable.
    1) ByRef or By Reference passes in the actual value, so you can manipulate the source straight away (like you're doing)

    2) ByVal or By Value is a copy of the variable & has a fixed value which you cannot alter (bit like a constant).

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  14. #14

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    OK

    Bit confused.. could you show an example
    using a Function? Pls

    Thanks

  15. #15
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489
    VB Code:
    1. Private Function ChangeData(p_MyVar as String) As String
    2. ChangeData = p_MyVar & "I changed it"
    3. End Function
    4.  
    5. Private Sub ChangeDataByRef(ByRef p_MyVar as String)
    6. p_MyVar = p_MyVar & "I changed it"
    7. End Sub
    8.  
    9. Private Sub Main()
    10.  
    11. Dim MyVariable, MyVariable2 as String
    12. MyVariable = "Hello "
    13. MyVaribale2 = "Hello "
    14.  
    15. MyVariable = ChangeData(MyVariable)
    16. ChangeDataByRef(MyVariable2)
    17.  
    18. End Sub

    Both MyVariable and MyVarible2 will equal the same thing.
    "Hello I changed it"

  16. #16
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Beat me to it - sorry SeaHag, had a long lunch there (hic)!
    Private Sub Form_Load()
    'set up variables
    Dim x As String, y As String
    x = "Hello World"
    y = "Hello World"

    'show the altered values returned from the function
    MsgBox myfunction(x, y), , "Function values"

    'now show the original values again
    MsgBox "ByVal x variable = " & x & vbCrLf & vbCrLf & _
    "ByRef y variable = " & y, , "Original Values"

    Unload Me
    Set Form1 = Nothing
    End Sub

    Private Function myfunction(Optional ByVal x As String, Optional ByRef y As String) As String
    x = Replace(x, "l", "_CHANGED_")
    y = Replace(y, "l", "_CHANGED_")

    myfunction = "ByVal x variable = " & x & vbCrLf & vbCrLf & _
    "ByRef y variable = " & y
    End Function

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  17. #17

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    cool thanks

    Now time for My lunch

  18. #18
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    ... u can return an array from a function ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  19. #19
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Ahhh, I thought we'd argued over this in the past, but couldn't find the post & thought I was dreaming, c'mon then where's the sample code to prove it ('cause I'm interested to know this too )!!!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  20. #20
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    yeah i was about to slap together some code, but ive to find a windows xp pro product key for the corporate edition like 10 minute ago ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  21. #21
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    VB Code:
    1. Private Function returnArray() As Long()
    2.     Dim x() As Long, i As Long: ReDim x(9)
    3.     For i = 0 To 9
    4.         x(i) = i
    5.     Next
    6.     returnArray = x
    7. End Function
    8.  
    9. Private Sub Form_Load()
    10.     Dim a() As Long, i As Long
    11.     a = returnArray
    12.     For i = 0 To UBound(a)
    13.         Debug.Print a(i)
    14.     Next
    15. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  22. #22
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489
    You can't do it with Fixed Length Arrays though
    right?

    Dim A(18) as Long

  23. #23
    Megatron
    Guest
    Just pass it by reference then.

  24. #24

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    If i remember, I had trouble doing that.
    Will it work through class mods (by using properties?)

  25. #25
    DaoK
    Guest
    You can't do it with Fixed Length Arrays though
    use Redim Preserve

  26. #26
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Um you can't ReDim Preserve a fixed array I don't think
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  27. #27
    DaoK
    Guest
    humm you have to declare you variable like : myArray()
    and under it you just use Redim Preserve myArray(8)

    I always do that because when it fixed you cant change it so with only 1 line you can change it like you want so I think it's better.

  28. #28
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Just to clear this one up - You can do a redim preserve, but NOT on a fixed array.
    • Dim arySample()
      Is declaring a dynamic array - an open one which you can alter the dimensions of.
    • Dim arySample(5)
      Is declaring a fixed - "you can't resize me I have 6 elements - bugger off" array.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  29. #29
    DaoK
    Guest
    yes that about what I just said

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