michael
michael

Reputation: 949

How to create an empty array of Double

I trying to create an empty/defined array of Double which would reflect as Double(0, -1).

I'm able to create one for an array of String, Variant and Byte:

  Dim arr_variant() As Variant
  arr_variant = Array()        ' Variant(0 to -1) '

  Dim arr_string() As String
  arr_string = Split(Empty)    ' String(0 to -1) '

  Dim arr_byte() As Byte
  arr_byte = ""                ' Byte(0 to -1) '

  Dim arr_double() As Double
  arr_double = ???             ' Double(0 to -1) '

, but still haven't found a way for Double.

Maybe with LSet or with a native function?

Upvotes: 5

Views: 3012

Answers (2)

michael
michael

Reputation: 949

It seems that the only way is to call a native function:

Private Declare PtrSafe Function SafeArrayRedim Lib "OleAut32" ( _
  ByVal arr As LongPtr, ByRef dims As Any) As Long


Public Sub RedimDouble(arr() As Double, ByVal count As Long)
  If count Then
    ReDim Preserve arr(0 To count - 1)
  Else
    ReDim arr(0 To 0)
    SafeArrayRedim Not Not arr, 0@
  End If
End Sub


Public Sub Usage()

  Dim arr_double() As Double
  RedimDouble arr_double, 0    ' Double(0 to -1) '

End Sub

Upvotes: 3

Vityata
Vityata

Reputation: 43593

I would go with - not possible.

Take a look at the following code:

Option Explicit
Sub TestMe()
    Dim arr                   'Line 1
    arr = Array(CDbl(0))      'Line 2
    arr = Array(Empty)        'Line 3
End Sub
  • Line 1 - It takes a Variant array

enter image description here

  • Line 2 - Makes it Double array

enter image description here

  • Line 3 - When emptied, it is converted from double to Variant again.

enter image description here

Upvotes: 1

Related Questions