Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Handling arrays

Posted on 2016-10-13
14
Medium Priority
?
69 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 64

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

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

Thanks.
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 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 30

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 35

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 30

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 30

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 35

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

722 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