Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Array Shift Register

Array Shift Register

Scheduled Pinned Locked Moved C#
data-structureshelpquestionannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cmarcus
    wrote on last edited by
    #1

    I've got a very large array of picture boxes that I'm trying to shift with out using a loop. I've tried looping through the array from last element to first, setting the prior element to the current element, however this takes too long. Basically I want to take the properties from elements [0] to [array length - 1] and set them to elements [1] to [array length]. And then take element [0] and update it programically. I'm trying to use the Array.CopyTo function, where I copy to a temp array but haven't had any success. Can anyone help!?!?

    G 1 Reply Last reply
    0
    • C cmarcus

      I've got a very large array of picture boxes that I'm trying to shift with out using a loop. I've tried looping through the array from last element to first, setting the prior element to the current element, however this takes too long. Basically I want to take the properties from elements [0] to [array length - 1] and set them to elements [1] to [array length]. And then take element [0] and update it programically. I'm trying to use the Array.CopyTo function, where I copy to a temp array but haven't had any success. Can anyone help!?!?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      I don't see how looping through the array could be taking too long? Do you have several million items in the array, or what? Besides, whatever method you try to use instead of a loop will still be using a loop in the end. You will only be wasting resources trying to avoid the inevitable. Could you show some of your code? That might give a clue to what the problem is.

      --- single minded; short sighted; long gone;

      C 1 Reply Last reply
      0
      • G Guffa

        I don't see how looping through the array could be taking too long? Do you have several million items in the array, or what? Besides, whatever method you try to use instead of a loop will still be using a loop in the end. You will only be wasting resources trying to avoid the inevitable. Could you show some of your code? That might give a clue to what the problem is.

        --- single minded; short sighted; long gone;

        C Offline
        C Offline
        cmarcus
        wrote on last edited by
        #3

        Here's the sample code for what I'm trying to do. I actually have a 2D array. It's an array of pictureBox arrays. If you picture it like a grid with rows and columns. I want to foreach row, I want to shift each column back one, and set the first element to a new image. Here's the code (two for loops) // Shift the lane to the right // // Loop through each array (row) // for(int i = 0; i < allLanes.Length; i++) { // Loop through each element in the array (column) // // From back to front // for(int j = allLanes[i].Length-1; j > 0; j--) { // If the previous element's image is the failed image // if(allLanes[i][j-1].Image == failImg) { // Set the previous element to a pass image // allLanes[i][j-1].Image = passImg; // Set the current elemtn to a fail image // allLanes[i][j].Image = failImg; } else { // Set the current element to a pass image // allLanes[i][j].Image = passImg; } // Thread.Sleep(1); // } Note the Thread.Sleep(1). If I remove this, the code start to bomb out saying either: System.Drawing element is in use elsewhere or sometimes the display simply doesn't finish updating with only some of the rows showing the shift.

        M 1 Reply Last reply
        0
        • C cmarcus

          Here's the sample code for what I'm trying to do. I actually have a 2D array. It's an array of pictureBox arrays. If you picture it like a grid with rows and columns. I want to foreach row, I want to shift each column back one, and set the first element to a new image. Here's the code (two for loops) // Shift the lane to the right // // Loop through each array (row) // for(int i = 0; i < allLanes.Length; i++) { // Loop through each element in the array (column) // // From back to front // for(int j = allLanes[i].Length-1; j > 0; j--) { // If the previous element's image is the failed image // if(allLanes[i][j-1].Image == failImg) { // Set the previous element to a pass image // allLanes[i][j-1].Image = passImg; // Set the current elemtn to a fail image // allLanes[i][j].Image = failImg; } else { // Set the current element to a pass image // allLanes[i][j].Image = passImg; } // Thread.Sleep(1); // } Note the Thread.Sleep(1). If I remove this, the code start to bomb out saying either: System.Drawing element is in use elsewhere or sometimes the display simply doesn't finish updating with only some of the rows showing the shift.

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello, You shouldn't access the Images and Lanes allways over the index ([i] or [i][j-1]). Do it once and hold the reference as long as you need it. This can also be a performance improfement. All the best, Martin

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups