where to put this code

J

John Salerno

Here's what I have:

while (readSwitches.Peek() != -1)
{
int i;
string row = readSwitches.ReadLine();

for (int i = 0; i < 32; i++)
{
string[] switches;
switches += row.Split('|');
}

for (int z = 0; z < 8; z++)
{
i = 0;

for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 10; y++)
panelOne[x, y, z] = switches[i++];
}
}
}

Now, the problem here is that each time the while loop runs, it is still
using panelOne in the for loop. I need to change this to panelTwo,
Three, and Four each time the while loop iterates. Should this stuff be
placed out of the loop? Even still, how do I get it to use a different
'panel' array each time?
 
J

John Salerno

John said:
Here's what I have:

while (readSwitches.Peek() != -1)
{
int i;
string row = readSwitches.ReadLine();

for (int i = 0; i < 32; i++)
{
string[] switches;
switches += row.Split('|');
}

for (int z = 0; z < 8; z++)
{
i = 0;

for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 10; y++)
panelOne[x, y, z] = switches[i++];
}
}
}

Now, the problem here is that each time the while loop runs, it is still
using panelOne in the for loop. I need to change this to panelTwo,
Three, and Four each time the while loop iterates. Should this stuff be
placed out of the loop? Even still, how do I get it to use a different
'panel' array each time?

I just realized that the code won't compile. My original question
stands, but please forgive the sloppy code.
 
M

Marcus Andrén

John said:
Here's what I have:

while (readSwitches.Peek() != -1)
{
int i;
string row = readSwitches.ReadLine();

for (int i = 0; i < 32; i++)
{
string[] switches;
switches += row.Split('|');
}

for (int z = 0; z < 8; z++)
{
i = 0;

for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 10; y++)
panelOne[x, y, z] = switches[i++];
}
}
}

Now, the problem here is that each time the while loop runs, it is still
using panelOne in the for loop. I need to change this to panelTwo,
Three, and Four each time the while loop iterates. Should this stuff be
placed out of the loop? Even still, how do I get it to use a different
'panel' array each time?

I just realized that the code won't compile. My original question
stands, but please forgive the sloppy code.

You should place it in a method and and call it using the appropriate
panel as an argument. Something like this:

//
void Read(ReadSwitchClass readSwitch, PanelClass[,,] panel)
{
.. Your code goes here ..
}

You should probably choose a more descriptive method name though. And
when you need to use the code do

Read(readSwitches,panelOne) //or use panelTwo, panelThree,panelFour
 
J

John Salerno

Marcus said:
John said:
Here's what I have:

while (readSwitches.Peek() != -1)
{
int i;
string row = readSwitches.ReadLine();

for (int i = 0; i < 32; i++)
{
string[] switches;
switches += row.Split('|');
}

for (int z = 0; z < 8; z++)
{
i = 0;

for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 10; y++)
panelOne[x, y, z] = switches[i++];
}
}
}

Now, the problem here is that each time the while loop runs, it is still
using panelOne in the for loop. I need to change this to panelTwo,
Three, and Four each time the while loop iterates. Should this stuff be
placed out of the loop? Even still, how do I get it to use a different
'panel' array each time?

I just realized that the code won't compile. My original question
stands, but please forgive the sloppy code.


You should place it in a method and and call it using the appropriate
panel as an argument. Something like this:

//
void Read(ReadSwitchClass readSwitch, PanelClass[,,] panel)
{
.. Your code goes here ..
}

You should probably choose a more descriptive method name though. And
when you need to use the code do

Read(readSwitches,panelOne) //or use panelTwo, panelThree,panelFour

Thanks!
 
J

John Salerno

Oh wait, I was just thinking about this and I got stuck again. If I
place this method in a while loop, then how do I make it so that it
calls the method with a different parameter each time? Won't it just
call the same method each time?
 
G

Guest

You could just create an array that contains panelone at index 0, paneltwo at
index 1 etc so as you iterate through the loop you get the correct instance
of the panel.

Hope that helps.
Mark.
 
M

Marcus Andrén

Oh wait, I was just thinking about this and I got stuck again. If I
place this method in a while loop, then how do I make it so that it
calls the method with a different parameter each time? Won't it just
call the same method each time?

I think I misread your problem slightly. I thought you had multiple
while loops.

As Mark R. Dawson said, the easiest way is probably to put the
panelone..panelfour into an array and increase the index on each pass
through the loop.

Although not needed, I would still recommend that you put the panel
manipulation inside a new method. The code in the main method should
look something like this (I noted that your panels are strings
according to your earlier code):

string[][,,] panelArray = new string[4][,,];
panelArray[0] = panelOne;
panelArray[1] = panelTwo;
panelArray[2] = panelThree;
panelArray[3] = panelFour;

int panelIndex = 0;
while (readSwitches.Peek() != -1)
ReadIntoPanel(readSwitches,panelArray[panelIndex++]);

Note, that the following code will through an exception if
readSwitches contains data for more than 4 panels.

If you want code that handles any number of panels it should look like
this instead:

ArrayList panels = new ArrayList();
while (readSwitches.Peek() != -1)
{
string[,,] panel = new string[2,10,8];
ReadIntoPanel(readSwitches,panel);
panels.Add(panel);
}

//All panels are now stored in the ArrayList and can be accessed
int x = 0; //x can be anything from 0 to panels.Count-1
string[,,] panelX = (string[,,]) panels[x];
 
J

John Salerno

Marcus said:
string[][,,] panelArray = new string[4][,,];
panelArray[0] = panelOne;
panelArray[1] = panelTwo;
panelArray[2] = panelThree;
panelArray[3] = panelFour;

I considered using a jagged array, but I wasn't sure how to implement
it. If I wanted to use a jagged array for each panel, and each of these
arrays contains 8 arrays, it might be easier to access each "switch"
inside each "panel," right? Like:

panelOne[0][0,0] would be the first element of the first switch. Right
now I'm using a 3D array ([,,]) to store the 8 switches, but perhaps
with the separate array for each switch, it would be easier to pull out
a switch from a panel.

(Funny, every time I read a C# book, arrays are always what I dread
reading about because they can be so confusing, and now here I am diving
into them! I guess it was only a matter of time before I had to use
them!) :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top