-
Apr 21st, 2015, 03:10 PM
#1
Thread Starter
Frenzied Member
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".
-
Apr 21st, 2015, 03:16 PM
#2
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.
-
Apr 21st, 2015, 03:26 PM
#3
Thread Starter
Frenzied Member
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".
-
Apr 21st, 2015, 03:31 PM
#4
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.
-
Apr 21st, 2015, 03:38 PM
#5
Thread Starter
Frenzied Member
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".
-
Apr 21st, 2015, 03:44 PM
#6
Hyperactive Member
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.
-
Apr 21st, 2015, 03:47 PM
#7
Thread Starter
Frenzied Member
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".
-
Apr 21st, 2015, 04:00 PM
#8
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
-
Apr 21st, 2015, 04:02 PM
#9
Thread Starter
Frenzied Member
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".
-
Apr 21st, 2015, 04:02 PM
#10
Hyperactive Member
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
-
Apr 21st, 2015, 04:09 PM
#11
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"}
-
Apr 21st, 2015, 04:15 PM
#12
Thread Starter
Frenzied Member
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".
-
Apr 21st, 2015, 04:20 PM
#13
Thread Starter
Frenzied Member
Re: Passing Arrays
Originally Posted by dday9
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".
-
Apr 21st, 2015, 04:28 PM
#14
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
-
Apr 21st, 2015, 04:34 PM
#15
Thread Starter
Frenzied Member
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".
-
Apr 21st, 2015, 04:37 PM
#16
Thread Starter
Frenzied Member
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".
-
Apr 21st, 2015, 04:45 PM
#17
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
-
Apr 21st, 2015, 04:51 PM
#18
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
-
Apr 21st, 2015, 04:55 PM
#19
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
-
Apr 21st, 2015, 05:15 PM
#20
Re: Passing Arrays
Originally Posted by RudyL
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.
-
Apr 21st, 2015, 05:28 PM
#21
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
-
Apr 22nd, 2015, 09:15 AM
#22
Thread Starter
Frenzied Member
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".
-
Apr 22nd, 2015, 11:58 AM
#23
Re: Passing Arrays
Originally Posted by RudyL
To be specific the list is defined as As System.Collections.Generic.IList
I missed this previously.
Originally Posted by RudyL
...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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|