{"id":4378,"date":"2014-03-30T10:19:00","date_gmt":"2014-03-30T10:19:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/cant-loop-through-list-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:19:00","modified_gmt":"2014-03-30T10:19:00","slug":"cant-loop-through-list-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/cant-loop-through-list-collection-of-common-programming-errors\/","title":{"rendered":"Can&#39;t loop through List-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn <strong>Here&#8217;s the code:<\/strong><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>ProductList products = xxx.GetCarProducts(productCount);<\/strong><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 List imageList = new List(); \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>foreach(Product p in products)<\/strong><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 { \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 string imageTag = HttpUtility.HtmlEncode(string.Format(@&#8221;<img decoding=\"async\" src=\"\" alt=\"\" \/>&#8220;, ImageUrl(p.Image, false))); \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 imageList.Add(new CarImageList{ImageTag = imageTag}); \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i++; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/p>\n<p><strong>Under the covers ProductList is really defined like this:<\/strong><\/p>\n<p>\u00a0\u00a0\u00a0 public class <strong>ProductList<\/strong> : <strong>List<\/strong><\/p>\n<p>\u00a0\u00a0\u00a0 { \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 static Random rand = new Random(); \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 public ProductList Shuffle() \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 { \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 ProductList list = new ProductList(); \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 list.AddRange(this); \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 for (int i = 0; i &lt; list.Count; i++) \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 { \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 int r = rand.Next(list.Count &#8211; 1); \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 Product swap = list[i]; \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 list[i] = list[r]; \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 list[r] = swap; \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 } \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 return list; \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 } \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 public Product FindById(int id) \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 { \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 foreach (Product p in this) \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 if (p.Id == id) return p; \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 return null; \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 } \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 public Product GetRandom() \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 { \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 int r = rand.Next(this.Count &#8211; 1); \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 return this[r]; \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 } \u00a0\u00a0\u00a0 } so why do I get the error when I try to foreach through the ProductList instance? Cannot convert type &#8216;xxx.Product&#8217; to &#8216;Product&#8217; is the error I get.\u00a0 But if ProductList is really List why in the world would it have an issue with conversion? C# Web Developer<\/li>\n<li>\n<h3>5 Answers<\/h3>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn1 Hi NoEgo&#8230; maybe I&#8217;m missing something, but the only reason I could think of is that you have define a Product class twice. Right click and Go to Definition to see if both Product classes are the same. Other than that&#8230; on what line exatcly do you get the error? Can you post the GetCarProducts method? Regards,<\/p>\n<p>Fernando.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn2<\/p>\n<p>I know when I moused over Product I saw that it was the right namespace.\u00a0 But for some reason I still had to preface it with the namespace and walla, it&#8217;s resolved.\u00a0 Weird.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn3 Thanks.\u00a0 The product class is the same wherever referenced, stemming only from one Product.cs public ProductList GetCarProducts(string productCount) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 { \u00a0\u00a0 &#8230; }<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn4<\/p>\n<p>I know when I moused over Product I saw that it was the right namespace.\u00a0 But for some reason I still had to preface it with the namespace and walla, it&#8217;s resolved.\u00a0 Weird.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn5<\/p>\n<p>Are you by any chance loading Assemblies dynamically at runtime?<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>msdn Here&#8217;s the code: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ProductList products = xxx.GetCarProducts(productCount); \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 List imageList = new List(); \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach(Product p in products) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 { \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 string imageTag = HttpUtility.HtmlEncode(string.Format(@&#8221;&#8220;, ImageUrl(p.Image, false))); \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 imageList.Add(new CarImageList{ImageTag = imageTag}); \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i++; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } Under the covers ProductList is really defined like this: \u00a0\u00a0\u00a0 public class ProductList : [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4378","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4378"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4378\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}