Quickly initialize Variant and String arrays

Quickly initialize Variant and String arrays

Visual Basic doesn’t provide any way to declare an array and initialize its elements at the same time. In most cases you end up with setting individual elements one by one, as in:

Dim strArray(0 To 3) As StringstrArray(0) = "Spring"strArray(1) = "Summer"strArray(2) = "Fall"strArray(3) = "Winter"

Under VB4, VB5, and VB6 you can create an array of Variants on the fly, using the Array() function:

Dim varArray() As VariantvarArray() = Array("Spring", "Summer", "Fall", "Winter")

but there is no similar function to create arrays of data types other than Variant. If you’re using VB6, however, you can create String arrays using the Split() function:

Dim varArray() As String' arrays returned by Split are always zero-basedvarArray() = Split("Spring;Summer;Fall;Winter", ";")

Under VB6 you can also take advantage of the capability for a Function to return an array, and build your own array initialization routines, such as the following one:

Function ArrayInt(ParamArray values() As Variant) As Integer()    Dim i As Long    ReDim res(0 To UBound(values)) As Integer    For i = 0 To UBound(values)        res(i) = values(i)    Next    ArrayInt = res()End Function

You could also build a routine that tests the type of values passed to it, and returns an array of the correct type. In this case, the function should be declared to return a Variant.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several