Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 72
  • Last Modified:

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.
0
RadhaKrishnaKiJaya
Asked:
RadhaKrishnaKiJaya
  • 4
  • 4
  • 3
  • +2
1 Solution
 
Fernando SotoCommented:
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.
0
 
RadhaKrishnaKiJayaAuthor Commented:
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"
0
 
AndyAinscowCommented:
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
0
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!

 
RadhaKrishnaKiJayaAuthor Commented:
I have no idea what is the right way to achieve it.

Thanks.
0
 
anarki_jimbelCommented:
Again, it's not quite clear what has to be achieved. If we want to keep an ID but change status - queue probably is not the best option.

Also, when you process array elements you run a loop anyway, don't you? Why not use the same loop for changing status?
See an example:

namespace Boxes
{
        public struct Box
        {
            public int ID;
            public string Status;

            public Box(int _id, string _status)
            {
                ID = _id;
                Status = _status;
            }
        }

    class Program
    {

        static void Main(string[] args)
        {
            Box[] boxes = { new Box(0, "Good"), new Box(1, "Bad"), new Box(2, "Good"), new Box(3, "Empty"), new Box(4, "Empty") };

            // Process
            for (int counter = 0; counter < boxes.Length; counter++)
            {
                // Check old status
                Console.Write("Processing box with ID = " + boxes[counter].ID + "; Old status = " + boxes[counter].Status);
                if (counter == boxes.Length - 1)
                {
                    boxes[counter].Status = "Empty";
                }
                else
                {
                    boxes[counter].Status = boxes[counter + 1].Status;
                }
                // Check new status
                Console.WriteLine("; New status = " + boxes[counter].Status);
            }
            Console.ReadLine();
        }
    }
}

Open in new window



Output:
Processing box with ID = 0; Old status = Good; New status = Bad
Processing box with ID = 1; Old status = Bad; New status = Good
Processing box with ID = 2; Old status = Good; New status = Empty
Processing box with ID = 3; Old status = Empty; New status = Empty
Processing box with ID = 4; Old status = Empty; New status = Empty
0
 
RadhaKrishnaKiJayaAuthor Commented:
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.
0
 
anarki_jimbelCommented:
I'd say "no". You probably can use some LINQ statement but this is just "syntactic sugar" - it loops anyway behind scenes.
0
 
sarabandeCommented:
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
1
 
AndyAinscowCommented:
>>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
1
 
anarki_jimbelCommented:
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... :)
0
 
anarki_jimbelCommented:
like a queue could be useful

In a way - yes, no argue But still using a loop...
0
 
AndyAinscowCommented:
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.
0
 
sarabandeCommented:
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
0
 
RadhaKrishnaKiJayaAuthor Commented:
Thanks!!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now