Solved

Handling arrays

Posted on 2016-10-13
14
40 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 62

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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now