vb6-array-examples
vb6-array-examples
'----------------------------------------------------------
' 1. Basic Array Declaration and Initialization
'----------------------------------------------------------
Private Sub DemoBasicArrays()
' Fixed-size array declaration
Dim numbers(4) As Integer ' Creates array with 5 elements (0 to 4)
'----------------------------------------------------------
' 2. Array Operations with Control Structures
'----------------------------------------------------------
Private Sub ArrayOperations()
Dim scores(9) As Integer
Dim i As Integer
For i = 0 To UBound(scores)
If scores(i) >= 60 Then
passing = passing + 1
Else
failing = failing + 1
End If
Next i
'----------------------------------------------------------
' 3. Multi-Dimensional Arrays
'----------------------------------------------------------
Private Sub MultiDimensionalArrayDemo()
' Declare 2D array for student grades (3 students, 4 subjects)
Dim grades(2, 3) As Integer
Dim student As Integer
Dim subject As Integer
Debug.Print "Student " & (student + 1) & " Average: " & _
Format(studentTotal / 4, "0.00")
Next student
End Sub
'----------------------------------------------------------
' 4. Dynamic Array Manipulation
'----------------------------------------------------------
Private Sub DynamicArrayManipulation()
Dim numbers() As Integer
Dim size As Integer
size = 5
'----------------------------------------------------------
' 5. Array Searching and Sorting
'----------------------------------------------------------
Private Sub ArraySearchAndSort()
Dim numbers(9) As Integer
Dim i As Integer, j As Integer
For i = 0 To UBound(numbers)
If numbers(i) = searchValue Then
found = True
Debug.Print "Value " & searchValue & " found at index " & i
Exit For
End If
Next i
'----------------------------------------------------------
' 6. Array Utility Functions
'----------------------------------------------------------
Private Function FindMaxValue(arr() As Integer) As Integer
Dim maxVal As Integer
Dim i As Integer
For i = 1 To UBound(arr)
If arr(i) > maxVal Then
maxVal = arr(i)
End If
Next i
FindMaxValue = maxVal
End Function
For i = 1 To UBound(arr)
If arr(i) < minVal Then
minVal = arr(i)
End If
Next i
FindMinValue = minVal
End Function
'----------------------------------------------------------
' 7. Array Application Example: Simple Inventory System
'----------------------------------------------------------
Private Type Product
Name As String
Quantity As Integer
Price As Double
End Type
With inventory(1)
.Name = "Mouse"
.Quantity = 50
.Price = 29.99
End With
For i = 0 To UBound(inventory)
If inventory(i).Name <> "" Then ' Check if product exists
Dim itemTotal As Double
itemTotal = inventory(i).Quantity * inventory(i).Price
totalValue = totalValue + itemTotal