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. How to modify the foreach identifier?

How to modify the foreach identifier?

Scheduled Pinned Locked Moved C#
helptutorialquestion
3 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.
  • B Offline
    B Offline
    blackhattrick
    wrote on last edited by
    #1

    I have several PictureBox Controls in my Form. I want to change the Image property of one of them depending on its Tag property. This is my code: foreach(Control ctrl in this.controls) { if(ctrl.GetType().ToString() == "System.Windows.Form.PictureBox") { System.Windows.Form.PictureBox temPic = (System.Windows.Form.PictureBox)ctrl; if(tempPic.Tag="1") { tempPic.Image = someImg; //an image object ctrl = tempPic; } } } Doing this I get an error: Cannot assign to 'ctrl' because it is a 'foreach iteration variable' Is there any workaround for this or any ideas in order to achive this goal? Any help would be apreciated :) Ivan

    L 1 Reply Last reply
    0
    • B blackhattrick

      I have several PictureBox Controls in my Form. I want to change the Image property of one of them depending on its Tag property. This is my code: foreach(Control ctrl in this.controls) { if(ctrl.GetType().ToString() == "System.Windows.Form.PictureBox") { System.Windows.Form.PictureBox temPic = (System.Windows.Form.PictureBox)ctrl; if(tempPic.Tag="1") { tempPic.Image = someImg; //an image object ctrl = tempPic; } } } Doing this I get an error: Cannot assign to 'ctrl' because it is a 'foreach iteration variable' Is there any workaround for this or any ideas in order to achive this goal? Any help would be apreciated :) Ivan

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, your code doesn't make much sense: 1. the statement ctrl = tempPic; would do no good since tempPic already equals ctrl due to a previous assignment. 2. why set a new value to ctrl, it is not used anywhere. (if you were to use PRE tags, see the "code block" button, you would have preserved the formatting of your code, making things much easier to read and understand). BTW1: if(ctrl.GetType().ToString() == "System.Windows.Form.PictureBox")... is horrible; what you probably want is: if (ctrl is PictureBox)... or the "as" keyword (see below). BTW2: your code will not compile, e.g. this.controls and ...Tag="1" are wrong Maybe this is what you want overall:

      foreach(Control ctrl in Controls) {
      PictureBox pb=ctrl as PictureBox;
      if(pb!=null && pb.Tag=="1") pb.Image=someImg;
      }

      I would like to suggest you buy a tutorial book on C# and study it; that will teach you all the basics in a structured and logical way, explaining the rationale and providing good examples. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      S 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, your code doesn't make much sense: 1. the statement ctrl = tempPic; would do no good since tempPic already equals ctrl due to a previous assignment. 2. why set a new value to ctrl, it is not used anywhere. (if you were to use PRE tags, see the "code block" button, you would have preserved the formatting of your code, making things much easier to read and understand). BTW1: if(ctrl.GetType().ToString() == "System.Windows.Form.PictureBox")... is horrible; what you probably want is: if (ctrl is PictureBox)... or the "as" keyword (see below). BTW2: your code will not compile, e.g. this.controls and ...Tag="1" are wrong Maybe this is what you want overall:

        foreach(Control ctrl in Controls) {
        PictureBox pb=ctrl as PictureBox;
        if(pb!=null && pb.Tag=="1") pb.Image=someImg;
        }

        I would like to suggest you buy a tutorial book on C# and study it; that will teach you all the basics in a structured and logical way, explaining the rationale and providing good examples. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        S Offline
        S Offline
        Samer Aburabie
        wrote on last edited by
        #3

        You left nothing for him :)

        Sincerely Samer Abu Rabie Imagination is more important than knowledge !

        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