Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya
 asked on

Handling arrays

Hi Experts,

I have a structure called BOX with 2 elements. ID and Status.
I have created an object
public NewBox(5) as box

After I process the first element, I want to overwrite  the Current element with the next element. Is there any easier way to do it or I have to run a loop and do it.

Thanks in advance.
Visual Basic.NETC#

Avatar of undefined
Last Comment
RadhaKrishnaKiJaya

8/22/2022 - Mon
Fernando Soto

Can you give more details so that we can determine what you are trying to do. If you have code that is currently doing it please post.
RadhaKrishnaKiJaya

ASKER
Thank u for trying to help me.

newBox(0).Status="Good"
NewBox(1).Status="BAD"
NewBox(2).Status="GOOD"
NewBox(3).Status="Empty"
NewBox(4).Status="Empty"

After I process newBox(0).Status="Good" I want to move NewBox(0).Status = NewBox(1).Status
NewBox(1).Status = NewBox(2).Status and so on.

So the output should be
newBox(0).Status="BAD"
NewBox(1).Status="GOOD"
NewBox(2).Status="Empty"
NewBox(3).Status="Empty"
NewBox(4).Status="Empty"
AndyAinscow

Do you know about a 'queue' ?  (Sounds a bit like you might be trying to reinvent it)

https://msdn.microsoft.com/en-us/library/system.collections.queue(v=vs.110).aspx
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
RadhaKrishnaKiJaya

ASKER
I have no idea what is the right way to achieve it.

Thanks.
ASKER CERTIFIED SOLUTION
Dmitry G

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
RadhaKrishnaKiJaya

ASKER
Hi,

Actually I am doing exactly the same thing what u r suggesting. I wanted to know if there is any other shortcut or command available instead of looping thru.

thanks.
Dmitry G

I'd say "no". You probably can use some LINQ statement but this is just "syntactic sugar" - it loops anyway behind scenes.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sarabande

I wanted to know if there is any other shortcut or command available instead of looping thru.

the alternate way to loops is recursion.


 
NewBox(0).Status="Good"
 NewBox(1).Status="BAD"
 NewBox(2).Status="GOOD"
 NewBox(3).Status="Empty"
 NewBox(4).Status="Empty"
 ProcessNewBox(NewBox, 0)

....
Sub ProcessNewBox(ByVal NewBox() As Box, ByVal Index As Long, ByVal Size As Long) 

    ' process NewBox(Index)
    
    ' check some end condition to avoid infinite calls

    ' clear the current slot or - better - find out next status 
    NewBox(Index).Status = "Empty"
    ....
    ' increment index (ring)
    Index = (Index + 1) Mod Size

    ' call recursively same function
    ProcessNewBox(NewBox, Index, Size)

End Sub

Open in new window

   

Sara
AndyAinscow

>>I want to overwrite  the Current element with the next element
Keeping the same ID is not replacing in my view.  If it is really replacing an element then it still sounds like a queue could be useful.  The current description sounds like
process A
copy B to A
copy C to B
copy D to C
copy E to D
(fill E ????)
back to start with Process A
Dmitry G

the alternate way to loops is recursion.

Recursion is good :). Not really faster than loops. And I remember when I tried to solve a graph problem  (years ago) with recursion - computer just died because lack of memory... :)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Dmitry G

like a queue could be useful

In a way - yes, no argue But still using a loop...
AndyAinscow

I'm thinking why do lots of copying when just taking away from the collection will automatically bring the next one to the top for processing.  No looping or recursion.
Anyway the person that can really know is the asker of the question.
sarabande

just taking away from the collection will automatically bring the next one to the top for processing.  No looping or recursion.

if the "next" is on top you either need a loop or recursion to process the next ...

as shown by the Asker, the last slot automatically should be initialized again and - probably - be used for further processing. i assume the intention of the array is to work as a "state machine" where there are rules how to change between states. if so, it can be implemented by using either a (polling) loop or recursion regardless of the container type used.

Sara
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
RadhaKrishnaKiJaya

ASKER
Thanks!!