ArrayCopy function in VB (Run in ASP)

Posted on 2001-07-27
Last Modified: 2011-10-03
Is any array-related functions in VB such as copying an array?

Built-In functions will be perfect or i have to write a specific function.

Is it true that UBound function cannot be used while writing ASP scripts?
Question by:chnelson

Expert Comment

Comment Utility
Hi chnelcon

No you can use both lbound and ubound function in ASP. i donot think so that there is any function to copy the whole array. You have to make it by yourself.

The code is showing you the concept. Modify it as per your requiremnets.

Private Sub Command1_Click()
Dim A
A = Array(10, 20, 30)
Dim b
b = Array(45, 46, 57)
For i = 0 To 2
b = A(i)
MsgBox b
End Sub


lovneesh Bansal
LVL 22

Expert Comment

Comment Utility
UBound can be used. Just note that if the array has not been dimensioned that you will receive an error.

I am not aware of any build in function of copying an array to an array. So I have my own function. it will append the data from one array to the other array.

Public Sub AddArrayToArray(ByRef mArr As Variant, ByVal mArr2 As Variant)
On Error GoTo errHandler
    Dim iCount As Long, iCount2 As Long, SubPro As Long, iCounter As Long, iInt
    SubPro = 0
    iCount = UBound(mArr)
    SubPro = 1
    iCount2 = UBound(mArr2) + 1
    iInt = iCount + iCount2
    ReDim Preserve mArr(1 To iInt)
    For iCounter = iCount To (iInt - 1)
        mArr(iCounter + 1) = mArr2(iCounter - iCount)
    Exit Sub
    Select Case Err.Number
        Case 9:
            'set the array
            If (SubPro = 0) Then
                ReDim mArr(0)
            ElseIf (SubPro = 1) Then
                Exit Sub
            End If
        Case Else:
            Exit Sub
    End Select
    Resume 0
End Sub

Accepted Solution

nigelrowe earned 15 total points
Comment Utility
You can directly assign an array to another variable...

Private Sub Command1_Click()
Dim a, b, c
    a = Array(1, 2, 3)
    b = a
    If IsArray(b) Then
        For c = LBound(b) To UBound(b)
            MsgBox b(c)
    End If
End Sub

Expert Comment

Comment Utility
That works in VBScript as well.
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.


Expert Comment

Comment Utility
But, maybe, only from VB6 on. Heres the MSDN blurb for what's new in VB6...

Advanced Features of Arrays

Although arrays are most commonly used to store groups of variables, there are several other ways in which arrays are useful. You can assign the contents of one array to another, create functions that return arrays, and create properties that return arrays. In many cases these techniques can improve the performance of your application.

Assigning Arrays
Just as you can assign the contents of one variable to another, for example strA = strB, you can also assign the contents of one array to another. Imagine, for instance, that you wanted to copy an array of bytes from one location to another. You could do it by copying one byte at a time, like this:

Sub ByteCopy(oldCopy() As Byte, newCopy() As Byte)
   Dim i As Integer
   ReDim newCopy (Lbound(oldCopy) To UBound(oldCopy)

   For i ? Lbound(oldCopy) To Ubound(oldCopy)
      newCopy(i) = oldCopy(i)
End Sub

A much more efficient way to do this is to assign one array to another:

Sub ByteCopy(oldCopy() As Byte, newCopy() As Byte)
   newCopy = oldCopy
End Sub

With variable assignment there are certain rules that you need to keep in mind. For example, although you can assign a variable declared as Integer to a variable declared as Long without any problem, assigning a Long to an Integer could easily lead to an overflow error. In addition to data typing rules, array assignments have additional rules involving the number of dimensions, the size of those dimensions, and whether an array is fixed or dynamic.

Attempting to assign arrays with different dimensions and/or data types may or may not succeed, depending on several factors:

The type of array used on the left-hand side of the assignment: a fixed array (Dim x(1 to 10) As Integer) or a dynamic array (Dim x() As Integer).

Whether or not the number of dimensions on the left-hand side match the number of dimensions of the array on the right-hand side of the assignment.

Whether or not the number of elements for each dimension on each side of the assignment match. The dimensions may match even if the declarations are different, such as when one array is zero-based and another is one-based, as long as they have the same number of elements.

The data types of all elements for each side of the assignment must be compatible. The rules are the same as for variable assignments.
The following table shows the effects of these factors:

Left-hand Side Number of Dimensions Match? Number of Elements Match? Result of Assignment
Dynamic No Yes or No Succeeds. Left-hand side ReDim?s to match right-hand side if necessary.
Dynamic Yes No Succeeds. Left-hand side ReDim?s to match right-hand side if necessary.
Dynamic Yes Yes Succeeds.
Fixed Yes or No Yes or No Fails with a compilation error.

Errors can occur both at compile time and at run time (for example, if data types can?t be coerced or if an assignment attempts to ReDim a fixed size array.) As the programmer, it?s up to you to add error handling to make sure that the arrays are compatible before attempting an assignment.

Returning an Array from a Function
It's possible for a function to return an array of values. For example, you might want to return an array of bytes from a function without having to perform conversions to and from a string.

Here?s a simple example of a function that returns an array of bytes:

Private Sub Form_Load()
   Dim b As Byte
   Dim i As Integer
   Dim ReturnArray() As Byte

   b = Cbyte(54)
   ReturnArray() = ArrayFunction(b)
   For i = 0 To Ubound(ReturnArray)
      Debug.Print ReturnArray(i)
End Sub

Public Function ArrayFunction(b As Byte) As Byte()
   Dim x(2) As Byte

   x(0) = b
   x(1) = b + CByte(200)
   x(2) = b + b

   ArrayFunction = x  
End Function

After running the above example, ReturnArray() would be a three-element array containing the values assigned to the array in the ArrayFunction. Note that the array must be of the same data type as the function (in this case, Byte). Because this is a function call, you can pass the array without the parentheses.

Note   Although it?s possible to return the array by assigning another array (ArrayFunction = x()), this isn?t recommended for performance reasons.

You must specify a type for a function that returns an array; that type may be a Variant. Thus Function X() As Variant() would work whereas Function X() As () would fail.

When calling a function that returns an array, the variable to hold the return values must be an array and must be of the same data type as the function, otherwise it will display a "Type Mismatch" error.

For More Information   To learn more about using arrays, see "Arrays" in "Programming Fundamentals." For information about returning arrays from properties, see "Putting Property Procedures to Work for You" in "Programming with Objects."
LVL 26

Expert Comment

Comment Utility
Just one thing to point out.

Never use ubound or lbound on the for loop directly.

ie for i=1 to ubound(aa)

I read a article before that will cause memory leak if aa is not a array

Always use variable

ie. dim bb as integer
on error resume next
bb = ubound(aa)
if err.number then ...

Expert Comment

Comment Utility
If it is not an array, an error will be raised. So you should test if it is an array before...

If IsArray(a) Then
  For i = LBound(a) To UBound(a)
End If

Author Comment

Comment Utility
Thanks all guys giving so useful information.

Expert Comment

Comment Utility

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now