PC Review


Reply
Thread Tools Rate Thread

Create Function which returns an Array

 
 
ExcelMonkey
Guest
Posts: n/a
 
      24th Jun 2008
I want to create a function which returns an array. For Example I want
ArrayFunction to return X,Y.

I will then load the results of the Function into a 2D Array with 2 colums:

Private FunctionArray(A as String, B as String) As Variant
'This needs to produce a result that looks like X,Y

End Function

How do I set up the function to return an array result (2 values)?


Thanks

EM
 
Reply With Quote
 
 
 
 
Charlie
Guest
Posts: n/a
 
      24th Jun 2008
To declare a function as an array function it must be typed as String, Long,
Double, etc., not Variant

Public Function XYCoords(X as String, Y as String) As Double()

Dim dTmp as Double

ReDim dTmp(2) 'Note - I always use Option Base 1 in all my modules

dTmp(1) = CDbl(X)
dTmp(2) = CDbl(Y)
XYCoords = dTmp

End Function

End Function

"ExcelMonkey" wrote:

> I want to create a function which returns an array. For Example I want
> ArrayFunction to return X,Y.
>
> I will then load the results of the Function into a 2D Array with 2 colums:
>
> Private FunctionArray(A as String, B as String) As Variant
> 'This needs to produce a result that looks like X,Y
>
> End Function
>
> How do I set up the function to return an array result (2 values)?
>
>
> Thanks
>
> EM

 
Reply With Quote
 
Charlie
Guest
Posts: n/a
 
      24th Jun 2008
Correction

Dim dTmp() as Double


BTW, you can also ReDim dTmp as a two-dimension array and your array
function will return a 2D array!



"Charlie" wrote:

> To declare a function as an array function it must be typed as String, Long,
> Double, etc., not Variant
>
> Public Function XYCoords(X as String, Y as String) As Double()
>
> Dim dTmp as Double
>
> ReDim dTmp(2) 'Note - I always use Option Base 1 in all my modules
>
> dTmp(1) = CDbl(X)
> dTmp(2) = CDbl(Y)
> XYCoords = dTmp
>
> End Function
>
> End Function
>
> "ExcelMonkey" wrote:
>
> > I want to create a function which returns an array. For Example I want
> > ArrayFunction to return X,Y.
> >
> > I will then load the results of the Function into a 2D Array with 2 colums:
> >
> > Private FunctionArray(A as String, B as String) As Variant
> > 'This needs to produce a result that looks like X,Y
> >
> > End Function
> >
> > How do I set up the function to return an array result (2 values)?
> >
> >
> > Thanks
> >
> > EM

 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      24th Jun 2008
Here is a simple example...

Sub test()
Dim var As Variant

var = MyArray("This", "That")
End Sub

Public Function MyArray(ByVal str1 As String, ByVal str2 As String) As Variant
Dim aryReturn(2, 2) As Variant
Dim lng1 As Long
Dim lng2 As Long

For lng1 = 0 To 2
For lng2 = 0 To 2
aryReturn(lng1, lng2) = str1 & lng1 & str2 & lng2
Next lng2
Next lng1
MyArray = aryReturn

End Function
--
HTH...

Jim Thomlinson


"ExcelMonkey" wrote:

> I want to create a function which returns an array. For Example I want
> ArrayFunction to return X,Y.
>
> I will then load the results of the Function into a 2D Array with 2 colums:
>
> Private FunctionArray(A as String, B as String) As Variant
> 'This needs to produce a result that looks like X,Y
>
> End Function
>
> How do I set up the function to return an array result (2 values)?
>
>
> Thanks
>
> EM

 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      24th Jun 2008
You can not redim a 1d array into a 2d array. Additionally if you redim a 2 d
array you can only redimension the first element.
--
HTH...

Jim Thomlinson


"Charlie" wrote:

> Correction
>
> Dim dTmp() as Double
>
>
> BTW, you can also ReDim dTmp as a two-dimension array and your array
> function will return a 2D array!
>
>
>
> "Charlie" wrote:
>
> > To declare a function as an array function it must be typed as String, Long,
> > Double, etc., not Variant
> >
> > Public Function XYCoords(X as String, Y as String) As Double()
> >
> > Dim dTmp as Double
> >
> > ReDim dTmp(2) 'Note - I always use Option Base 1 in all my modules
> >
> > dTmp(1) = CDbl(X)
> > dTmp(2) = CDbl(Y)
> > XYCoords = dTmp
> >
> > End Function
> >
> > End Function
> >
> > "ExcelMonkey" wrote:
> >
> > > I want to create a function which returns an array. For Example I want
> > > ArrayFunction to return X,Y.
> > >
> > > I will then load the results of the Function into a 2D Array with 2 colums:
> > >
> > > Private FunctionArray(A as String, B as String) As Variant
> > > 'This needs to produce a result that looks like X,Y
> > >
> > > End Function
> > >
> > > How do I set up the function to return an array result (2 values)?
> > >
> > >
> > > Thanks
> > >
> > > EM

 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      24th Jun 2008
A variant can be anything so it is acceptable to declare an array as variant.
Where possible you want to avoid variants, but if you are not sure what
values are going into the array then variant is not a bad choice as it will
hold anything. Your example is a 1d array which is not what was asked for.
--
HTH...

Jim Thomlinson


"Charlie" wrote:

> To declare a function as an array function it must be typed as String, Long,
> Double, etc., not Variant
>
> Public Function XYCoords(X as String, Y as String) As Double()
>
> Dim dTmp as Double
>
> ReDim dTmp(2) 'Note - I always use Option Base 1 in all my modules
>
> dTmp(1) = CDbl(X)
> dTmp(2) = CDbl(Y)
> XYCoords = dTmp
>
> End Function
>
> End Function
>
> "ExcelMonkey" wrote:
>
> > I want to create a function which returns an array. For Example I want
> > ArrayFunction to return X,Y.
> >
> > I will then load the results of the Function into a 2D Array with 2 colums:
> >
> > Private FunctionArray(A as String, B as String) As Variant
> > 'This needs to produce a result that looks like X,Y
> >
> > End Function
> >
> > How do I set up the function to return an array result (2 values)?
> >
> >
> > Thanks
> >
> > EM

 
Reply With Quote
 
ExcelMonkey
Guest
Posts: n/a
 
      24th Jun 2008
Slick. Thanks again

EM

"Jim Thomlinson" wrote:

> Here is a simple example...
>
> Sub test()
> Dim var As Variant
>
> var = MyArray("This", "That")
> End Sub
>
> Public Function MyArray(ByVal str1 As String, ByVal str2 As String) As Variant
> Dim aryReturn(2, 2) As Variant
> Dim lng1 As Long
> Dim lng2 As Long
>
> For lng1 = 0 To 2
> For lng2 = 0 To 2
> aryReturn(lng1, lng2) = str1 & lng1 & str2 & lng2
> Next lng2
> Next lng1
> MyArray = aryReturn
>
> End Function
> --
> HTH...
>
> Jim Thomlinson
>
>
> "ExcelMonkey" wrote:
>
> > I want to create a function which returns an array. For Example I want
> > ArrayFunction to return X,Y.
> >
> > I will then load the results of the Function into a 2D Array with 2 colums:
> >
> > Private FunctionArray(A as String, B as String) As Variant
> > 'This needs to produce a result that looks like X,Y
> >
> > End Function
> >
> > How do I set up the function to return an array result (2 values)?
> >
> >
> > Thanks
> >
> > EM

 
Reply With Quote
 
Charlie
Guest
Posts: n/a
 
      24th Jun 2008
Sorry for the ambiguous statment. I certainly didn't mean to imply that a 1D
array could be ReDimmed into a 2D array. I meant that the OP could initially
ReDim dTmp into a 2D array, fill it as desired, and return it through the
function name. Much like your example further down.

"Jim Thomlinson" wrote:

> You can not redim a 1d array into a 2d array. Additionally if you redim a 2 d
> array you can only redimension the first element.
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Charlie" wrote:
>
> > Correction
> >
> > Dim dTmp() as Double
> >
> >
> > BTW, you can also ReDim dTmp as a two-dimension array and your array
> > function will return a 2D array!
> >
> >
> >
> > "Charlie" wrote:
> >
> > > To declare a function as an array function it must be typed as String, Long,
> > > Double, etc., not Variant
> > >
> > > Public Function XYCoords(X as String, Y as String) As Double()
> > >
> > > Dim dTmp as Double
> > >
> > > ReDim dTmp(2) 'Note - I always use Option Base 1 in all my modules
> > >
> > > dTmp(1) = CDbl(X)
> > > dTmp(2) = CDbl(Y)
> > > XYCoords = dTmp
> > >
> > > End Function
> > >
> > > End Function
> > >
> > > "ExcelMonkey" wrote:
> > >
> > > > I want to create a function which returns an array. For Example I want
> > > > ArrayFunction to return X,Y.
> > > >
> > > > I will then load the results of the Function into a 2D Array with 2 colums:
> > > >
> > > > Private FunctionArray(A as String, B as String) As Variant
> > > > 'This needs to produce a result that looks like X,Y
> > > >
> > > > End Function
> > > >
> > > > How do I set up the function to return an array result (2 values)?
> > > >
> > > >
> > > > Thanks
> > > >
> > > > EM

 
Reply With Quote
 
Charlie
Guest
Posts: n/a
 
      24th Jun 2008
I beg to differ on what was asked for. The OP indicated that he/she would
take the results of the array (X, Y) and "then load the results of the
Function into a 2D Array"

Sounded like the OP was asking for a 1D array to be returned. Which was why
I later added the part about ReDimming dTmp to a 2D array, implying that the
OP could do it all in the function and save time.

"Jim Thomlinson" wrote:

> ... Your example is a 1d array which is not what was asked for.
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Charlie" wrote:
>
> > To declare a function as an array function it must be typed as String, Long,
> > Double, etc., not Variant
> >
> > Public Function XYCoords(X as String, Y as String) As Double()
> >
> > Dim dTmp as Double
> >
> > ReDim dTmp(2) 'Note - I always use Option Base 1 in all my modules
> >
> > dTmp(1) = CDbl(X)
> > dTmp(2) = CDbl(Y)
> > XYCoords = dTmp
> >
> > End Function
> >
> > End Function
> >
> > "ExcelMonkey" wrote:
> >
> > > I want to create a function which returns an array. For Example I want
> > > ArrayFunction to return X,Y.
> > >
> > > I will then load the results of the Function into a 2D Array with 2 colums:
> > >
> > > Private FunctionArray(A as String, B as String) As Variant
> > > 'This needs to produce a result that looks like X,Y
> > >
> > > End Function
> > >
> > > How do I set up the function to return an array result (2 values)?
> > >
> > >
> > > Thanks
> > >
> > > EM

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To: C++ DLL function returns Structure Array to VB .NET lov4mu6@gmail.com Microsoft VB .NET 0 16th Sep 2006 01:20 AM
Function that returns an array? The.Relinator@gmail.com Microsoft C# .NET 6 16th Aug 2006 03:42 PM
Function Returns Multidim Array =?Utf-8?B?TXVycA==?= Microsoft Access VBA Modules 3 15th Sep 2005 07:07 PM
Function that returns an Array with a Query =?Utf-8?B?R3JlZyBLYXVmbWFu?= Microsoft Access Queries 2 6th Mar 2004 03:05 PM
Function that returns an Array with a Query =?Utf-8?B?R3JlZyBLYXVmbWFu?= Microsoft Access VBA Modules 2 6th Mar 2004 03:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:20 PM.