msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Array problem

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax
Author Message
hailconan



Joined: 04 Oct 2007
Posts: 3

PostPosted: Wed Jul 19, 2006 3:46 am    Post subject: Array problem Reply with quote

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
View user's profile Send private message
Rick Rothstein



Joined: 04 Oct 2007
Posts: 1589

PostPosted: Wed Jul 19, 2006 8:19 am    Post subject: Re: Array problem Reply with quote

> 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
View user's profile Send private message
hailconan



Joined: 04 Oct 2007
Posts: 3

PostPosted: Wed Jul 19, 2006 5:47 am    Post subject: Re: Array problem Reply with quote

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
View user's profile Send private message
Rick Rothstein



Joined: 04 Oct 2007
Posts: 1589

PostPosted: Wed Jul 19, 2006 1:22 pm    Post subject: Re: Array problem Reply with quote

> 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
View user's profile Send private message
Jeff Johnson



Joined: 04 Oct 2007
Posts: 1327

PostPosted: Wed Jul 19, 2006 1:21 pm    Post subject: Re: Array problem Reply with quote

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
View user's profile Send private message
Bob Butler



Joined: 04 Oct 2007
Posts: 1325

PostPosted: Wed Jul 19, 2006 12:49 pm    Post subject: Re: Array problem Reply with quote

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
View user's profile Send private message
Alcedes



Joined: 04 Oct 2007
Posts: 2

PostPosted: Fri Jul 28, 2006 10:33 pm    Post subject: Re: Array problem Reply with quote

Here ya go..One line. Smile

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
View user's profile Send private message
argusy



Joined: 04 Oct 2007
Posts: 145

PostPosted: Sat Jul 29, 2006 5:22 pm    Post subject: Re: Array problem Reply with quote

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. Smile
>
> For i = 1 To 5: d(0 + i) = k(55 + i): Next i
>
>


Argusy

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group