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. Converting Array To Dictionary

Converting Array To Dictionary

Scheduled Pinned Locked Moved C#
data-structureshelp
9 Posts 7 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.
  • M Offline
    M Offline
    MadDashCoder
    wrote on last edited by
    #1

    I am trying to convert an array to a dictionary but my code is throwing a null exception. Below is my code

        static void Main(string\[\] args)
        {  
            Product p1 = new Product();
            p1.ProductSkew = 99;
            p1.ProductName = "Accord";
            p1.Color = "Black";
            p1.Price = 35000;
    
            Product p2 = new Product();
            p2.ProductSkew = 46;
            p2.ProductName = "Corvette";
            p2.Color = "Black";
            p2.Price = 65000;
    
            Product p3 = new Product();
            p3.ProductSkew = 35;
            p3.ProductName = "Altima";
            p3.Color = "Black";
            p3.Price = 32000;
            
            Product\[\] products = new Product\[3\];
    
            products\[0\] = p1;
            products\[1\] = p2;
            products\[2\] = p3;
    
            Dictionary productlist = products.ToDictionary(p => p.ProductSkew, p => p);
    
            foreach (KeyValuePair item in productlist)
            {
                Product product = new Product();
                product = item.Value;
    
                Console.WriteLine(
                "\\nSkew: {0} \\n" + "Name: {1} \\n" +  "Color: {2} \\n" +  "Price: {3} \\n",
                product.ProductSkew, product.ProductName, product.Color, product.Price );                
            }
            Console.ReadLine();
        }
    }
    
    public class Product
    {
        public int ProductSkew { get; set; }
        public string ProductName { get; set; }        
        public string Color { get; set; }
        public double Price { get; set; }
    
        public void AddProduct()
        { 
            Console.WriteLine("Enter Product Skew.");
            this.ProductSkew = Convert.ToInt32(Console.ReadLine());
    
            Console.WriteLine("Enter Product Name.");
            this.ProductName = Console.ReadLine();
    
            Console.WriteLine("Enter Product Color.");
            this.Color = Console.ReadLine();
    
            Console.WriteLine("Enter Product Price.");
            this.Price = Convert.ToDouble(Console.ReadLine());
        }
    }
    

    The syntax seems correct but when I step through the code inside of ToDictionay(), p.ProduckSkew is empty and I don't know why. Any help is greatly appreciated, thanks.

    J S Richard DeemingR L 4 Replies Last reply
    0
    • M MadDashCoder

      I am trying to convert an array to a dictionary but my code is throwing a null exception. Below is my code

          static void Main(string\[\] args)
          {  
              Product p1 = new Product();
              p1.ProductSkew = 99;
              p1.ProductName = "Accord";
              p1.Color = "Black";
              p1.Price = 35000;
      
              Product p2 = new Product();
              p2.ProductSkew = 46;
              p2.ProductName = "Corvette";
              p2.Color = "Black";
              p2.Price = 65000;
      
              Product p3 = new Product();
              p3.ProductSkew = 35;
              p3.ProductName = "Altima";
              p3.Color = "Black";
              p3.Price = 32000;
              
              Product\[\] products = new Product\[3\];
      
              products\[0\] = p1;
              products\[1\] = p2;
              products\[2\] = p3;
      
              Dictionary productlist = products.ToDictionary(p => p.ProductSkew, p => p);
      
              foreach (KeyValuePair item in productlist)
              {
                  Product product = new Product();
                  product = item.Value;
      
                  Console.WriteLine(
                  "\\nSkew: {0} \\n" + "Name: {1} \\n" +  "Color: {2} \\n" +  "Price: {3} \\n",
                  product.ProductSkew, product.ProductName, product.Color, product.Price );                
              }
              Console.ReadLine();
          }
      }
      
      public class Product
      {
          public int ProductSkew { get; set; }
          public string ProductName { get; set; }        
          public string Color { get; set; }
          public double Price { get; set; }
      
          public void AddProduct()
          { 
              Console.WriteLine("Enter Product Skew.");
              this.ProductSkew = Convert.ToInt32(Console.ReadLine());
      
              Console.WriteLine("Enter Product Name.");
              this.ProductName = Console.ReadLine();
      
              Console.WriteLine("Enter Product Color.");
              this.Color = Console.ReadLine();
      
              Console.WriteLine("Enter Product Price.");
              this.Price = Convert.ToDouble(Console.ReadLine());
          }
      }
      

      The syntax seems correct but when I step through the code inside of ToDictionay(), p.ProduckSkew is empty and I don't know why. Any help is greatly appreciated, thanks.

      J Offline
      J Offline
      John Torjo
      wrote on last edited by
      #2

      This is weird. Just copy-pasted your code. - VS 2013 - .net 4.5 - debug and release Works as expected. Best, John

      -- Log Wizard - a Log Viewer that is easy and fun to use!

      M 1 Reply Last reply
      0
      • J John Torjo

        This is weird. Just copy-pasted your code. - VS 2013 - .net 4.5 - debug and release Works as expected. Best, John

        -- Log Wizard - a Log Viewer that is easy and fun to use!

        M Offline
        M Offline
        MadDashCoder
        wrote on last edited by
        #3

        Hi, thanks for replying. I think I know what the problem is. In my actual code I set the size of the array to 10 but inserted data into only three of the elements. So my question is how do I test for null values when converting an array to a dictionary. Thanks for your help.

        M M 2 Replies Last reply
        0
        • M MadDashCoder

          Hi, thanks for replying. I think I know what the problem is. In my actual code I set the size of the array to 10 but inserted data into only three of the elements. So my question is how do I test for null values when converting an array to a dictionary. Thanks for your help.

          M Offline
          M Offline
          Matt T Heffron
          wrote on last edited by
          #4

          The most obvious solution would be to use List<Product> instead of Product[], and then use Products.Add(...); to populate the collection.

              List<Product> products = new List<Product>;
          
              products.Add(p1);
              products.Add(p2);
              products.Add(p3);
          

          "Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton

          1 Reply Last reply
          0
          • M MadDashCoder

            Hi, thanks for replying. I think I know what the problem is. In my actual code I set the size of the array to 10 but inserted data into only three of the elements. So my question is how do I test for null values when converting an array to a dictionary. Thanks for your help.

            M Offline
            M Offline
            Mathi Mani
            wrote on last edited by
            #5

            Just add a where clause in your code like this:

            Dictionary<int, Product> productlist = products.Where(b => b != null).ToDictionary(p => p.ProductSkew, p => p);

            This will ignore all the null products and convert the rest into dictionary.

            M 1 Reply Last reply
            0
            • M MadDashCoder

              I am trying to convert an array to a dictionary but my code is throwing a null exception. Below is my code

                  static void Main(string\[\] args)
                  {  
                      Product p1 = new Product();
                      p1.ProductSkew = 99;
                      p1.ProductName = "Accord";
                      p1.Color = "Black";
                      p1.Price = 35000;
              
                      Product p2 = new Product();
                      p2.ProductSkew = 46;
                      p2.ProductName = "Corvette";
                      p2.Color = "Black";
                      p2.Price = 65000;
              
                      Product p3 = new Product();
                      p3.ProductSkew = 35;
                      p3.ProductName = "Altima";
                      p3.Color = "Black";
                      p3.Price = 32000;
                      
                      Product\[\] products = new Product\[3\];
              
                      products\[0\] = p1;
                      products\[1\] = p2;
                      products\[2\] = p3;
              
                      Dictionary productlist = products.ToDictionary(p => p.ProductSkew, p => p);
              
                      foreach (KeyValuePair item in productlist)
                      {
                          Product product = new Product();
                          product = item.Value;
              
                          Console.WriteLine(
                          "\\nSkew: {0} \\n" + "Name: {1} \\n" +  "Color: {2} \\n" +  "Price: {3} \\n",
                          product.ProductSkew, product.ProductName, product.Color, product.Price );                
                      }
                      Console.ReadLine();
                  }
              }
              
              public class Product
              {
                  public int ProductSkew { get; set; }
                  public string ProductName { get; set; }        
                  public string Color { get; set; }
                  public double Price { get; set; }
              
                  public void AddProduct()
                  { 
                      Console.WriteLine("Enter Product Skew.");
                      this.ProductSkew = Convert.ToInt32(Console.ReadLine());
              
                      Console.WriteLine("Enter Product Name.");
                      this.ProductName = Console.ReadLine();
              
                      Console.WriteLine("Enter Product Color.");
                      this.Color = Console.ReadLine();
              
                      Console.WriteLine("Enter Product Price.");
                      this.Price = Convert.ToDouble(Console.ReadLine());
                  }
              }
              

              The syntax seems correct but when I step through the code inside of ToDictionay(), p.ProduckSkew is empty and I don't know why. Any help is greatly appreciated, thanks.

              S Offline
              S Offline
              Sascha Lefevre
              wrote on last edited by
              #6

              MadDashCoder wrote:

              Dictionary<int, Product> productlist

              Maybe it's because it's just a code sample but here you don't actually make use of the dictionary as such because you don't use its keys - a list would suffice. If it actually has to be a dictionary then I'd suggest you don't name it productlist ;)

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              1 Reply Last reply
              0
              • M Mathi Mani

                Just add a where clause in your code like this:

                Dictionary<int, Product> productlist = products.Where(b => b != null).ToDictionary(p => p.ProductSkew, p => p);

                This will ignore all the null products and convert the rest into dictionary.

                M Offline
                M Offline
                MadDashCoder
                wrote on last edited by
                #7

                Thank you Mathi, your solution works great. Also I will keep everyone else's suggestions in mind as they are all very valuable, thank you all.

                1 Reply Last reply
                0
                • M MadDashCoder

                  I am trying to convert an array to a dictionary but my code is throwing a null exception. Below is my code

                      static void Main(string\[\] args)
                      {  
                          Product p1 = new Product();
                          p1.ProductSkew = 99;
                          p1.ProductName = "Accord";
                          p1.Color = "Black";
                          p1.Price = 35000;
                  
                          Product p2 = new Product();
                          p2.ProductSkew = 46;
                          p2.ProductName = "Corvette";
                          p2.Color = "Black";
                          p2.Price = 65000;
                  
                          Product p3 = new Product();
                          p3.ProductSkew = 35;
                          p3.ProductName = "Altima";
                          p3.Color = "Black";
                          p3.Price = 32000;
                          
                          Product\[\] products = new Product\[3\];
                  
                          products\[0\] = p1;
                          products\[1\] = p2;
                          products\[2\] = p3;
                  
                          Dictionary productlist = products.ToDictionary(p => p.ProductSkew, p => p);
                  
                          foreach (KeyValuePair item in productlist)
                          {
                              Product product = new Product();
                              product = item.Value;
                  
                              Console.WriteLine(
                              "\\nSkew: {0} \\n" + "Name: {1} \\n" +  "Color: {2} \\n" +  "Price: {3} \\n",
                              product.ProductSkew, product.ProductName, product.Color, product.Price );                
                          }
                          Console.ReadLine();
                      }
                  }
                  
                  public class Product
                  {
                      public int ProductSkew { get; set; }
                      public string ProductName { get; set; }        
                      public string Color { get; set; }
                      public double Price { get; set; }
                  
                      public void AddProduct()
                      { 
                          Console.WriteLine("Enter Product Skew.");
                          this.ProductSkew = Convert.ToInt32(Console.ReadLine());
                  
                          Console.WriteLine("Enter Product Name.");
                          this.ProductName = Console.ReadLine();
                  
                          Console.WriteLine("Enter Product Color.");
                          this.Color = Console.ReadLine();
                  
                          Console.WriteLine("Enter Product Price.");
                          this.Price = Convert.ToDouble(Console.ReadLine());
                      }
                  }
                  

                  The syntax seems correct but when I step through the code inside of ToDictionay(), p.ProduckSkew is empty and I don't know why. Any help is greatly appreciated, thanks.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  MadDashCoder wrote:

                  Product product = new Product(); product = item.Value;

                  There's no point creating a new Product if you're just going to throw it away on the next line. Just use:

                  Product product = item.Value;

                  MadDashCoder wrote:

                  ProductSkew

                  I think you meant to write ProductSku - a Stock Keeping Unit. "Skew"[^] has a totally different meaning. :)


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • M MadDashCoder

                    I am trying to convert an array to a dictionary but my code is throwing a null exception. Below is my code

                        static void Main(string\[\] args)
                        {  
                            Product p1 = new Product();
                            p1.ProductSkew = 99;
                            p1.ProductName = "Accord";
                            p1.Color = "Black";
                            p1.Price = 35000;
                    
                            Product p2 = new Product();
                            p2.ProductSkew = 46;
                            p2.ProductName = "Corvette";
                            p2.Color = "Black";
                            p2.Price = 65000;
                    
                            Product p3 = new Product();
                            p3.ProductSkew = 35;
                            p3.ProductName = "Altima";
                            p3.Color = "Black";
                            p3.Price = 32000;
                            
                            Product\[\] products = new Product\[3\];
                    
                            products\[0\] = p1;
                            products\[1\] = p2;
                            products\[2\] = p3;
                    
                            Dictionary productlist = products.ToDictionary(p => p.ProductSkew, p => p);
                    
                            foreach (KeyValuePair item in productlist)
                            {
                                Product product = new Product();
                                product = item.Value;
                    
                                Console.WriteLine(
                                "\\nSkew: {0} \\n" + "Name: {1} \\n" +  "Color: {2} \\n" +  "Price: {3} \\n",
                                product.ProductSkew, product.ProductName, product.Color, product.Price );                
                            }
                            Console.ReadLine();
                        }
                    }
                    
                    public class Product
                    {
                        public int ProductSkew { get; set; }
                        public string ProductName { get; set; }        
                        public string Color { get; set; }
                        public double Price { get; set; }
                    
                        public void AddProduct()
                        { 
                            Console.WriteLine("Enter Product Skew.");
                            this.ProductSkew = Convert.ToInt32(Console.ReadLine());
                    
                            Console.WriteLine("Enter Product Name.");
                            this.ProductName = Console.ReadLine();
                    
                            Console.WriteLine("Enter Product Color.");
                            this.Color = Console.ReadLine();
                    
                            Console.WriteLine("Enter Product Price.");
                            this.Price = Convert.ToDouble(Console.ReadLine());
                        }
                    }
                    

                    The syntax seems correct but when I step through the code inside of ToDictionay(), p.ProduckSkew is empty and I don't know why. Any help is greatly appreciated, thanks.

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

                    The fixed size array is a nono, this is where you should use some kind of a collection, probablye the List<Product> Matt already suggested. But if you know beforehand you will need a Dictionary (not sure you do!) maybe you could stuff the fresh products in the dictionary right away? :) PS: your Product class deserves to have a constructor that takes the basic values as parameters... PS2: ... as well as a ToString() to return a formatted representation of itself.

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    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