Solved

ArrayCopy function in VB (Run in ASP)

Posted on 2001-07-27
9
620 Views
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?
0
Comment
Question by:chnelson
[X]
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
9 Comments
 
LVL 1

Expert Comment

by:loveneesh_bansal
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
Next
End Sub

Bye

lovneesh Bansal
0
 
LVL 22

Expert Comment

by:CJ_S
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)
    Next
   
    Exit Sub
errHandler:
    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
    Err.Clear
    Resume 0
End Sub
0
 
LVL 3

Accepted Solution

by:
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)
        Next
    End If
End Sub
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 3

Expert Comment

by:nigelrowe
ID: 6326004
That works in VBScript as well.
0
 
LVL 3

Expert Comment

by:nigelrowe
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)
   Next
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)
   Next
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."
0
 
LVL 26

Expert Comment

by:EDDYKT
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 ...
0
 
LVL 3

Expert Comment

by:nigelrowe
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)
    ...
  Next
End If
0
 
LVL 1

Author Comment

by:chnelson
ID: 6347740
Thanks all guys giving so useful information.
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6347827
Cheers.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

623 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