Solved

Handling arrays

Posted on 2016-10-13
14
33 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 32

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 32

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

17 Experts available now in Live!

Get 1:1 Help Now