{"id":107,"date":"2007-10-30T18:26:00","date_gmt":"2007-10-30T18:26:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2007\/10\/30\/cannot-modify-members-of-item-because-it-is-a-foreach-iteration-variable\/"},"modified":"2007-10-30T18:26:00","modified_gmt":"2007-10-30T18:26:00","slug":"cannot-modify-members-of-item-because-it-is-a-foreach-iteration-variable","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2007\/10\/30\/cannot-modify-members-of-item-because-it-is-a-foreach-iteration-variable\/","title":{"rendered":"Cannot modify members of &#8216;item&#8217; because it is a &#8216;foreach iteration variable&#8217;"},"content":{"rendered":"<div>\n<p><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\"; color:black'>&nbsp;I had a compile time issue with using my custom list. The code simply looks like the following:<o:p><\/o:p><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\"; color:black'><o:p>&nbsp;<\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt; font-family:\"Courier New\";color:blue'>foreach<\/span><span style='font-size: 10.0pt;font-family:\"Courier New\";color:black'> (ItemType item <\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>in<\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:black'> listOfItems)<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt; font-family:\"Courier New\";color:black'>&nbsp;&nbsp;&nbsp; item.CustomAccessor = &quot;<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"; color:teal'>value<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"; color:black'>&quot;;<o:p><\/o:p><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\"; color:black'><o:p>&nbsp;<\/o:p><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\"; color:black'>And compiler complains with error:<o:p><\/o:p><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\"; color:black'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\";color:red'>Cannot modify members of &#8216;item&#8217; because it is a &#8216;foreach iteration variable&#8217;<o:p><\/o:p><\/span><\/p>\n<p><span style='color:black'><o:p>&nbsp;<\/o:p><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\"; color:black'>This is because the ItemType is defined as a&nbsp; <\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>struct<\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:black'> <\/span><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\";color:black'>and not as a <\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>class<\/span><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\";color:black'>. Doing so the iteration of <\/span><span style='font-size:10.0pt;font-family:\"Courier New\"; color:blue'>foreach<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"; color:black'> <\/span><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\"; color:black'>will result to some copies of the heap that are not reflecting the real objects. The question that has often been asked is why did you define your ItemType as struct and not as a class&#8230;. which I will answer it in other discussion.<o:p><\/o:p><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\"; color:black'>&nbsp;&nbsp;<o:p><\/o:p><\/span><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;I had a compile time issue with using my custom list. The code simply looks like the following: &nbsp; foreach (ItemType item in listOfItems) &nbsp;&nbsp;&nbsp; item.CustomAccessor = &quot;value&quot;; &nbsp; And compiler complains with error: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cannot modify members of &#8216;item&#8217; because it is a &#8216;foreach iteration variable&#8217; &nbsp; This is because the ItemType is defined &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2007\/10\/30\/cannot-modify-members-of-item-because-it-is-a-foreach-iteration-variable\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cannot modify members of &#8216;item&#8217; because it is a &#8216;foreach iteration variable&#8217;&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"&nbsp;I had a compile time issue with using my custom list. The code simply looks like the following: &nbsp; foreach (ItemType item in listOfItems) &nbsp;&nbsp;&nbsp; item.CustomAccessor = &quot;value&quot;; &nbsp; And compiler complains with error: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cannot modify members of &#8216;item&#8217; because it is a &#8216;foreach iteration variable&#8217; &nbsp; This is because the ItemType is defined&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/107"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}