i have 2 arrray.
array2 have 10 elements
array 1 have 5 elements

i want to copy all the element of array1 to array2 to fill all the 10spaces..ie arra2 is filled 2times of array1 how it done.

array1={1,2,3,4,5}
now array2 is filled like(1,2,3,4,5,1,2,3,4,5}

need urgently plz help..

Dani AI

Generated

The goal in this thread is to fill a larger array by repeating a smaller array. Both and showed simple loop-based approaches; the recurring source of confusion is indexing and array bounds. In classic VB this can be affected by Option Base and by whether the declared bound is an upper index or a count. Using explicit bounds or LBound/UBound avoids off‑by‑one errors and makes code robust across environments.

A compact, index-safe pattern is to map each target index back into the source using the modulus operator. This works in VB.NET with zero-based arrays:

' VB.NET (zero-based)
Dim small() As Integer = {1,2,3,4,5}
Dim big(9) As Integer    ' 10 slots: 0..9
For i As Integer = 0 To big.Length - 1
    big(i) = small(i Mod small.Length)
Next

For VB6 or mixed-bound code, compute lengths with LBound/UBound so the code does not assume 0-based indexing:

' VB6-compatible (handles nonzero LBound)
Dim i As Long
For i = LBound(big) To UBound(big)
    big(i) = small((i - LBound(big)) Mod (UBound(small) - LBound(small) + 1) + LBound(small))
Next

Notes and troubleshooting: declare correct upper bounds (Dim a(4) for five elements in 0-based VB), or use UBound/LBound to compute sizes. For large or performance-sensitive copies, Array.Copy/Buffer.BlockCopy (VB.NET) or copying contiguous blocks is faster than element-by-element loops. When the target size changes at runtime, prefer dynamic collections (List(Of T) / ArrayList) or ReDim Preserve (VB6) with care for performance.

Recommended Answers

All 5 Replies

That is one way of doing it not knowing what you want to achieve:

    Dim array1(5) As Integer
    Dim array2(10) As Integer 'array2 is filled like(1,2,3,4,5,1,2,3,4,5}

Private Sub Form_Load()

    Dim i, j As Integer
    For i = 1 To 5
        array1(i) = i
    Next i
    For j = 1 To 5
        array2(j) = j
    Next j
End Sub

Private Sub Command1_Click()
    Dim k, l As Integer
    l = 0
    For k = 6 To 10
        l = l + 1
        array2(k) = array1(l)
    Next k
    For i = 1 To 10
        Debug.Print (array2(i))
    Next i
End Sub

Or

For i = 0 to 4
    array1(i) = i + 1
    array2(i) = i + 1
    array2(i+5) = i + 1
Next
For i = 0 to 4
    array1(i) = i + 1
    array2(i) = i + 1
    array2(i+5) = i + 1
Next

what is mean ...

If you have two arrays

Dim array1(5) As Integer
Dim array2(10) As Integer

then after the loop executes they will have the values

array1 = {1, 2, 3, 4, 5}
array2 = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5}

which is what you said you wanted.

To copy from array1 to array 2 do

For i = 0 To 4
    array2(i) = array1(i)
    array2(i+5) = array1(i)
Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.