Solved

Handling arrays

Posted on 2016-10-13
14
53 Views
Last Modified: 2016-10-14
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
Comment
Question by:RadhaKrishnaKiJaya
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41842385
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
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41842406
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41842439
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41842445
I have no idea what is the right way to achieve it.

Thanks.
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 41842558
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
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41842630
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
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 41842642
I'd say "no". You probably can use some LINQ statement but this is just "syntactic sugar" - it loops anyway behind scenes.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 41843173
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41843187
>>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
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 41843192
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
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 41843196
like a queue could be useful

In a way - yes, no argue But still using a loop...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41843209
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
 
LVL 33

Expert Comment

by:sarabande
ID: 41843249
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
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 41843846
Thanks!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

808 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