# Visual Basic > Visual Basic 6 and Earlier >  Create arrays efficiently

## SearchingDataOnly

I want to get a function that creates an array:


```
Function New_Array(ByVal ElementType As VbVarType, ParamArray ArrayElements() As Variant) As Variant

End Function
```

I'd like to know what is the most efficient way. Thanks!

----------


## VanGoghGaming

I doubt you can make a new typed array without first declaring it of that type. I guess you could go through all possible types with a "Select Case" statement and convert each variant to that type using its corresponding function (CLng, CInt, CByte, CStr, etc).

----------


## Elroy

There's Array(...)  :Stick Out Tongue: 



```
Dim v As Variant
v = Array(1,2,3,4,5)
```

----------


## VanGoghGaming

That's not what he wants. "Array" creates an array of variants while he wants a variant containing an array of intrinsic types.

----------


## Elroy

Ahhh, I could do it with API calls, but I'm not sure I see the point.

I'd probably just use a *Select Case ElementType* statement.

Done that way, you'd need to check the VarType of all the items in your ParamArray, or strange things could happen during assignment.

----------


## dilettante

Given more context for specific cases various alternatives could be chosen.  I'm not sure why people ask for "one size fits all" magic bullets so often.

----------


## Elroy

> Given more context for specific cases various alternatives could be chosen.  I'm not sure why people ask for "one size fits all" magic bullets so often.


I must admit that that's where my head went as well.

----------


## The trick

An efficient way is to use a binary data stored either to a resource or to an usercontrol. Variant usage isn't efficient as for the speed as for the size.

----------


## Elroy

> An efficient way is to use a binary data stored either to a resource or to an usercontrol. Variant usage isn't efficient as for the speed as for the size.


Thinking about it like that, I miss the old DATA and READ statements of QBASIC more than you might think.  I'd love to be able to just dump certain data into my source code, and then have it available at runtime.  I sometimes use hidden multiline textboxes, but those have limits ... and then I'm forced to use a file in resources.

(And I certainly wouldn't want a solution that creates another reference.)

----------


## The trick

> Thinking about it like that, I miss the old DATA and READ statements of QBASIC more than you might think.  I'd love to be able to just dump certain data into my source code, and then have it available at runtime.  I sometimes use hidden multiline textboxes, but those have limits ... and then I'm forced to use a file in resources.
> 
> (And I certainly wouldn't want a solution that creates another reference.)


I often use my Add-in to store binary data like:


```
BITS 32

%assign TEMP_COUNTER 0

%macro insert_file 1

	cmp eax, TEMP_COUNTER
	jnz %%label3	

	call %%label2
%%label1:
	incbin %1
%%label2:
	pop eax
	mov edx, %%label2 - %%label1
	ret 4
%%label3:
%assign TEMP_COUNTER  TEMP_COUNTER +1
%endmacro

xor edx, edx
mov eax, dword [esp+4]

insert_file "wf_01.bin"
insert_file "wf_02.bin"
insert_file "wf_03.bin"
insert_file "wf_04.bin"
insert_file "wf_05.bin"
insert_file "wf_06.bin"
insert_file "firmware_sw.bin"
insert_file "firmware_sw_inv.bin"
insert_file "firmware_hw.bin"
insert_file "firmware_hw_inv.bin"
```



```
Option Explicit

Public Type tResource
    pData   As Long
    lSize   As Long
End Type

Public Function GetResource( _
                ByVal lIndex As Long) As tResource
                    
End Function
```

----------


## georgekar

In my M2000 Interpreter, I gave the READ and DATA, but I expand that, using a stack of values, so a Data 1, "alfa", 4 send these 3 values to the end of stack (it is collection inside), and the  Read statement pop values in a FIFO manner. So I gave the Push statement to add to stack to the top (the first item which Read pop values), so using Push and Read we can make a LIFO list of values. So in every line of code there is a hidden stack of values, and the READ, PUSH and DATA works fine. Each "module" (like a sub) call other modules passing the stack of values, and each function start a new stack of values, initializing with the passing arguments (as FIFO so the first READ pop the right most argument in the parameter list). So all functions/modules can use variadic passing of arguments, and there are ways to identify types before any pop (from a Read statement) and we can define types at the Read statement to raise error if the type to be popped isn't the specific demand type at Read statement.

For the OP, the RefArray which I demonstrate in this forum make arrays of some types, which we hold in a variant. So a variant can hold an array of long. Using the RefArray, a class, we hold the array with a pointer (object). So when we pass the array we pass it by the pointer. If we have an array of long as is (not in a variant) every pass by reference lock the array, so there isn't a way to resize it (in the calling sub or function). If we pass that array by value, we pass a copy of the array. So using the RefArray or anything like this, we can use arrays as objects, we can pass it everywhere and we can resize it everywhere. To resize the array we can't use DIM (because that DIM goes to variant which hold the array), so we have to use API, getting the pointer to safe array and resize as we wish.

----------


## SearchingDataOnly

Thank you, VanGoghGaming, Elroy, dilettante, georgekar and The trick.

I don't seem to describe my problem clearly. I want to implement something similar to vbCorLib's NewArray function. Here is vbCorLib's NewArray function:



```
Public Function NewArray(ByVal ElementType As VbVarType, ParamArray ArrayElements() As Variant) As Variant
    Dim Length As Long
    Dim Args() As Variant
    
    Length = UBound(ArrayElements) + 1
    Helper.MoveVariant NewArray, CorArray.CreateInstance(ElementType, Length)
    
    If Length > 0 Then
        Helper.Swap4 ByVal ArrPtr(Args), ByVal Helper.DerefEBP(16)
        CorArray.CopyEx Args, 0, NewArray, 0, Length
    End If
End Function
```

*Helper* in vbCorLib used Matt Curland's ASM code, and it had a lot of weird problems on my computer. I'd like to be able to implement something similar to vbCorLib's NewArray function without using ASM code.

----------


## SearchingDataOnly

Hi @The trick,

Please take a look at my other thread Equivalent functions (or APIs) to Matt Curland's ASM code.


I still can't find code to replace *Helper.Swap4* and *Helper.DerefEBP*, and I can't fix the problem with *ParamArray*:



```
Public Function Format(ByRef FormatText As String, ParamArray Args() As Variant) As String
    Dim SwappedArgs() As Variant
    Helper.Swap4 ByVal ArrPtr(SwappedArgs), ByVal Helper.DerefEBP(16)
    Format = FormatCore(Nothing, FormatText, SwappedArgs)
End Function
```

----------

