|
| Author |
Message |
hailconan
Joined: 04 Oct 2007 Posts: 3
|
Posted: Wed Jul 19, 2006 3:46 am Post subject: Array problem |
|
|
Hi,
suppose I have Array in Visual Basic
dim k(100) as integer
dim d(5) as integer
how can I assign in one line 5 elements from "k" to "d " directly?
e.g:
d(1:5) = k(55:60)
THX
Archived from group: microsoft>public>vb>syntax |
|
| Back to top |
|
 |
Rick Rothstein
Joined: 04 Oct 2007 Posts: 1589
|
Posted: Wed Jul 19, 2006 8:19 am Post subject: Re: Array problem |
|
|
> suppose I have Array in Visual Basic
>
> dim k(100) as integer
> dim d(5) as integer
>
> how can I assign in one line 5 elements from "k" to "d " directly?
>
> e.g:
>
> d(1:5) = k(55:60)
Why do you think it is important to do this in one line... because another
language allows it? Just do the loop.
First off, 1:5 is 5 elements whereas 55:60 is 6 elements, so that is
problem. I presume you meant 55:59.
D_Start_Index = 1
K_Start_Index =55
NumberToTransfer = 5
For X = 0 To NumberToTransfer - 1
d(D_Start_Index + X) = k(K_Start_Index + X)
Next
Of course, don't forget to Dim your variables.
Rick |
|
| Back to top |
|
 |
hailconan
Joined: 04 Oct 2007 Posts: 3
|
Posted: Wed Jul 19, 2006 5:47 am Post subject: Re: Array problem |
|
|
Thank you,
because if it is assigned in one line it will be faster ... .
Rick Rothstein wrote:
> > suppose I have Array in Visual Basic
> >
> > dim k(100) as integer
> > dim d(5) as integer
> >
> > how can I assign in one line 5 elements from "k" to "d " directly?
> >
> > e.g:
> >
> > d(1:5) = k(55:60)
>
> Why do you think it is important to do this in one line... because another
> language allows it? Just do the loop.
>
> First off, 1:5 is 5 elements whereas 55:60 is 6 elements, so that is
> problem. I presume you meant 55:59.
>
> D_Start_Index = 1
> K_Start_Index =55
> NumberToTransfer = 5
> For X = 0 To NumberToTransfer - 1
> d(D_Start_Index + X) = k(K_Start_Index + X)
> Next
>
> Of course, don't forget to Dim your variables.
>
> Rick |
|
| Back to top |
|
 |
Rick Rothstein
Joined: 04 Oct 2007 Posts: 1589
|
Posted: Wed Jul 19, 2006 1:22 pm Post subject: Re: Array problem |
|
|
> because if it is assigned in one line it will be faster ... .
That is not always true. There are many situations in VB where a one-line
solution is slower (sometimes much slower) than an alternate multi-line
solution. All a one-line solution guarantees is that it will be faster for
you to type in, not that it will be faster for the program to execute. In
any event, the array slicing you were looking for (from FORTRAN, right?) has
no equivalent in VB.
Rick |
|
| Back to top |
|
 |
Jeff Johnson
Joined: 04 Oct 2007 Posts: 1327
|
Posted: Wed Jul 19, 2006 1:21 pm Post subject: Re: Array problem |
|
|
wrote in message @m73g2000cwd.googlegroups.com...
> because if it is assigned in one line it will be faster ... .
Faster for you to type, but for all you know it will be implemented with the
same machine instructions that would result from your writing a loop.
Regardless, VB simply doesn't support doing what you're describing. |
|
| Back to top |
|
 |
Bob Butler
Joined: 04 Oct 2007 Posts: 1325
|
Posted: Wed Jul 19, 2006 12:49 pm Post subject: Re: Array problem |
|
|
wrote in message@m73g2000cwd.googlegroups.com
> Hi,
>
> suppose I have Array in Visual Basic
>
> dim k(100) as integer
> dim d(5) as integer
>
> how can I assign in one line 5 elements from "k" to "d " directly?
>
> e.g:
>
> d(1:5) = k(55:60)
For a one-dimensional array of Integer values to a matching destination...
Declare Sub CopyMemory lib "kernel32" (byval dst as any,byval src as
any,byval kbytes as long)
CopyMemory d(1),k(55),10
For Strings or Objects and for multi-dimensional arrays there are other
considerations but for simple numeric types it'll work OK
--
Reply to the group so all can participate
VB.Net: "Fool me once..." |
|
| Back to top |
|
 |
Alcedes
Joined: 04 Oct 2007 Posts: 2
|
Posted: Fri Jul 28, 2006 10:33 pm Post subject: Re: Array problem |
|
|
Here ya go..One line.
For i = 1 To 5: d(0 + i) = k(55 + i): Next i
hailconan@gmail.com wrote:
>Hi,
>
>suppose I have Array in Visual Basic
>
>dim k(100) as integer
>dim d(5) as integer
>
>how can I assign in one line 5 elements from "k" to "d " directly?
>
>e.g:
>
>d(1:5) = k(55:60)
>
>THX |
|
| Back to top |
|
 |
argusy
Joined: 04 Oct 2007 Posts: 145
|
Posted: Sat Jul 29, 2006 5:22 pm Post subject: Re: Array problem |
|
|
and to make it even simpler
For i = 1 To 5: d(i) = k(55 + i): Next i
Alcedes wrote:
> Here ya go..One line.
>
> For i = 1 To 5: d(0 + i) = k(55 + i): Next i
>
>
Argusy
|
|
| Back to top |
|
 |
|