Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleQuickly load an array with values in VB4
Keywordsarray, load, values, data
CategoriesTips and Tricks, Software Engineering
 
Visual Basic does not have a DATA statement. Use the Array statement to create a variant array of the numbers. Then copy the numbers into an Integer array for better performance later.
 
Private Sub Command1_Click()
Dim numbers() As Integer
Dim txt As String
Dim i As Integer

    ' Load the data.
    LoadNumbers numbers(), Array(10, 15, 20, 25)
    
    ' Display the values loaded.
    txt = ""
    For i = LBound(numbers) To UBound(numbers)
        txt = txt & Format$(numbers(i)) & Chr$(10) & _
            Chr$(13)
    Next i
    MsgBox txt
End Sub

' Copy numbers from a variant array to an integer
' array. This will give better peformance.
Private Sub LoadNumbers(arr() As Integer, ByVal var_arr As _
    Variant)
Dim min_num As Integer
Dim max_num As Integer
Dim i As Integer

    ' Make room for the numbers.
    max_num = UBound(var_arr)
    min_num = LBound(var_arr)
    ReDim arr(min_num To max_num)
    
    ' Copy the numbers.
    For i = min_num To max_num
        arr(i) = var_arr(i)
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated