Array Function

Enter your search terms Submit search form

 


 

 

Today I came across one question about Visual Basic arrays, it goes like this:

How would I set an array in visual basic to several things at once? Like:
Dim arrMass(1 To 118) As Integer
arrMass(1) = 1.00794
arrMass(2) = 4.002602
arrMass(3) = 6.941

That gets a bit annoying. Is there a way I can go like:
arrMass() = [1.00794, 4.002602, 6.941]
or something?

The secret to the above answer is to use an array function, where a single variant variable can hold an array. The format is as follows:

                                        Dim myArray as Variant

                                        myArray=(x1,x2,x3,........,xn)

                                        where x1,x2,x3,......,xn can be either an integer, a single precision number, a double precision number, a string and so on

 

Let me illustrate this with the example on the right:



Private Sub Command1_Click()
Dim arrMass As Variant, index As Integer

arrMass = Array(1.00794, 4.002602, 6.941)
For index = 0 To 2
Print arrMass(index)
Next

End Sub

The print out will be

1.00794
4.002602
6.941

 

 

 

 


 

 

 

 

 

 

 [Back to VBToday]