|
-
Aug 7th, 2002, 05:03 PM
#1
Thread Starter
Member
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 ?
-
Aug 7th, 2002, 05:33 PM
#2
You could use an arraylist:
VB Code:
Dim arr1 As New ArrayList()
Dim arr2 As New ArrayList()
arr1.Add("goto")
arr1.Add("hell")
arr2.Add("and")
arr2.Add("back")
arr1.AddRange(arr2)
MsgBox(arr1(3))
-
Aug 7th, 2002, 11:06 PM
#3
Member
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.
-
Aug 8th, 2002, 04:22 AM
#4
Thread Starter
Member
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
-
Aug 8th, 2002, 11:00 AM
#5
I'm not sure I tried this:
VB Code:
Dim array3 As Array = Array.CreateInstance(GetType(System.String), 1)
Dim array4 As Array = Array.CreateInstance(GetType(System.String), 1)
array3.SetValue("goto", 0)
array3.SetValue("hell", 1)
[b]array4.SetValue("and", 0)[/b]
array4.SetValue("back", 1)
array4.CopyTo(array3, 2)
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.
-
Aug 8th, 2002, 11:03 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|