PaulMurugesan
asked on
Using Array - very urgent
Dear friends'
i have two arrays one dimensioned and two dimensioned
Dim arr1(2)
Dim arr2(0, 2)
arr1(0) = "one"
arr1(1) = "two"
arr1(2) = "three"
i want to assign all the values in arr1 to arr2(0,0) directly without going through loop. Is it possible. In java it is possible if there is any way to do it in vb?
thanks
Paul
i have two arrays one dimensioned and two dimensioned
Dim arr1(2)
Dim arr2(0, 2)
arr1(0) = "one"
arr1(1) = "two"
arr1(2) = "three"
i want to assign all the values in arr1 to arr2(0,0) directly without going through loop. Is it possible. In java it is possible if there is any way to do it in vb?
thanks
Paul
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Do you want the VALUES or will the pointers to the Strings work?
Not possible with VB 6, but in VB.net
In form load:
arr1 = Split("one","two","three")
For the 2-dim array i would prefer to make a function, that have to have loops.....
In form load:
arr1 = Split("one","two","three")
For the 2-dim array i would prefer to make a function, that have to have loops.....
If you are satisfied with the Pointers you can try this :
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)
Dim ar1(2) As String
Dim ar2(0, 2) As String
Private Sub Form_Load()
ar1(0) = "ONE"
ar1(1) = "Two"
ar1(2) = "Three"
CopyMemory ByVal VarPtr(ar2(0, 0)), ByVal VarPtr(ar1(0)), (UBound(ar1) + 1) * 4
End Sub
Carefull warning... If you want to redim ar1 at any time afterwards you need to Void all strings in there manually
Do this by calling
zeromemory byval varptr(ar1(0)), (ubound(ar1)+1)*4
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)
Dim ar1(2) As String
Dim ar2(0, 2) As String
Private Sub Form_Load()
ar1(0) = "ONE"
ar1(1) = "Two"
ar1(2) = "Three"
CopyMemory ByVal VarPtr(ar2(0, 0)), ByVal VarPtr(ar1(0)), (UBound(ar1) + 1) * 4
End Sub
Carefull warning... If you want to redim ar1 at any time afterwards you need to Void all strings in there manually
Do this by calling
zeromemory byval varptr(ar1(0)), (ubound(ar1)+1)*4
Paul
You can as long as you declare arr2 as a variant. Try this:
Dim arr1(2)
Dim arr2
arr1(0) = "one"
arr1(1) = "two"
arr1(2) = "three"
'
' Assign array to variable. Arr2 becomes an array
'
arr2 = arr1
Hope it helps,
Med
You can as long as you declare arr2 as a variant. Try this:
Dim arr1(2)
Dim arr2
arr1(0) = "one"
arr1(1) = "two"
arr1(2) = "three"
'
' Assign array to variable. Arr2 becomes an array
'
arr2 = arr1
Hope it helps,
Med
This one works too, but all values are assigned to the 0th element of arr2. So effectively you end up with an array hanging off the 0th element of arr2.
Remember it only works, because it's a variant array.
Dim arr1(2)
Dim arr2(0, 2)
arr1(0) = "one"
arr1(1) = "two"
arr1(2) = "three"
arr2(0, 0) = arr1
Hope it helps,
Med
Remember it only works, because it's a variant array.
Dim arr1(2)
Dim arr2(0, 2)
arr1(0) = "one"
arr1(1) = "two"
arr1(2) = "three"
arr2(0, 0) = arr1
Hope it helps,
Med
What value to you want in Arr2(0,0)?
Are you trying for "one,two,three"? Just use:
Arr2(0,0) = Join(Arr1,",")
This works with both arrays declared as Strings OR as Variants (as you have shown by not naming another type)
If you want something else, please show us.
-------------
Please note, the Split() function is certainly available in VB6; not just .NET. It is also available in Access2K VBA. So you could use it to put those values from Arr2(0,0) back into Arr1 with:
Arr1 = Split(Arr2(0,0), ",")
PROVIDED that (a) arr1 is declared dynamically and (b) both arrays are declared as type String. You cannot assign the return of a Split() function call to a static array (perhaps you can do this in .NET and this may have been the poster's intent).
Are you trying for "one,two,three"? Just use:
Arr2(0,0) = Join(Arr1,",")
This works with both arrays declared as Strings OR as Variants (as you have shown by not naming another type)
If you want something else, please show us.
-------------
Please note, the Split() function is certainly available in VB6; not just .NET. It is also available in Access2K VBA. So you could use it to put those values from Arr2(0,0) back into Arr1 with:
Arr1 = Split(Arr2(0,0), ",")
PROVIDED that (a) arr1 is declared dynamically and (b) both arrays are declared as type String. You cannot assign the return of a Split() function call to a static array (perhaps you can do this in .NET and this may have been the poster's intent).
I THINK he wants this
ar1(0) -> ar2(0,0)
ar1(1) -> ar2(0,1)
ar1(2) -> ar2(0,2)
If thats the case then a memory copy will do the trick...
If he wants this
ar2(0,0) = "one two three"
then it can be solved by a JOIN
ar2(0,0) = join(ar1," ")
STILL another option would be this
(if AR2 is a variant)
ar2(0,0) = ar1
->
debug.? ar2(0,0)(0)
-> one
ar1(0) -> ar2(0,0)
ar1(1) -> ar2(0,1)
ar1(2) -> ar2(0,2)
If thats the case then a memory copy will do the trick...
If he wants this
ar2(0,0) = "one two three"
then it can be solved by a JOIN
ar2(0,0) = join(ar1," ")
STILL another option would be this
(if AR2 is a variant)
ar2(0,0) = ar1
->
debug.? ar2(0,0)(0)
-> one
I've played that "I think he wants" game far too often with clients. I'm sure not doing it here <g>. Let's see how he responds.
Hehe ;)
This one works too, but all values are assigned to the 0th element of arr2. So effectively you end up with an array hanging off the 0th element of arr2.
Remember it only works, because it's a variant array.
Dim arr1(2)
Dim arr2(0, 2)
arr1(0) = "one"
arr1(1) = "two"
arr1(2) = "three"
arr2(0, 0) = arr1
Hope it helps,
Med
Remember it only works, because it's a variant array.
Dim arr1(2)
Dim arr2(0, 2)
arr1(0) = "one"
arr1(1) = "two"
arr1(2) = "three"
arr2(0, 0) = arr1
Hope it helps,
Med
ASKER
Dear friends
from the answers u have provided I think it is not possible to do that. with Loops it takes more time so i want to find out whether any possible ways of adding values to array without Loop.
I want solutions in VB not in vb.net
--Paul
from the answers u have provided I think it is not possible to do that. with Loops it takes more time so i want to find out whether any possible ways of adding values to array without Loop.
I want solutions in VB not in vb.net
--Paul
ASKER
Dear friends
from the answers u have provided I think it is not possible to do that. with Loops it takes more time so i want to find out whether any possible ways of adding values to array without Loop.
I want solutions in VB not in vb.net
--Paul
from the answers u have provided I think it is not possible to do that. with Loops it takes more time so i want to find out whether any possible ways of adding values to array without Loop.
I want solutions in VB not in vb.net
--Paul
My answer didnt use a loop....