Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

ID: 6325871
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

ID: 6325873
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
ID: 6326002
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 6326004
That works in VBScript as well.

Expert Comment

ID: 6326012
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

ID: 6326498
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

ID: 6326527
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

ID: 6347740
Thanks all guys giving so useful information.

Expert Comment

ID: 6347827

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

860 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