dcsimg
Results 1 to 23 of 23

Thread: Passing Arrays

  1. #1

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Question Passing Arrays

    Hi,

    I have a function that generates an array of data (data(0), data(1)) and it's number of items is different every time. One time it could be data(0-2) and the next data (0-10). The array within the function is isolated to the function but I need to be able to send that data array back to the caller...

    for example:

    Code:
    public function xyz() as string
    
    array(0) = 1
    array(1) = 2
    ....
    
    
    end function
    How can i set the function call to be able to pass in an undefined array and have it return with the correct data?

    Something like?

    Code:
    dim myarray()
    
    call xyz(myarray())
    debug.print array(0)
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,569

    Re: Passing Arrays

    Set the parameter as ByRef:
    Code:
    Dim fooArray() As String
    
    Private Function fooFunction(ByRef arr() As String) As Boolean
        'Do something
    
        Return True
    End Function
    Here is how MSDN defines ByRef:
    Specifies that an argument is passed in such a way that the called procedure can change the value of a variable underlying the argument in the calling code.

  3. #3

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Passing Arrays

    ok - I was trying to make it an optional addition in the function call (adding it to the end of an existing function... :-/)

    Now my issue is assigning the arrays values within the function to the array passed in the call... It used to be I could do fooArray = MyArray but I get an error when I do that in .net...
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,569

    Re: Passing Arrays

    Could you give me a bit more detail to what it is that you're wanting to do? Generalities just cause confusion.

  5. #5

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Passing Arrays

    Basically I have a function that calls a class that returns an array of data..

    Code:
    MyArray = ClassFunction(...)
    
    debug.print(MyArray(0))
    debug.print(MyArray(1))
    debug.print(MyArray(2))
    I'm calling the function from a form (and other places hence the function) and I need to return the array (MyArray) back to the calling form so my function is

    Code:
    Public Function XYZ(ByRef Array() as string)
    
    MyArray = ClassFunction(...)
    
    end function
    and my form (command button) called the function and then handles the array to display in to the user..

    Code:
    command button_click
    
    dim Array() as string
    
    call XYZ(Array)
    
    Text1.text = Array(0)
    Text2.test = Array(2)
    ...

    In my function I can pass the values of one array to another with a loop - not a problem. BUT I was hoping to do it more elegantly and simply assign the one array to the other and save the looping..
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  6. #6
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Passing Arrays

    With Dday9's example, setting the arr() parameter ByRef means that whatever is done inside the function will occur to the original array that the caller passes in to that function. Although that example shows how to pass in a array with any number of items, there isn't an easy way to create a parameter for an array that may have any number of dimensions. If you are using an array as a parameter, you'd write the variable name followed by ()'s with a comma for every additional dimension beyond the first. Thus:
    Code:
    Function fooBar(ByRef arr(,) As String) As Boolean
        ' Whatever this function does
    End Function
    fooBar would expect a 2-dimensional array of Strings to be passed to it, with any number of elements inside each dimension.

  7. #7

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Passing Arrays

    I get that pat Pyth007... My issue is how can I assign an array withing fooBar to arr?

    Something like this (but not this because it does not work)

    Code:
    Function fooBar(ByRef arr(,) As String) As Boolean
        arr = MyExistingArry
    End Function
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  8. #8
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Passing Arrays

    This depends on where myExisting array gets its data.

    If it is literal data as you have shown so far you can set them with literals like so.
    (Using a single dimension array of integers for this example.)
    Code:
    Dim MyExistingArray() as integer {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}
    Incidentally if this is all you are doing I would look at simply making your array as public and not use a function at all.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  9. #9

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Passing Arrays

    It is not leterals Gruff... It's generated by the class that my function is calling (not my class). I suppose I could make the array I am having the class assign it to public but that seems a bit sloppy IMO...
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  10. #10
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Passing Arrays

    I'm unsure what you mean. You pass in the existing array when you call the function. And since that parameter is ByRef, the parameter will not be a copy of the existing array, but will point to that actual existing array meaning that all of the changes within the function will be seen once the function ends. Take a look at this example. I am creating an existingArray that is a String array with 4 items in it. I pass this array to arrayFunction ByRef where one of the things it does is change the String in the first element from "Hi" to "Hello". In the caller (the Load event-handler), I show what this 1st element is before the function call as well as after to show how that value got changed in the caller in addition to being changed in the function.
    Code:
        Private Sub Start_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim existingArray() As String = {"Hi", "VB", ".Net", "World"}
            MsgBox(existingArray(0))
            Dim result As String = arrayFunction(existingArray)
            MsgBox(result & " | " & existingArray(0))
        End Sub
    
        Function arrayFunction(ByRef arr() As String) As String
            arr(0) = "Hello"
            Return Join(arr, " ")
        End Function

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,569

    Re: Passing Arrays

    As far as I can tell, there is no need for multiple dimensions, but I'm still not sure what it is that you're trying to do(again... generalities). However, you can make one array equal another without using a loop. Here is one example of doing just that:
    Code:
    Dim arr(), foo() As String
    
    arr = {"1", "2", "3", "4"}
    foo = arr 'foo = {"1", "2", "3", "4"}

  12. #12

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Passing Arrays

    I think the part your missing is that there is a 3rd array that is part of the class...

    Heres a more accurate code sample without giving out the details I'm not allowed to share (company rules and all)...

    Code:
    Sub commandbutton1_click
    
    dim Array() as string
    
    call GetData(Array())
    
    text1.text = array(0)
    text2.text = array(2)
    ...
    
    end sub
    Code:
    public function GetData(ByRef MyArray() as string
    
    ClassMessage as new GetMessage(variable1, variable2, variable3)
    
    Dim SomeResponse  as ClassMessener
    
    SomeResponse  = ClassMessage.GetRespose(Timeout,Client)
    
    Dim MyResult = SomeResponse .ABC.Variables
    
    ******************
    Here is where I want to assign MyResult to MyArray without a loop
    ******************
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  13. #13

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Passing Arrays

    Quote Originally Posted by dday9 View Post
    As far as I can tell, there is no need for multiple dimensions, but I'm still not sure what it is that you're trying to do(again... generalities). However, you can make one array equal another without using a loop. Here is one example of doing just that:
    Code:
    Dim arr(), foo() As String
    
    arr = {"1", "2", "3", "4"}
    foo = arr 'foo = {"1", "2", "3", "4"}
    I am unable to simply assign arr to foo in this scenerio..


    I just realized that the class is using a LIST instead of an Array. I'm a bit confused because it is an array and I can parse it just like an array...
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  14. #14
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Passing Arrays

    If by list you mean list(of type) then that is an easy fix.
    List(of type) has a toArray method.
    Code:
    Dim oList as New List(of string)
    oList.Add("Cat")
    oList.Add("Dog")
    oList.Add("Mouse")
    
    Dim oArr() as string = oList.ToArray
    And yes lists of type share some of the functionality of an array.

    Dim sName as string = oList(0)
    or
    oList(2) = "House"

    Lists have a lot more tools built into them.
    I like to think of them as a listbox without the graphics.
    Last edited by Gruff; Apr 21st, 2015 at 04:35 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  15. #15

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Passing Arrays

    That give me a new error..

    Code:
    Error	6	Number of indices is less than the number of dimensions of the indexed array.
    I think it has to do with the array being passed in the function call ByRef..

    If I create a new array I do not get the error..
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  16. #16

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Passing Arrays

    To be specific the list is defined as As System.Collections.Generic.IList
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  17. #17
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Passing Arrays

    That is a bit different.
    A list of that type can hold just about anything including objects with properties.

    I guess I would try to find out what the datatype is that is being stored in your list before writing code to deal with it.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  18. #18
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,911

    Re: Passing Arrays

    If you want the function to return an array, why not just have the function return an array?
    Code:
      Dim fooArray() As String = fooFunction()
    
      Private Function fooFunction() As String()
        Dim MyArray() As String = {"One", "Two", "Three"}
        Return MyArray
      End Function

  19. #19
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Passing Arrays

    What does this yield?

    Code:
       MessageBox.Show(TypeName(list(0)))
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  20. #20
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Passing Arrays

    Quote Originally Posted by RudyL View Post
    Basically I have a function that calls a class that returns an array of data..
    Code:
    MyArray = ClassFunction(...)
    Code:
    Public Function XYZ(ByRef Array() as string)
       MyArray = ClassFunction(...)
    end function
    and my form (command button) called the function and then handles the array to display in to the user..

    Code:
    command button_click
    
    dim Array() as string
    
    call XYZ(Array)
    
    Text1.text = Array(0)
    Text2.test = Array(2)
    ...
    Rudy,
    Functions have return value. The pattern you have shown above is treating the Function as a Subroutine in which you are trying to return the result of the function to the caller via the function argument.

    Take a look at this and see if it makes sense to you.

    Code:
    Private Sub test()
       Dim array As String() = XYZ()
       For i As Int32 = 0 To array.GetUpperBound(0)
          Debug.Print(array(i))
       Next
    End Sub
    Public Function XYZ() As String()
       Return SomeClass.GetData 
    End Function
    Code:
    Public Class SomeClass
       Private Shared rnd As New Random
       Public Shared Function GetData() As String()
          Dim ub As Int32 = rnd.Next(3, 10) ' make random sized return array
          Dim ret(0 To ub) As String
          For i As Int32 = 0 To ub
             ret(i) = (i + 10).ToString
          Next
          Return ret
       End Function
    End Class
    Edit: I see that while I was reviewing this thread and writing this out that Passel said the same thing.

  21. #21
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Passing Arrays

    I guess until we know what the datatype is that is being returned by his class it is difficult to give relevant help.

    The first thing that jumps out at me is that originally he was trying to pass a 2D array.
    The class is returning a ilist of some sort.

    There are some fundamental problems right there.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  22. #22

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Passing Arrays

    Sorry - let me catch up from last night...

    Passel - I did not know a function could "return" as an array. I'll need to look into that at some point... and may see if it will work for this.

    TnTinMN - Yes, in this example the function appears to be treated like a sub routine but it is not. I am passing the array back as an argument for a few reasons. 1) I did not know a function could return an array 2) The function returns a status of PASS or FAIL when it completes as it is today (follows the design pattern of the application).

    Gruff - when I look at the first element in the array (myarray(0)) I get a string value with letters and numbers and special characters like &*^% {which are expected as part of the string}) returned. Looking at the class definition in the Object browser it does not tell me String or anything else. It looks like this:

    Code:
    {Variable: Id: 125.2355.88544; Data: abcdefghijklmno}
        Data: {abcdefghijklmno}
        Id: {125.2355.88544}
    If I do myarray(0).tostring I get this

    Code:
    Variable: Id: 125.2355.88544; Data: abcdefghijklmno
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  23. #23
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Passing Arrays

    Quote Originally Posted by RudyL View Post
    To be specific the list is defined as As System.Collections.Generic.IList
    I missed this previously.

    Quote Originally Posted by RudyL View Post
    ...TnTinMN - Yes, in this example the function appears to be treated like a sub routine but it is not. I am passing the array back as an argument for a few reasons. 1) I did not know a function could return an array 2) The function returns a status of PASS or FAIL when it completes as it is today (follows the design pattern of the application).
    IList is an interface and can be used as an argument type.

    Code:
    Private Function xyz(ByRef il As IList) As Boolean
       il = Form1.GetData
       Return il IsNot Nothing AndAlso (il.Count > 0)
    End Function
    Example usage:
    Code:
    Dim il As IList = Nothing
    If xyz(il) Then
       Debug.Print(il.GetType.FullName)
       Debug.Print(il(0).GetType.FullName)
    End If
    If you follow the pattern shown in this example, what is printed in the 'Immediate Window' when you run it? This information could help you assign the values to a more meaning type.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width