Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning an array by function

Status
Not open for further replies.

WilliamUT

IS-IT--Management
Oct 8, 2002
182
US
I have written a function that needs to return an array how do you do this?
 
can you be a little more dcetailed then that.
what are you doing? what is populating the array? and how is it populating the array? ---------------------------------------
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }

 
Well just like it sounds :) :

in normal vb it would look something like this



Function Test() as array
Dim MyArray(10) as string

MyArray(0) = "1"
...
MyAttay(10) = "10"


Test = MyArray
end Function
 
Well just like it sounds :) :

in normal vb it would look something like this



Function Test() as array
Dim MyArray(10) as string

MyArray(0) = "1"
...
MyArray(10) = "10"


Test = MyArray
end Function
 
alrighty then...
Dim MyArray(5)
' creates the array
poulate it the same as in VB
MyArray(0) = "1"

maybe a loop wioll explain the creation better
Dim MyArray(5)
For counter = 0 To 5
MyArray(counter) = " Element " & counter
msgbox "This is array element # " & counter & MyArray(counter)
Next

also you will want to redim preserve the array if you build on it. same as in VB

main difference is you do not have the option of string or integer etc.. you must validate for this upon needing a certain data type ---------------------------------------
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }

 
well that really didnt answer my question. I need my function to be able to return an array.

Your example just shows me how to populate an array i already know how to do that...i need a function to return the array after it's been populated with data
 
WilliamUT seems to be on the right track, but I can't get it to work for some reason.

In Vbscript, to return a value from a function call, you assign the function name a value WITHIN the function, and that's what gets returned. So, in WilliamUt's example, "Test = MyArray" would presumeably return whatever was in MyArray so you can use it once the function ends.

So, how do I know it worked? If I try to refer to any of the elements of the array outside of the of the Test() function like:

first_element = Test(0)

...how does it know the difference between referring to the first element of the Test array and trying to pass the value of Zero into the function? I'm assuming that's why WilliamUT declared the function "as array", but when I do that all I get is:

function the_function(test) as Array
----------------------------^

(the arrow is supposed to line up with the "a" in "as")

Any takers?
 
errr, scratch that. My error is

function Test() as Array
----------------^
 
sorry guys, I had to leave for a while and couldn't finish my post, but anyways. the reasoning behind showing the population of the array was building to showing how to return all the value in valid form but again have to make it short so I went ahead and found a example

WilliamUT, there were about a 100 different ways you could have interpruted your question. that is why I was asking for a little more detail.

disord, you'll get a type mismatch everytime that way [smile] read the example in the link ---------------------------------------
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }

 
Wow! I'll just settle for cramming all into a string separated by pipes and split()ing it afterwards. :)
 
tell me about it. that's why vbscript lacks there of some of the things regular VB has.

one note. VB.NET does support the same methods as VB6 so if you really want to avoid lengthy functions to do simple or as some say simple conversions etc.. you may want to consider converting to the .NET platform ---------------------------------------
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }

 
Well, you can always do this, much as you would in VB with a variant function:
Code:
Option Explicit
Dim varX

Function MakeArray(ByVal lngSize)
  Dim aryTemp(), lngI

  ReDim aryTemp(lngSize)
  For lngI = 0 To UBound(aryTemp)
    aryTemp(lngI) = lngI
  Next

  MakeArray = aryTemp
End Function

varX = MakeArray(3)
MsgBox CStr(varX(2))
In this example MakeArray() returns a variant that contains an array. It isn't an array itself, but you can use it much as you would an array as shown at the end where element 2 of the array within variant varX is displayed.

Does this do what you need?
 
Yep! Thanks for all the help guys :) I have another question too but i will post it in another thread...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top