Results 1 to 6 of 6

Thread: Add an Array to another Array

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    32

    Add an Array to another Array

    I want to add an array to another array.

    dim array1() as string
    dim array2() as string

    array1(0) = "goto"
    array1(1) = "hell"

    array2(0) = "and"
    array2(1) = "back"

    Now i want to add the contents of array1 into array2 without loop and independent from the array length. Is this possible ?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could use an arraylist:
    VB Code:
    1. Dim arr1 As New ArrayList()
    2.         Dim arr2 As New ArrayList()
    3.  
    4.         arr1.Add("goto")
    5.         arr1.Add("hell")
    6.         arr2.Add("and")
    7.         arr2.Add("back")
    8.  
    9.         arr1.AddRange(arr2)
    10.         MsgBox(arr1(3))

  3. #3
    Member
    Join Date
    Mar 2002
    Posts
    40
    Or, check out System.Array.Copy and arr1.CopyTo. Just note that with Copy to have to make sure that the destination array is large enough to hold the data that is being copied to it.

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    32
    OK, nice solution Edneeis,

    but if i use this i have to convert a 1-dim string-array into arraylist format. Do you have a easy wait to do this ?
    ( I prefer something without loop for better performance )

    Thanks

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I'm not sure I tried this:
    VB Code:
    1. Dim array3 As Array = Array.CreateInstance(GetType(System.String), 1)
    2.         Dim array4 As Array = Array.CreateInstance(GetType(System.String), 1)
    3.  
    4.         array3.SetValue("goto", 0)
    5.         array3.SetValue("hell", 1)
    6.  
    7.         [b]array4.SetValue("and", 0)[/b]
    8.         array4.SetValue("back", 1)
    9.  
    10.         array4.CopyTo(array3, 2)
    11.  
    12.         MsgBox(array3.GetValue(2))

    But I get an IndexOutOfBoundsException on the bolded line???? Which makes absolutely no sense to me. If anyway wants to expalin it feel free.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Either way I think you are either going to have to convert or use something other than the string type either Array or ArrayList or you will end up looping.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width