code_junkie

How do I determine if an array is initialized in VB6?

Nov 11th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. Public Declare Function GetMem4 Lib "msvbvm60" (ByVal pSrc As Long, ByVal pDst As Long) As Long
  2. Public Declare Function ArrPtr Lib "msvbvm60" Alias "VarPtr" (arr() As Any) As Long
  3.  
  4.  
  5. Public Function ArrayExists(ByVal ppArray As Long) As Long
  6. GetMem4 ppArray, VarPtr(ArrayExists)
  7. End Function
  8.  
  9. ? ArrayExists(ArrPtr(someArray))
  10.  
  11. Public Function IsArrayInitialized(arr) As Boolean
  12.  
  13. Dim rv As Long
  14.  
  15. On Error Resume Next
  16.  
  17. rv = UBound(arr)
  18. IsArrayInitialized = (Err.Number = 0)
  19.  
  20. End Function
  21.  
  22. Dim arr() As String
  23.  
  24. arr = Split(vbNullString, ",")
  25. Debug.Print UBound(arr)
  26.  
  27. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  28. (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
  29.  
  30. Public Function ArrayIsInitialized(arr) As Boolean
  31.  
  32. Dim memVal As Long
  33.  
  34. CopyMemory memVal, ByVal VarPtr(arr) + 8, ByVal 4 'get pointer to array
  35. CopyMemory memVal, ByVal memVal, ByVal 4 'see if it points to an address...
  36. ArrayIsInitialized = (memVal <> 0) '...if it does, array is intialized
  37.  
  38. End Function
  39.  
  40. Dim someArray() As Integer
  41.  
  42. If ((Not someArray) = -1) Then
  43. Debug.Print "this array is NOT initialized"
  44. End If
  45.  
  46. Dim ArrayToTest() As String
  47.  
  48. MsgBox StringArrayCheck(ArrayToTest) ' returns "false"
  49.  
  50. ReDim ArrayToTest(1 To 10)
  51.  
  52. MsgBox StringArrayCheck(ArrayToTest) ' returns "true"
  53.  
  54. ReDim ArrayToTest(0 To 0)
  55.  
  56. MsgBox StringArrayCheck(ArrayToTest) ' returns "false"
  57.  
  58. Dim x As String
  59.  
  60. x = Join(o)
  61.  
  62. StringArrayCheck = (Len(x) <> 0)
  63.  
  64. Dim someArray() as Integer
  65.  
  66. If someArray Is Nothing Then
  67. Debug.print "this array is not initialised"
  68. End If
Add Comment
Please, Sign In to add comment