where to put this code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    where to put this code

    Here's what I have:

    while (readSwitches.P eek() != -1)
    {
    int i;
    string row = readSwitches.Re adLine();

    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?
  • John Salerno

    #2
    Re: where to put this code

    John Salerno wrote:[color=blue]
    > Here's what I have:
    >
    > while (readSwitches.P eek() != -1)
    > {
    > int i;
    > string row = readSwitches.Re adLine();
    >
    > 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?[/color]

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

    Comment

    • Marcus Andrén

      #3
      Re: where to put this code

      On Fri, 11 Nov 2005 22:10:54 -0500, John Salerno
      <johnjsal@NOSPA Mgmail.com> wrote:
      [color=blue]
      >John Salerno wrote:[color=green]
      >> Here's what I have:
      >>
      >> while (readSwitches.P eek() != -1)
      >> {
      >> int i;
      >> string row = readSwitches.Re adLine();
      >>
      >> 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?[/color]
      >
      >I just realized that the code won't compile. My original question
      >stands, but please forgive the sloppy code.[/color]

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

      //
      void Read(ReadSwitch Class 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(readSwitch es,panelOne) //or use panelTwo, panelThree,pane lFour

      --
      Marcus Andrén

      Comment

      • John Salerno

        #4
        Re: where to put this code

        Marcus Andrén wrote:[color=blue]
        > On Fri, 11 Nov 2005 22:10:54 -0500, John Salerno
        > <johnjsal@NOSPA Mgmail.com> wrote:
        >
        >[color=green]
        >>John Salerno wrote:
        >>[color=darkred]
        >>>Here's what I have:
        >>>
        >>>while (readSwitches.P eek() != -1)
        >>> {
        >>> int i;
        >>> string row = readSwitches.Re adLine();
        >>>
        >>> 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?[/color]
        >>
        >>I just realized that the code won't compile. My original question
        >>stands, but please forgive the sloppy code.[/color]
        >
        >
        > You should place it in a method and and call it using the appropriate
        > panel as an argument. Something like this:
        >
        > //
        > void Read(ReadSwitch Class 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(readSwitch es,panelOne) //or use panelTwo, panelThree,pane lFour
        >
        > --
        > Marcus Andrén[/color]

        Thanks!

        Comment

        • John Salerno

          #5
          Re: where to put this code

          > Marcus Andrén wrote:
          [color=blue][color=green]
          >>You should place it in a method and and call it using the appropriate
          >>panel as an argument. Something like this:[/color][/color]

          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?

          Comment

          • Mark R. Dawson

            #6
            Re: where to put this code

            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.

            "John Salerno" wrote:
            [color=blue][color=green]
            > > Marcus Andrén wrote:[/color]
            >[color=green][color=darkred]
            > >>You should place it in a method and and call it using the appropriate
            > >>panel as an argument. Something like this:[/color][/color]
            >
            > 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?
            >[/color]

            Comment

            • Marcus Andrén

              #7
              Re: where to put this code

              On Sat, 12 Nov 2005 12:58:57 -0500, John Salerno
              <johnjsal@NOSPA Mgmail.com> wrote:
              [color=blue][color=green]
              >> Marcus Andrén wrote:[/color]
              >[color=green][color=darkred]
              >>>You should place it in a method and and call it using the appropriate
              >>>panel as an argument. Something like this:[/color][/color]
              >
              >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?[/color]

              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..panel four 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.P eek() != -1)
              ReadIntoPanel(r eadSwitches,pan elArray[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.P eek() != -1)
              {
              string[,,] panel = new string[2,10,8];
              ReadIntoPanel(r eadSwitches,pan el);
              panels.Add(pane l);
              }

              //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];

              --
              Marcus Andrén














              Comment

              • John Salerno

                #8
                Re: where to put this code

                Marcus Andrén wrote:
                [color=blue]
                > string[][,,] panelArray = new string[4][,,];
                > panelArray[0] = panelOne;
                > panelArray[1] = panelTwo;
                > panelArray[2] = panelThree;
                > panelArray[3] = panelFour;[/color]

                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!) :)

                Comment

                Working...