Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

ArrayAny - Return an initialized array of any type
[ All Languages » VB »  Arrays]

Total Hit ( 2040)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 



Click here to copy the following block
' Returns an array and initializes it with passed data.
'
' It is similar to the Array function, but it works with
' array of any type. The type of the returned array is
' assumed to be the type of the first element in the
' parameter list, so you might need to force a given
' data type using one of the VB's data conversion functions
'
' Example: return the array of the first 8 prime numbers, as Longs
'   primes() = ArrayAny(2&, 3, 5, 7, 11, 13, 17, 19)
' or
'   primes() = ArrayAny(CLng(2), 3, 5, 7, 11, 13, 17, 19)

Function ArrayAny(ParamArray values() As Variant) As Variant
  Dim i As Long
  Dim maxEl As Long
  Dim res As Variant
  
  maxEl = UBound(values)
  
  ' we can't use the vbObject constant for objects
  ' because the VarType() function might return
  ' the type of the object's default property
  If IsObject(values(0)) Then
    ReDim arrObj(0 To maxEl) As Object
    ' we need a separate loop, too
    For i = 0 To maxEl
      Set arrObj(i) = values(i)
    Next
    ArrayAny = arrObj()
    Exit Function
  End If
  
  ' create different arrays, depending on the
  ' type of the first argument
  Select Case VarType(values(0))
    Case vbInteger
      ReDim arrInt(0 To maxEl) As Integer
      res = arrInt()
    Case vbLong
      ReDim arrLng(0 To maxEl) As Long
      res = arrLng()
    Case vbSingle
      ReDim arrSng(0 To maxEl) As Single
      res = arrSng()
    Case vbDouble
      ReDim arrDbl(0 To maxEl) As Double
      res = arrDbl()
    Case vbCurrency
      ReDim arrCur(0 To maxEl) As Currency
      res = arrCur()
    Case vbString
      ReDim arrStr(0 To maxEl) As String
      res = arrStr()
    Case vbDate
      ReDim arrDat(0 To maxEl) As Date
      res = arrDat()
    Case vbBoolean
      ReDim arrBol(0 To maxEl) As Boolean
      res = arrBol()
    Case Else
      ' unsupported data type
      ' (might be a UDT or an array)
      Err.Raise 5
  End Select
      
  ' now we can copy all values into the array
  For i = 0 To maxEl
    res(i) = values(i)
  Next
  ArrayAny = res
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.