convert Dictionary to array

This is a discussion on convert Dictionary to array within the C# Programming forums, part of the General Programming Boards category; Code: // declare child arrays public string[,] childcolor; public string[,] childgeneo; // create dictionaries public Dictionary<string, int> colors = new ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    20

    convert Dictionary to array

    Code:
    
        // declare child arrays
        public string[,] childcolor;
        public string[,] childgeneo;
      
       // create dictionaries
        public Dictionary<string, int> colors = new Dictionary<string, int>();
        public string color; // name for dictionary
       
        public Dictionary<string, string> geneo = new Dictionary<string, string>();
        public string gtype; // name for dictionary
    
      var jw = new hk1Class();
    
     public static void child_calc()
            {
        // convert dictionaries to child arrays
                
                    jw.childcolor[,] arr = new jw.colors[jw.color.Count];
                    jw.color.Values.ToArray();
    
    
                    jw.childgeneo[,] arr = new jw.geneo[jw.gtype.Count];
                    jw.gtype.Values.ToArray();
    
    
                   int z = 0;
                   while (z < (jw.totalcount + 1))
                   {
                       // calculate color type %
                       int colorpct = ((jw.childcolor[z, 1] / jw.totalcount) * 100);
                       colorpct += "%"; // appedn to string
    
    
                       Console.WriteLine("{0}, {1}, {2}, {3}",
                           jw.childcolor[z, 0],
                           jw.childgeneo[z, 0],
                           jw.childcolor[z, 1],
                           colorpct);
                   } // end while
    
    
                } // end if
              
    
    
                return;
            } // end child_calc
    I'm getting "field' is used like a 'type' error on jw.childcolor which causes other parts to error out.

    How do I fix this & why am I getting the error?

    I want to convert the dictionaries to arrays to read & write sequentially.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Code:
    jw.childcolor[,] arr = new jw.colors[jw.color.Count];
    This is a very weird line. Neither jw.childcolor nor jw.colors is a type. Variable declarations and new'd items need a type. Your compiler is telling you this and I don't know how to help you but to tell you the very same thing: the syntax you are using requires a TYPE. Like Int32 or String or Dictionary of T. Not a variable name.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,170
    You can just ToArray() the collection:
    Code:
                Dictionary<int, string> dict = new Dictionary<int,string>();
                int[] keys = dict.Keys.ToArray();
                string[] values = dict.Values.ToArray();
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    20
    I've reworked the code and now the problem is in the writeline parameters setup.

    Code:
    public class hk1Class
    
       public char colorpct;
      // create dictionaries
        public Dictionary<string, int> colors = new Dictionary<string, int>();
        public string color; // name for dictionary
       
        public Dictionary<string, string> geneo = new Dictionary<string, string>();
        public string gtype; // name for dictionary
    
       public class ColorGenetics
        {
            // hooks to other classes
            public static hk1Class jw;
            public static linkedClass link;
      static void Main(string[] args)
            {
                var jw = new hk1Class();
                var link = new linkedClass();
    
    
                program(); // all other methods are called from within
    
    
            } // end main
           public static void child_calc()
            {
               
    
    
                if (jw.totalcount == hk1Class.rows)
                { jw.linked = true; }
    
    
                if (jw.linked == true)
                {
                    linkedClass.linked_parent_setup();
                    {
                        Dictionary<string, int> childcolor1 = new Dictionary<string, int>();
                        Dictionary<string, int> colorscopy = new Dictionary<string, int>(childcolor1);
                        string[] keys = childcolor1.Keys.ToArray();
                        int[] values = childcolor1.Values.ToArray();
    
    
                        Dictionary<string, string> childgeneo1 = new Dictionary<string, string>();
                        Dictionary<string, string> geneocopy = new Dictionary<string, string>(childgeneo1);
                        string[] keys1 = childgeneo1.Keys.ToArray();
                        string[] values1 = childgeneo1.Values.ToArray();
    
    
    
    
            
    
    
                    }
                    int z = 0;
    this section is giving me problems
    Code:
                       while (z < (jw.totalcount + 1))
                    {
                        // calculate color type %
                        int colorpct1 = ((childcolor1[z, 1] / jw.totalcount) * 100);
                        jw.colorpct = colorct1;
    
    
                        {
                            int x;
                            x = jw.colorpct.Length;
                            jw.colorpct += "%";
                        }
    
    
                        Console.WriteLine("{0}, {1}, {2}, {3}",
                           childcolor1[z, 0],
                           childgeneo1[z, 0],
                           childcolor1[z, 1],
                           jw.colorpct);
                    } // end while
    
    
                } // end if
              
    
    
                return;
            } // end child_calc
    I'm running into var not existing in current context and type conversion errors.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Maybe you can format your code so that it looks neat and proper, the way it's sprinkled all over your file with various anonymous blocks (curly braces where none are needed) it's no wonder you get confused with variable scopes
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert int array to char array ?
    By ahmedBanihammad in forum C Programming
    Replies: 6
    Last Post: 11-08-2010, 12:15 PM
  2. convert char array to int array
    By redruby147 in forum C Programming
    Replies: 3
    Last Post: 03-25-2009, 11:09 AM
  3. Replies: 24
    Last Post: 11-11-2008, 12:39 PM
  4. convert char array to int array
    By skeme in forum C Programming
    Replies: 10
    Last Post: 09-07-2008, 10:37 PM
  5. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 11:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21