5

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?

5
  • 2
    I think the answer below is probably true, but if you really really want to dive into details, I suggest these two pages: bytecomb.com/… and bytecomb.com/vba-scalar-variables-and-pointers-in-depth. Commented Jul 28, 2017 at 13:07
  • 3
    The relevant question to me would be why? Commented Jul 28, 2017 at 14:05
  • 2
    @Wolfie, are you really asking why I would need to create a zero length typed array? Well an array not defined doesn't work with UBound, LBound and For Each, but mainly because I need an huge array initialized with 0 that I can compute and filter and where the expected output can be zero items. I'm still amazed to see that it's not possible with Redim arr(0, -1) and that the language doesn't offer a simple way to do it. Commented Jul 28, 2017 at 20:47
  • Initialising it with 0 is doable though, and why can't you output 0 items in an untyped array? I was just curious because I can't think of a situation when a quick check for emptiness wouldn't belay any concerns... Commented Jul 28, 2017 at 21:34
  • option base 0 ? I'm not sure want you are looking for. Commented Jul 28, 2017 at 23:18

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Not Not arr returns 0 and the call to SafeArrayRedim does nothing
1

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

1 Comment

That's rather presumptuous to state that it's not possible since it's technically possible with a safe array. If not with a VBA function, there should be a way with a native function like RtlMoveMemory or SafeArrayRedim. And how is your code relevant to the post? The array from your example is a Variant array of and not a Double array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.