?
Solved

Using Array - very urgent

Posted on 2003-03-13
14
Medium Priority
?
157 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:PaulMurugesan
[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
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 5

Accepted Solution

by:
TheMek earned 1000 total points
ID: 8134931
As far as I know, not without loops. And btw, I take it you mean you want to assing the values to arr2(0, x).
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 8134993
Do you want the VALUES or will the pointers to the Strings work?

0
 
LVL 3

Expert Comment

by:Gunsen
ID: 8135016
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.....
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:rdrunner
ID: 8135118
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
0
 
LVL 2

Expert Comment

by:uncle_med
ID: 8136023
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
0
 
LVL 2

Expert Comment

by:uncle_med
ID: 8136070
  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

0
 
LVL 3

Expert Comment

by:QJohnson
ID: 8136463
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).
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 8136673
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


0
 
LVL 3

Expert Comment

by:QJohnson
ID: 8136720
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.
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 8137185
Hehe ;)
0
 
LVL 2

Expert Comment

by:uncle_med
ID: 8137606
  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

0
 

Author Comment

by:PaulMurugesan
ID: 8150033
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
0
 

Author Comment

by:PaulMurugesan
ID: 8150201
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
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 8150339
My answer didnt use a loop....
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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 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