locked
Dictionary Help. "Not Homework" RRS feed

  • Question

  • How can I use the values from a dictionary that have been selected by a ComboBox in a math function? I can provide some code upon request but I think it might serve to confuse more than provide clarity.

     

    Greg Rutledge

    tejas_grande@yahoo.com

     

    Edit 1:

    The math to be done is easy. (a / b) *c = x

    a = amperage, (selected by ComboBox cb_amperage)

    b = cable rating per size, (selected by ComboBox cb_cable_size)

    c = electrical system type, (selected by ComboBox cb_system_type)

    x = Number of cables needed, (displayed in TextBox tb6_cable_qty)

     

    I am planning on using dictionaries for cb_cable_size and cb_system_type; dictionaries are shown below.

     //System Type Dictionary
                Dictionary<string, float> system_type1 = new Dictionary<string, float>();
                system_type1.Add("Select System Type", 0);
                system_type1.Add("3P 3W", 3);
                system_type1.Add("3P 4W", 4);
                
                //Cable Dictionary 1 used for cable quantity calculation
                Dictionary<string, float> cable_dictionary_1 = new Dictionary<string, float>();
                
                cable_dictionary_1.Add ("#1", 130);
                cable_dictionary_1.Add ("1/0", 150);
                cable_dictionary_1.Add ("2/0", 175);
                cable_dictionary_1.Add ("3/0", 200);
                cable_dictionary_1.Add ("4/0", 230);
                cable_dictionary_1.Add ("250", 255);
                cable_dictionary_1.Add ("300", 285);
                cable_dictionary_1.Add ("400", 355);
                cable_dictionary_1.Add ("500", 380);
                cable_dictionary_1.Add ("600", 720);
                cable_dictionary_1.Add ("750", 475);

     This is what I have so far. I have not been able to make any progress beyond this point.

     Thanks in advance.

    • Edited by Tejas_Grande Thursday, January 29, 2009 10:24 PM
    Thursday, January 29, 2009 10:02 PM

Answers

  • I found several problems by setting a breakpoint on 2 lines, first one point and then another.

    I first set one here.  The values for x, y, z were zero or not defined well. 
    a = ((x / y)*z);   // set 1st breakpoint here. 
     I moved up and set a breakpoint here to step through the code, and found a few road blocks.
    double a;    //  Set 2nd breakpoint here. 

    I refactored your code to make easier on my bad eyes.  All problems were within the button click handler.  See comments.



    public partial class Form1 : Form  
        {  
            double x, y, z;  
     
            List<string> amperage;  
            List<string> cable_size1;  
            List<string> system_type_list;  
     
            Dictionary<stringdouble> amperage_dictionary;  
            Dictionary<stringdouble> cable_dictionary_1;  
            Dictionary<stringdouble> system_type;  
     
            public Form1()  
            {  
                InitializeComponent();  
                //this.label1.TextAlign = ContentAlignment.MiddleCenter;  
            }  
     
            private void Form1_Load(object sender, EventArgs e)  
            {  
                // Each of the following lists populate the ComboBoxes   
                // to be selected on the main form.               
                amperage = new List<string>();  
                cable_size1 = new List<string>();  
                system_type_list = new List<string>();  
                //  
                InitializeCurrentLoadCB();  
                InitializeCableSizeCB();  
                InitializeSystemTypeCB();  
     
                //---------------Dictionaries-------  
                InitializeAmperageLookup();  
                InitializeCableLookup();  
                InitializeSystemTypeLookup();  
            }  
     
            private void InitializeCurrentLoadCB()  
            {  
                //Amperage List, No Exclusions-------------    
                amperage.Add("Please Select Amperage");  
                amperage.Add("400");  
                amperage.Add("800");  
                amperage.Add("1000");  
                amperage.Add("1200");  
                amperage.Add("1600");  
                amperage.Add("2000");  
                amperage.Add("2500");  
                amperage.Add("3000");  
                amperage.Add("3200");  
                amperage.Add("4000");  
                amperage.Add("5000");  
                amperage.Add("6000");  
     
                comboBox1.DataSource = amperage;  
                comboBox1.SelectedIndex = 0;  
                comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;  
            }  
     
            private void InitializeCableSizeCB()  
            {  
                //Cable List, No Exclusions -----------------  
                cable_size1.Add("Please Select Cable Size");  
                cable_size1.Add("#1");  
                cable_size1.Add("1/0");  
                cable_size1.Add("2/0");  
                cable_size1.Add("3/0");  
                cable_size1.Add("4/0");  
                cable_size1.Add("250");  
                cable_size1.Add("300");  
                cable_size1.Add("400");  
                cable_size1.Add("500");  
                cable_size1.Add("600");  
                cable_size1.Add("700");  
                cable_size1.Add("750");  
     
                comboBox2.DataSource = cable_size1;  
                comboBox2.SelectedIndex = 0;  
                comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;  
            }  
     
            private void InitializeSystemTypeCB()  
            {  
                //System Type List   
                system_type_list.Add("Select System Type");  
                system_type_list.Add("3 Phase 3 Wire");  
                system_type_list.Add("3 Phase 4 Wire");  
     
                comboBox3.DataSource = system_type_list;  
                comboBox3.SelectedIndex = 0;  
                comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;  
                //Initial DataBind for cb_system type ComboBox       
            }  
     
              
     
            private void button1_Click(object sender, EventArgs e)  
            {               
                if (!String.IsNullOrEmpty(comboBox1.Text) &&   
                    (!String.IsNullOrEmpty(comboBox2.Text) &&   
                    (!String.IsNullOrEmpty(comboBox3.Text))))    
                    {       
                        double a;    //  Set 2nd breakpoint here.  
                        if (comboBox1.Text == ("400"))  // SelectedText means highlighted text.  
                            {    
                                x = amperage_dictionary["400"];                     
                            }  
     
                            if (comboBox1.Text == ("800"))    // SelectedText means highlighted text.  
                            {    
                                x = amperage_dictionary["800"];                     
                            }  
     
                            if (comboBox2.Text == ("#1"))    // SelectedText means highlighted text.  
                            {    
                                y = cable_dictionary_1["#1"];    // wrong lookup                  
                            }  
     
                            if (comboBox2.Text == ("1/0"))   // SelectedText means highlighted text.  
                            {  
                                y = cable_dictionary_1["1/0"];   //  wrong lookup               
                            }  
     
                            if (comboBox3.Text == ("3 Phase 3 Wire"))    // SelectedText means highlighted text.  
                            {    
                                z = system_type["3 Phase 3 Wire"];                      
                            }  
                            if (comboBox3.Text == ("3 Phase 4 Wire"))    // SelectedText means highlighted text.  
                            {    
                                z = system_type["3 Phase 4 Wire"];    
                            }    
                                        
                        a = ((x / y)*z);   // set 1st breakpoint here.  
                        this.textBox1.Text = Math.Round(a,2).ToString();    
                                
                    }   
     
            }  
     
            private void InitializeSystemTypeLookup()  
            {  
                //System Type Dictionary    
                system_type = new Dictionary<stringdouble>();  
                system_type.Add("Select System Type", 0);  
                system_type.Add("3 Phase 3 Wire", 3);  
                system_type.Add("3 Phase 4 Wire", 4);  
            }  
     
            private void InitializeCableLookup()  
            {  
                //Cable Dictionary 1 used for cable quantity calculation    
                cable_dictionary_1 = new Dictionary<stringdouble>();  
     
                cable_dictionary_1.Add("#1", 130);  
                cable_dictionary_1.Add("1/0", 150);  
                cable_dictionary_1.Add("2/0", 175);  
                cable_dictionary_1.Add("3/0", 200);  
                cable_dictionary_1.Add("4/0", 230);  
                cable_dictionary_1.Add("250", 255);  
                cable_dictionary_1.Add("300", 285);  
                cable_dictionary_1.Add("400", 355);  
                cable_dictionary_1.Add("500", 380);  
                cable_dictionary_1.Add("600", 720);  
                cable_dictionary_1.Add("750", 475);  
            }  
     
            private void InitializeAmperageLookup()  
            {  
                //Amperage Dictionary       
                amperage_dictionary = new Dictionary<stringdouble>();  
     
                amperage_dictionary.Add("400", 400);  
                amperage_dictionary.Add("800", 800);  
                amperage_dictionary.Add("1000", 1000);  
                amperage_dictionary.Add("1200", 1200);  
                amperage_dictionary.Add("1600", 1600);  
                amperage_dictionary.Add("2000", 2000);  
                amperage_dictionary.Add("2500", 2500);  
                amperage_dictionary.Add("3000", 3000);  
                amperage_dictionary.Add("3200", 3200);  
                amperage_dictionary.Add("4000", 4000);  
                amperage_dictionary.Add("5000", 5000);  
                amperage_dictionary.Add("6000", 6000);  
            }  
        } 

    =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Tejas_Grande Tuesday, February 3, 2009 6:21 PM
    Tuesday, February 3, 2009 2:47 PM
    Moderator
  •  I included those Dictionary<> initializations in the each of the last three methods.  They are called during Form.Load.  As for your exceptions, sometimes it a good programming practice to verify that a key exists in a dictionary before you use it. 

    Which is why I have a habit of using the enum type as my Dictionary TKey.  As long as I use the Enum as the type for my key, then I have a type-safe guarantee that my key will be found.  No "key not found" exception to worry about.

    I think you just need to be careful as to how you create your lookups.  Your code needs a zillion more if statements.  You may wish to change those last three methods that initialize the lookups to look something like this.

     private void InitializeSystemTypeLookup()  
            {  
                //System Type Dictionary    
                system_type = new Dictionary<stringdouble>();  
                system_type.Add(this.system_type_list[0], 0);  
                system_type.Add(this.system_type_list[1], 3);  
                system_type.Add(this.system_type_list[2], 4);  
            } 

    So look at what happens to your if statements in the Button.Click handler....

    private void button1_Click(object sender, EventArgs e)  
            {               
                if (!String.IsNullOrEmpty(comboBox1.Text) &&   
                    (!String.IsNullOrEmpty(comboBox2.Text) &&   
                    (!String.IsNullOrEmpty(comboBox3.Text))))    
                    {       
                        double a;    //  Set 2nd breakpoint here.  
                        if (comboBox1.Text == ("400"))  // SelectedText means highlighted text.  
                            {    
                                x = amperage_dictionary["400"];                     
                            }  
     
                            if (comboBox1.Text == ("800"))    // SelectedText means highlighted text.  
                            {    
                                x = amperage_dictionary["800"];                     
                            }  
     
                            if (comboBox2.Text == ("#1"))    // SelectedText means highlighted text.  
                            {    
                                y = cable_dictionary_1["#1"];    // wrong lookup                  
                            }  
     
                            if (comboBox2.Text == ("1/0"))   // SelectedText means highlighted text.  
                            {  
                                y = cable_dictionary_1["1/0"];   //  wrong lookup               
                            }  
     
                            //if (comboBox3.Text == ("3 Phase 3 Wire"))    // SelectedText means highlighted text.  
                            //{    
                            //    z = system_type["3 Phase 3 Wire"];                      
                            //}  
                            //if (comboBox3.Text == ("3 Phase 4 Wire"))    // SelectedText means highlighted text.  
                            //{    
                            //    z = system_type["3 Phase 4 Wire"];    
                            //}    
                            if (comboBox3.SelectedIndex != 0)  
                            {  
                                z = system_type[system_type_list[comboBox3.SelectedIndex]];  
                            }  
                                        
                        a = ((x / y)*z);   // set 1st breakpoint here.  
                        this.textBox1.Text = Math.Round(a,2).ToString();    
                                
                    }   
     
            } 

    ...instead of an if statement for EVERY possible value---it is easy to forget to set an if for a given value---one statement handles all of them.




    Mark the best replies as answers. "Fooling computers since 1971."

    EDIT:  That is a bug with the highlight tool!
    Tuesday, February 3, 2009 7:11 PM
    Moderator
  • You try to redeclare the List<> variables.  This is my code.

    private void InitializeSystemTypeCB()  
            {  
                //System Type List      
                system_type_list = new List<string>();  
                system_type_list.Add("Select System Type");  
                system_type_list.Add("3 Phase 3 Wire");  
                system_type_list.Add("3 Phase 4 Wire");  
     
                comboBox3.DataSource = system_type_list;  
                comboBox3.SelectedIndex = 0;  
                comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;  
                //Initial DataBind for cb_system type ComboBox                  
            } 


    This is your code.

    private void InitializeSystemTypeCB()  
            {  
                //System Type List      
                List<string>  system_type_list = new List<string>();  
                system_type_list.Add("Select System Type");  
                system_type_list.Add("3 Phase 3 Wire");  
                system_type_list.Add("3 Phase 4 Wire");  
     
                comboBox3.DataSource = system_type_list;  
                comboBox3.SelectedIndex = 0;  
                comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;  
                //Initial DataBind for cb_system type ComboBox                  
            } 

    Get rid of that.  You declaring a local variable with the same name.  The variable declared at the class level never gets initialized, so it remains null.  That mistake appears 3 times, inside each of the Initialize****CB() methods.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Edited by Rudedog2Moderator Wednesday, February 4, 2009 1:03 AM wrong method posted
    • Marked as answer by Tejas_Grande Wednesday, February 4, 2009 2:03 AM
    Wednesday, February 4, 2009 12:59 AM
    Moderator
  •  I think i fixed more than I just posted.  You have too many definition declarations.  You had the same problem with the Dictionaries<>, too. 

    When you place the class' type name to the left of the variable name you are declaringa new variable definition, not working with the same one.  We declared the variables names and their types at the top of the code.

    double x, y, z;  
     
            List<string> amperage;  
            List<string> cable_size1;  
            List<string> system_type_list;  
     
            Dictionary<stringdouble> amperage_dictionary;  
            Dictionary<stringdouble> cable_dictionary_1;  
            Dictionary<stringdouble> system_type; 

    Once we do that, we only need to use the variable name.  I was able to observe the error by setting a watch on "amperage", and noticed that it went to null when the method to initialize it was called ended.   I blame myself for this error.  I should have used the "this" reference to access those fields listed above.  I suggest that you modify your code to the same.  I left them out partially to avoid confusion, but wound up creating it.  My bad.

    private void InitializeSystemTypeLookup()  
            {  
                //System Type Dictionary    
                this.system_type = new Dictionary<stringdouble>();  
                this.system_type.Add(this.system_type_list[0], 0);  
                this.system_type.Add(this.system_type_list[1], 3);  
                this.system_type.Add(this.system_type_list[2], 4);  
            } 

    Make all of your code look like that.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Tejas_Grande Wednesday, February 4, 2009 2:03 AM
    Wednesday, February 4, 2009 1:11 AM
    Moderator

All replies

  •  How do we know you are telling the truth and it is not a homework? :) Anyhow, you should explain what Math function it is, what kind of values you are planning to use and how the ComboBox is related to the story. From what you told us so far nothing can be understood. In any event you retrieve values from a Dictionary by iterating thru KeyVauePair Collection object. You must know the Key for the Value you want to retrieve or you can simply iterate with no Key knowledge and once you stumbled on the Value you want you grab it.
    AlexB
    Thursday, January 29, 2009 10:13 PM
  • OK

    • what's in the combobox?
    • what does the dictionary look like?
    • what do the math functions require as parameters?

    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Thursday, January 29, 2009 10:14 PM
  •  Take a look at Reverse Polish Notation. It is Polish. :)
    AlexB
    Thursday, January 29, 2009 10:30 PM
  •  So, b is cable. If it is that stationary, you don't really need to Use RPN. You can simply implement a loop

     foreach ( KeyValuePair pair in cable_dic )  
    {  
            string b1 = pair,Key;  
            string b2 = pair.Value;  
            // you probably will want to use b2 as a b value but I don;t understand the meaning of 1/0. 2/0, etc.  
    }  
     

    AlexB
    Thursday, January 29, 2009 10:35 PM
  • ok so you have strings in the combo boxes and the dictionaries are

    Dictionary<string, float>

    do lets say you have a method

    public int GetRequiredCables( float amps, float rating, float type)
    {
         return Math.Ceiling(amps * rating / type);
    }

    then you can get

    string amp =  = ComboBox_cb.SelectedText;
    ...
    etc

    then

    int result = GetRequiredCables( dictionary[amp], ... etc);

    Does this do the kind of thing you want?


    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Thursday, January 29, 2009 10:41 PM
  • 1/0 2/0 are cable sizes. Each cable size has a rating defined in the dictionary. 
    Thursday, January 29, 2009 10:47 PM
  • Richard; I am having a difficult time understanding your answer. The problem could very well be me. However, can you provide a little more detail. I think you understand what I am trying to do, but I need a little more. 

     

    Thank you
    Thursday, January 29, 2009 11:01 PM
  • for each parameter you want to pass into the method you direct need to get the text selected in each combobox

    for example to get the selected text freom the ComboBox_cb you so

    string cbText = ComboBox_cb.SelectedText;

    next you need to look up the actual values from the corresponding dictionary so something like this

    float cb = dictionaryCB[cbText];

    you do this kind of thing for each value you need and then pass the values into your calculation


    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Thursday, January 29, 2009 11:09 PM
  • Here's a couple of brainstorms for you.

    http://www.dofactory.com/Patterns/PatternCommand.aspx   Take a look at the second example.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/dd4d943b-951d-4228-8205-8b6260f1aca5/

    Rudedog   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, January 29, 2009 11:16 PM
    Moderator
  • Rudedog2 said:

    Here's a couple of brainstorms for you.

    http://www.dofactory.com/Patterns/PatternCommand.aspx   Take a look at the second example.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/dd4d943b-951d-4228-8205-8b6260f1aca5/

    Rudedog   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."



    Oh, as for your Dictionary problem.  Create them like this.  Dictionary<string,ValueType>.  Use the string from the ComboBox as a lookup into your Dictionary to return a numeric value, like float or Single.

    You do realize that ComboBoxes store type System.Object, and use the ToString() method to display the objects.  You could actually store instances of actual objects that contain the values for your calculations if you wanted in the control.


    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, January 29, 2009 11:21 PM
    Moderator
  • Richard; I know I am missing something simple can you take a look at my attempt to implement the solution and tell me what I am doing wrong. I get the "does not exist in the current context error."

     

    void Cb_amperageSelectedIndexChanged(object sender, EventArgs e)     
            {            
                 
                string amp = cb_amperage.SelectedText; 
                string cable = cb_cable_size.SelectedText; 
                string system = cb_system_type.SelectedText; 
                 
                int x; 
                float y = cable_dictionary1 [cable]; 
                float z = system_type1 [system];             
                     
                 
                //System Type Dictionary 
                Dictionary<string, float> system_type1 = new Dictionary<string, float>(); 
                system_type1.Add("Select System Type", 0); 
                system_type1.Add("3P 3W", 3); 
                system_type1.Add("3P 4W", 4); 
                         
                 
                //Cable Dictionary 1 used for cable quantity calculation 
                Dictionary<string, float> cable_dictionary_1 = new Dictionary<string, float>(); 
                 
                cable_dictionary_1.Add ("#1", 130); 
                cable_dictionary_1.Add ("1/0", 150); 
                cable_dictionary_1.Add ("2/0", 175); 
                cable_dictionary_1.Add ("3/0", 200); 
                cable_dictionary_1.Add ("4/0", 230); 
                cable_dictionary_1.Add ("250", 255); 
                cable_dictionary_1.Add ("300", 285); 
                cable_dictionary_1.Add ("400", 355); 
                cable_dictionary_1.Add ("500", 380); 
                cable_dictionary_1.Add ("600", 720); 
                cable_dictionary_1.Add ("750", 475); 
            { 
                             

     

    Friday, January 30, 2009 2:09 AM
  •  Net time always indicate where the compil eerror points to. Whic line? How many lines are affected? What variables do not e4xist in teh current context? it is all very important. You leave people guessing for no reason. In this case it did not seem to be difficult to determine what the culprits were. I presume those vraibles are textBoxes. I also assume they are class properties. Therefore you can resolve the compile hangup with this simple method:

                string amp = this.cb_amperage.SelectedText;  
                string cable = this.cb_cable_size.SelectedText;  
                string system = this.cb_system_type.SelectedText;  
                int x; 

    AlexB
    Friday, January 30, 2009 2:46 AM
  • Alex; you are right, I do apologize. The error came from the below. The error centers around the dictionary names "cable_dictionary and system_type." 
               float y = cable_dictionary1[cable]; 
                float z = system_type[system];   
    Friday, January 30, 2009 2:51 AM
  • 1) it's named cable_dictionary1 instead of cable_dictionary_1

    2) those 2 lines need to go at the very bottom of that code snippet.


    Daniel - http://webs.neumont.edu/dstafford
    • Marked as answer by Tejas_Grande Friday, January 30, 2009 3:01 AM
    • Unmarked as answer by Tejas_Grande Friday, January 30, 2009 3:01 AM
    Friday, January 30, 2009 2:53 AM
  • Thank you Daniel that did it. I moved the snippet after the dictionaries and the errors went away.
    Friday, January 30, 2009 3:03 AM
  • Alex & Daniel; please have a look at this, the math portion of the code. I am having trouble with the "x" portion. How do I need to structure it to fit in the equation. What I have is below.  The X does not need to be referenced to a dictionary; the value selected will work directly in the formula.  X = float.Parse returns this error when amperage is selected. "Input string was not in a correct format." i think if this works that will be it. I will continue to look into it; if anyone has any ideas please post. Thank you in advance for your help.

     EDIT: I do believe the problem is not listed below. I populated the ComboBox cb_amperage with a list full of strings. How do I implement a string in the equation?

     

     EDIT 2: The input code is posted at the bottom. Is there a problem with it being a string?

     

    void Cb_amperageSelectedIndexChanged(object sender, EventArgs e)     
            {            
                 
                 
                string cable = cb_cable_size.SelectedText; 
                string system = cb_system_type.SelectedText; 
                 
                //-----------------------------Dictionaries----------------------------------------------- 
                //Amperage Dictionary    
                Dictionary<string, int> amperage_dictionary = new Dictionary<string, int>(); 
             
                amperage_dictionary.Add ("400", 1); 
                amperage_dictionary.Add ("800", 2); 
                amperage_dictionary.Add ("1000", 3); 
                amperage_dictionary.Add ("1200", 4); 
                amperage_dictionary.Add ("1600", 5); 
                amperage_dictionary.Add ("2000", 6); 
                amperage_dictionary.Add ("2500", 7); 
                amperage_dictionary.Add ("3000", 8); 
                amperage_dictionary.Add ("3200", 9); 
                amperage_dictionary.Add ("4000", 10); 
                amperage_dictionary.Add ("5000", 11); 
                amperage_dictionary.Add ("6000", 12);            
                 
                //System Type Dictionary 
                Dictionary<string, float> system_type = new Dictionary<string, float>(); 
                system_type.Add("Select System Type", 0); 
                system_type.Add("3P 3W", 3); 
                system_type.Add("3P 4W", 4); 
                             
                //Cable Dictionary 1 used for cable quantity calculation 
                Dictionary<string, float> cable_dictionary_1 = new Dictionary<string, float>(); 
                 
                cable_dictionary_1.Add ("#1", 130); 
                cable_dictionary_1.Add ("1/0", 150); 
                cable_dictionary_1.Add ("2/0", 175); 
                cable_dictionary_1.Add ("3/0", 200); 
                cable_dictionary_1.Add ("4/0", 230); 
                cable_dictionary_1.Add ("250", 255); 
                cable_dictionary_1.Add ("300", 285); 
                cable_dictionary_1.Add ("400", 355); 
                cable_dictionary_1.Add ("500", 380); 
                cable_dictionary_1.Add ("600", 720); 
                cable_dictionary_1.Add ("750", 475); 
                 
                float x;  
                x = float.Parse(cb_amperage.SelectedText);//<--- how do I form this? 
                float y = cable_dictionary_1[cable]; 
                float z = system_type[system];           
                float a; 
                a = ((x / y)*z); 
                tb6_cable_qty.Text = Math.Round(a,2).ToString(); 
                 
                 
    //Each of the following lists populate the ComboBoxes to be selected on the main form.  
    //Amperage List, No Exclusions----------------------------------------------------------- 
                List<string> amperage = new List<string>(); 
                amperage.Add("400"); 
                amperage.Add("800"); 
                amperage.Add("1000"); 
                amperage.Add("1200"); 
                amperage.Add("1600"); 
                amperage.Add("2000"); 
                amperage.Add("2500"); 
                amperage.Add("3000"); 
                amperage.Add("3200"); 
                amperage.Add("4000"); 
                amperage.Add("5000"); 
                amperage.Add("6000"); 
                 
                cb_amperage.DataSource = amperage; 
                cb_amperage.SelectedIndex = 0
                //Initial DataBind for Amperage ComboBox 
    Friday, January 30, 2009 3:30 AM
  •  
     
                float x = float.Parse ( cb_amperage.SelectedText.Trim ( ) ); //<--- how do I form this?    
                float y = cable_dictionary_1[ cable ];  
                float z = system_type[ system ];  
                float a;  
                a = ( ( x / y ) * z );  
                tb6_cable_qty.Text = Math.Round ( a, 2 ).ToString ( );   
    It means it is not in correct format. How does your input string looks like? I added the Trim( ) method just in case. Make sure there are no trailing blanks. Post your input string. If you are using a different culture then you will have to indicate the IFormatProcvider to make the Culture clear.
    AlexB
    Friday, January 30, 2009 4:10 AM
  • Populate your ComboBoxes with constants, then use constants.

    enum CableSize  
        {  
            Size1 = 130,  
            Size1_0 = 150,  
            Size300 = 285,  
            Size400 = 355        
              
        }  
        enum LoadSize  
        {  
            Load400 = 1,  
            Load800 = 2  
        } 

     

    Load them up like this.

    void InitializeComboBox1()  
            {  
                foreach (CableSize size in Enum.GetValues(typeof(CableSize)))  
                {  
                    cb1.Items.Add(size);  
                    int wireGauge = (int)size;  
                }  
            } 

     

    Now your SelectedItem will be an Enum holding your constant ready to be cast to an integer. 

    Make the comboBox DropDownStyle a List and you off and flying.


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 30, 2009 5:12 PM
    Moderator
  • Rudedog2; can I place this code in another function? When I place it in the code below it creates errors on previously operational code. Does this function need to stand alone?

     

    EDIT: I apologize. The least I could do was copy the code without inserting a bunch of typos. Disregard my earlier post. However, I do have another question. I have been using those lists, <string>, and some "if" statements to filter the cable selections available based on the selection of amperage. Can I still do this with the Enum?

     

    void Cb_amperageSelectedIndexChanged(object sender, EventArgs e)     
            { 
     
    //Cable Enum, No Exclusions -------------------------------------------------------------- 
                enum cable_size1  
                { 
                    size1 = 130
                    size1_0 = 150
                    size2_0 = 175
                    size3_0 = 200
                    size4_0 = 230
                    size250 = 255
                    size300 = 285,  
                    size400 = 355
                    size500 = 380
                    size600 = 420
                    size750 = 475 
                } 
                 
                void Initialize cb_cable_size() 
                { 
                    foreach (cable_size1 size in enum.GetValues(typeof(cable_size1))) 
                    { 
                        cb_cable_size.Items.Add(size); 
                        int mcm = (int)size; 
                    } 
                } 
    • Edited by Tejas_Grande Friday, January 30, 2009 5:53 PM mistake
    Friday, January 30, 2009 5:46 PM
  • Write a test program that fills a dropdownlist style combobox with enum values.  Create an event handler for SelectedIndex has changed.  Print the SelectedItem.ToString() in a MessageBox.  Cast the SelectedItem to an integer and look at that too with a breakpoint.  Simple, huh?

    You can remove any Item you want from the ComboBox, just as easily as you add them.  ComboBox1.Items.Clear() will clear the list.  You could create separate Enums that define cables for each amperage rating.  Or, you could try to save it and build relationships in a database.  But, I think the readonly comboboxes and enums is simple and straightfoward enough for what you seem to be doing.


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 30, 2009 6:11 PM
    Moderator
  • I will keep the same setup as I had with my lists. It should conform to your statement, "You could create separate Enums that define cables for each amperage rating;" since I have all the logic worked out. I appreciate your help!
    Friday, January 30, 2009 6:48 PM
  • Tejas_Grande said:

    I will keep the same setup as I had with my lists. It should conform to your statement, "You could create separate Enums that define cables for each amperage rating;" since I have all the logic worked out. I appreciate your help!



    It was fun.   The most common mistake that "experienced" developers make is not re-writing code that needs to be re-written.  Not to say that yours does, though.  Never be afraid to update code that "works" just fine.  Especially if it is hard to read and debug when you created it.
    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 30, 2009 6:59 PM
    Moderator
  • RudeDog; I am running into an issue that I don't know how to explain. Below I have posted the code before and after I attempt to place the suggested code. Before the code will compile and debug with no issues. After, I get three errors that would indicate that my brackets are wrong. First one is "} expected" (shown on the first bracket after MainFormLoad. The second error is "A namespace does not directly contain memebers such as fields or methods." (located on the second void for cb_amperageSelectedIndexChanged. And the last "Type or namespace definition, or end-of-file expected, at the very end of the code. 

    I am almost certain I copied the code as you showed it. Where did I go wrong? I hate that I cant see it. 

     

    //BEFORE------ 
    void MainFormLoad(object sender, EventArgs e) 
            { 
                string amp = this.cb_amperage.SelectedText; 
            } 
             
            void cb_amperageSelectedIndexChanged(object sender, EventArgs e)     
            {            
                //float x = float.Parse(cb_amperage.SelectedText.Trim()); 
                //float y = cable_dictionary_1[cable]; 
                //float z = system_type[system];             
                //float a; 
                //a = ((x / y)*z); 
                //tb6_cable_qty.Text = Math.Round(a,2).ToString(); 
                         
            } 
     
    //AFTER------ 
     
    void MainFormLoad(object sender, EventArgs e) 
            { 
                enum cable_size1  
                    { 
                        Size1 = 130
                        Size1_0 = 150
                        Size2_0 = 175
                        Size3_0 = 200
                        Size4_0 = 230
                        Size250 = 255
                        Size300 = 285,  
                        Size400 = 355
                        Size500 = 380
                        Size600 = 420
                        Size750 = 475 
                    }//Cable Enum, No Exclusions -------------------------------------------------------- 
            } 
             
            void cb_amperageSelectedIndexChanged(object sender, EventArgs e)     
            {            
                //float x = float.Parse(cb_amperage.SelectedText.Trim()); 
                //float y = cable_dictionary_1[cable]; 
                //float z = system_type[system];             
                //float a; 
                //a = ((x / y)*z); 
                //tb6_cable_qty.Text = Math.Round(a,2).ToString(); 
                         
             
                 
            } 
     
                 
    Friday, January 30, 2009 8:55 PM
  • The enum is a class all by itself.  Put into a separate file, just like a class.  You are declaring it inside of a method.  The others errors are the result of the compiler trying to make sense out of the misplaced enum definition.

    I can see that you will have other problems.  This compiles...

    //BEFORE------    
    void MainFormLoad(object sender, EventArgs e)    
            {    
                string amp = this.cb_amperage.SelectedText;    
            }    
     

    ...but you will have to initialize the comboboxes first. 

    Use a method to initialize the CBs similar to the one that I posted.  Put it into the Form Load event before you try to read the data in the CB, combobox.  Make sure you set the SelectedItem Property before you try to read what the CB is displaying to avoid null reference errors.


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 30, 2009 9:24 PM
    Moderator
  • The enum type is a constant.

    void InitializeComboBox1()     
            {     
                foreach (CableSize size in Enum.GetValues(typeof(CableSize)))     
                {     
                    cb1.Items.Add(size);     
                    int wireGauge = (int)size;     
                }     
            }    
     

     

    That is a method will use the constant definition to initialize a combobox named cb1.  You will still need to set the SelectedIndex property to set an initial display value.  You might also want to set the cb1.DropDownStyle to DropDownList, to make it read-only. 

    Add of this stuff to this same method.  Make one for each CB.  Call the methods in the FormLoad event before you do anything else to avoid null reference errors.


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 30, 2009 9:30 PM
    Moderator
  • Thanks for sticking with me. The code I placed in the "before section" was more of place holder. However, I will keep in mind your advice as I move forward. Thanks again, and sorry for the common errors.

     

    Additional Question: Can I use "wordify" or something similar to make the enum values more readable in the ComboBox?

    Friday, January 30, 2009 9:32 PM
  • Did you write the test program I suggested at the top of the page?

    Sorry, no.  Enums are constants, that your code sees as another class, but you cannot override their ToString() method.  As I mentioned above, ComboBoxes store their items as type System.Object.  The control uses the inherited ToString() method to display the Items.  But, you can modify the "storage" routine I wrote to store strings, instead of the actual enum objects.

    string obj = enum.ToString()  // obj = "Size600", example

    Now you can trim off the first four characters and store that.  But, that will put you back to where you almost were.  When the user selects an item, you will need to translate the selection into a numeric value. 

    Create a lookup list.  You could add the enum to a List<Enum> object at the same time as you add each string to the combobox.  This will create a list of the enum values nearly identical to the ComboBox list.  You could even store this list inside of the Tag property of the combobox.  When the user makes a selection, read the combobox.SelectedIndex integer value.  Use that integer value to as the index to read your lookup list to fetch the proper enum.


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, January 30, 2009 10:41 PM
    Moderator
  • I am still working on this one. However, I think the best direction to move in uses dictionaries or some other method that allows the user to select from a list of what he or she understands. I would be easy to ask the user to do the work of looking up values to enter in to text boxes but that is not much better than what we have currently.

    Is there a function I can use to return a value "int" through an "if" function, from the dictionary below and use it in an equation that displays the answer in a text box as a "string"; Possibly TryGetValue?



                    //Cable Dictionary 1 used for cable quantity calculation 
                    Dictionary<string, float> cable_dictionary_1 = new  Dictionary<string, float>();             
                    cable_dictionary_1.Add ("#1", 130); 
                    cable_dictionary_1.Add ("1/0", 150); 
                    cable_dictionary_1.Add ("2/0", 175); 
                    cable_dictionary_1.Add ("3/0", 200); 
                    cable_dictionary_1.Add ("4/0", 230); 
                    cable_dictionary_1.Add ("250", 255); 
                    cable_dictionary_1.Add ("300", 285); 
                    cable_dictionary_1.Add ("400", 355); 
                    cable_dictionary_1.Add ("500", 380); 
                    cable_dictionary_1.Add ("600", 420); 
                    cable_dictionary_1.Add ("700", 460); 
                    cable_dictionary_1.Add ("750", 475); 

    Sunday, February 1, 2009 3:33 AM
  • Do you mean return the numeric value from the dictionary?  Use the "key" to access the dictionary.

    int result = cable_dictionary["1/0"];  // result = 150 !!!
    Mark the best replies as answers. "Fooling computers since 1971."
    Sunday, February 1, 2009 6:03 PM
    Moderator
  • Can I do this? int result = cable_dictionary[cb_cable_type.SelectedIndex]; result 150????
    Sunday, February 1, 2009 7:41 PM
  • Tejas_Grande said:

    Can I do this? int result = cable_dictionary[cb_cable_type.SelectedIndex]; result 150????



    Use the debugger and and examine the type returned from combobox.SelectedIndex.  Take a look at the property in your Object Browser Window, by doing a search for "comboBox".  Click on System.Windows.Forms.Combobox in the left pane, then find the property in the right pane.  Click on it, then look at the lower right corner.  The Object Browser is THE most useful tool in the IDE.

    How is your dictionary declared?  Shouldn't the dictionary key be a string, I think it was? 
    Mark the best replies as answers. "Fooling computers since 1971."
    Sunday, February 1, 2009 8:02 PM
    Moderator
  • Yes the key is a string and the value is a int or float. I think I see what your getting at. The following says I cant implicitly convert type string to int. Is there a way I can return the value instead of the key without specifying what must be selected in the ComboBox like you show above?

    What about TValue GetByKey? Can I use that function in this case?

    int y; 
    y = cable_dictionary_1[cb_cable_size.SelectedIndex].ToString();  

    Sunday, February 1, 2009 10:15 PM
  • Rudedog2; I think I am close. I have written some test code and I am almost getting the result I need. However, when this code is executed it returns a value of "NaN." I understand that this means, "not a number" and that must mean that I have a formatting issue. Please have a look at the code below and see if you can point me in the right direction. I feel like I am so close. Thanks in advance.

    void MainFormLoad(object sender, EventArgs e) 
            { 
                    //Each of the following lists populate the ComboBoxes to be selected on the main form.  
                    //Amperage List, No Exclusions----------------------------------------------------------- 
                    List<string> amperage = new List<string>(); 
                    amperage.Add("Please Select Amperage"); 
                    amperage.Add("400"); 
                    amperage.Add("800"); 
                    amperage.Add("1000"); 
                    amperage.Add("1200"); 
                    amperage.Add("1600"); 
                    amperage.Add("2000"); 
                    amperage.Add("2500"); 
                    amperage.Add("3000"); 
                    amperage.Add("3200"); 
                    amperage.Add("4000"); 
                    amperage.Add("5000"); 
                    amperage.Add("6000"); 
                 
                    cb_test_1.DataSource = amperage
                    cb_test_1.SelectedIndex = 0
                     
                    //Cable List, No Exclusions -------------------------------------------------------------- 
                    List<string> cable_size1 = new List<string>(); 
                    cable_size1.Add("Please Select Cable Size"); 
                    cable_size1.Add ("#1"); 
                    cable_size1.Add ("1/0"); 
                    cable_size1.Add ("2/0"); 
                    cable_size1.Add ("3/0"); 
                    cable_size1.Add ("4/0"); 
                    cable_size1.Add ("250"); 
                    cable_size1.Add ("300"); 
                    cable_size1.Add ("400"); 
                    cable_size1.Add ("500"); 
                    cable_size1.Add ("600"); 
                    cable_size1.Add ("700"); 
                    cable_size1.Add ("750"); 
                 
                    cb_test_2.DataSource = cable_size1
                    cb_test_2.SelectedIndex = 0
                    //Initial DataBind for cable size ComboBox 
                     
                    //System Type List   
                    List<string> system_type_list = new List<string>(); 
                    system_type_list.Add("Select System Type"); 
                    system_type_list.Add("3 Phase 3 Wire"); 
                    system_type_list.Add("3 Phase 4 Wire"); 
                 
                    cb_test_3.DataSource = system_type_list
                    cb_test_3.SelectedIndex = 0
                    //Initial DataBind for cb_system type ComboBox               
            } 
             
             
            double x, y, z; 
             
            void Button1Click(object sender, System.EventArgs e) 
            { 
                //-----------------------------Dictionaries----------------------------------------------- 
                //Amperage Dictionary    
                Dictionary<string, double> amperage_dictionary = new Dictionary<string, double>(); 
             
                amperage_dictionary.Add ("400", 400); 
                amperage_dictionary.Add ("800", 800); 
                amperage_dictionary.Add ("1000", 1000); 
                amperage_dictionary.Add ("1200", 1200); 
                amperage_dictionary.Add ("1600", 1600); 
                amperage_dictionary.Add ("2000", 2000); 
                amperage_dictionary.Add ("2500", 2500); 
                amperage_dictionary.Add ("3000", 3000); 
                amperage_dictionary.Add ("3200", 3200); 
                amperage_dictionary.Add ("4000", 4000); 
                amperage_dictionary.Add ("5000", 5000); 
                amperage_dictionary.Add ("6000", 6000);          
                         
                //Cable Dictionary 1 used for cable quantity calculation 
                Dictionary<string, double> cable_dictionary_1 = new Dictionary<string, double>(); 
                 
                cable_dictionary_1.Add ("#1", 130); 
                cable_dictionary_1.Add ("1/0", 150); 
                cable_dictionary_1.Add ("2/0", 175); 
                cable_dictionary_1.Add ("3/0", 200); 
                cable_dictionary_1.Add ("4/0", 230); 
                cable_dictionary_1.Add ("250", 255); 
                cable_dictionary_1.Add ("300", 285); 
                cable_dictionary_1.Add ("400", 355); 
                cable_dictionary_1.Add ("500", 380); 
                cable_dictionary_1.Add ("600", 720); 
                cable_dictionary_1.Add ("750", 475); 
                 
                //System Type Dictionary 
                Dictionary<string, double> system_type = new Dictionary<string, double>(); 
                system_type.Add("Select System Type", 0); 
                system_type.Add("3 Phase 3 Wire", 3); 
                system_type.Add("3 Phase 4 Wire", 4); 
                 
                         
                if (!String.IsNullOrEmpty(cb_test_1.Text) && (!String.IsNullOrEmpty(cb_test_2.Text) && (!String.IsNullOrEmpty(cb_test_3.Text)))) 
                    {    
                        double a; 
                        if (cb_test_1.SelectedText == ("400")) 
                            { 
                                x = amperage_dictionary["400"];                  
                            }        
                         
                        if (cb_test_1.SelectedText == ("800")) 
                            { 
                                x = amperage_dictionary["800"];                  
                            }    
                         
                        if (cb_test_2.SelectedText == ("#1")) 
                            { 
                                y = amperage_dictionary["#1"];                   
                            }    
                     
                        if (cb_test_2.SelectedText == ("1/0")) 
                            { 
                                y = amperage_dictionary["1/0"];                  
                            }    
                                     
                        if (cb_test_3.SelectedText == ("3 Phase 3 Wire")) 
                            { 
                                z = system_type["3 Phase 3 Wire"];                   
                            } 
                        if (cb_test_3.SelectedText == ("3 Phase 4 Wire")) 
                            { 
                                z = system_type["3 Phase 4 Wire"]; 
                            } 
                                     
                        a = ((x / y)*z); 
                        this.tb_1.Text = Math.Round(a,2).ToString(); 
                             
                    } 
                 
                         
            } 
         
                 
             
        } 

    Tuesday, February 3, 2009 4:18 AM
  • I found several problems by setting a breakpoint on 2 lines, first one point and then another.

    I first set one here.  The values for x, y, z were zero or not defined well. 
    a = ((x / y)*z);   // set 1st breakpoint here. 
     I moved up and set a breakpoint here to step through the code, and found a few road blocks.
    double a;    //  Set 2nd breakpoint here. 

    I refactored your code to make easier on my bad eyes.  All problems were within the button click handler.  See comments.



    public partial class Form1 : Form  
        {  
            double x, y, z;  
     
            List<string> amperage;  
            List<string> cable_size1;  
            List<string> system_type_list;  
     
            Dictionary<stringdouble> amperage_dictionary;  
            Dictionary<stringdouble> cable_dictionary_1;  
            Dictionary<stringdouble> system_type;  
     
            public Form1()  
            {  
                InitializeComponent();  
                //this.label1.TextAlign = ContentAlignment.MiddleCenter;  
            }  
     
            private void Form1_Load(object sender, EventArgs e)  
            {  
                // Each of the following lists populate the ComboBoxes   
                // to be selected on the main form.               
                amperage = new List<string>();  
                cable_size1 = new List<string>();  
                system_type_list = new List<string>();  
                //  
                InitializeCurrentLoadCB();  
                InitializeCableSizeCB();  
                InitializeSystemTypeCB();  
     
                //---------------Dictionaries-------  
                InitializeAmperageLookup();  
                InitializeCableLookup();  
                InitializeSystemTypeLookup();  
            }  
     
            private void InitializeCurrentLoadCB()  
            {  
                //Amperage List, No Exclusions-------------    
                amperage.Add("Please Select Amperage");  
                amperage.Add("400");  
                amperage.Add("800");  
                amperage.Add("1000");  
                amperage.Add("1200");  
                amperage.Add("1600");  
                amperage.Add("2000");  
                amperage.Add("2500");  
                amperage.Add("3000");  
                amperage.Add("3200");  
                amperage.Add("4000");  
                amperage.Add("5000");  
                amperage.Add("6000");  
     
                comboBox1.DataSource = amperage;  
                comboBox1.SelectedIndex = 0;  
                comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;  
            }  
     
            private void InitializeCableSizeCB()  
            {  
                //Cable List, No Exclusions -----------------  
                cable_size1.Add("Please Select Cable Size");  
                cable_size1.Add("#1");  
                cable_size1.Add("1/0");  
                cable_size1.Add("2/0");  
                cable_size1.Add("3/0");  
                cable_size1.Add("4/0");  
                cable_size1.Add("250");  
                cable_size1.Add("300");  
                cable_size1.Add("400");  
                cable_size1.Add("500");  
                cable_size1.Add("600");  
                cable_size1.Add("700");  
                cable_size1.Add("750");  
     
                comboBox2.DataSource = cable_size1;  
                comboBox2.SelectedIndex = 0;  
                comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;  
            }  
     
            private void InitializeSystemTypeCB()  
            {  
                //System Type List   
                system_type_list.Add("Select System Type");  
                system_type_list.Add("3 Phase 3 Wire");  
                system_type_list.Add("3 Phase 4 Wire");  
     
                comboBox3.DataSource = system_type_list;  
                comboBox3.SelectedIndex = 0;  
                comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;  
                //Initial DataBind for cb_system type ComboBox       
            }  
     
              
     
            private void button1_Click(object sender, EventArgs e)  
            {               
                if (!String.IsNullOrEmpty(comboBox1.Text) &&   
                    (!String.IsNullOrEmpty(comboBox2.Text) &&   
                    (!String.IsNullOrEmpty(comboBox3.Text))))    
                    {       
                        double a;    //  Set 2nd breakpoint here.  
                        if (comboBox1.Text == ("400"))  // SelectedText means highlighted text.  
                            {    
                                x = amperage_dictionary["400"];                     
                            }  
     
                            if (comboBox1.Text == ("800"))    // SelectedText means highlighted text.  
                            {    
                                x = amperage_dictionary["800"];                     
                            }  
     
                            if (comboBox2.Text == ("#1"))    // SelectedText means highlighted text.  
                            {    
                                y = cable_dictionary_1["#1"];    // wrong lookup                  
                            }  
     
                            if (comboBox2.Text == ("1/0"))   // SelectedText means highlighted text.  
                            {  
                                y = cable_dictionary_1["1/0"];   //  wrong lookup               
                            }  
     
                            if (comboBox3.Text == ("3 Phase 3 Wire"))    // SelectedText means highlighted text.  
                            {    
                                z = system_type["3 Phase 3 Wire"];                      
                            }  
                            if (comboBox3.Text == ("3 Phase 4 Wire"))    // SelectedText means highlighted text.  
                            {    
                                z = system_type["3 Phase 4 Wire"];    
                            }    
                                        
                        a = ((x / y)*z);   // set 1st breakpoint here.  
                        this.textBox1.Text = Math.Round(a,2).ToString();    
                                
                    }   
     
            }  
     
            private void InitializeSystemTypeLookup()  
            {  
                //System Type Dictionary    
                system_type = new Dictionary<stringdouble>();  
                system_type.Add("Select System Type", 0);  
                system_type.Add("3 Phase 3 Wire", 3);  
                system_type.Add("3 Phase 4 Wire", 4);  
            }  
     
            private void InitializeCableLookup()  
            {  
                //Cable Dictionary 1 used for cable quantity calculation    
                cable_dictionary_1 = new Dictionary<stringdouble>();  
     
                cable_dictionary_1.Add("#1", 130);  
                cable_dictionary_1.Add("1/0", 150);  
                cable_dictionary_1.Add("2/0", 175);  
                cable_dictionary_1.Add("3/0", 200);  
                cable_dictionary_1.Add("4/0", 230);  
                cable_dictionary_1.Add("250", 255);  
                cable_dictionary_1.Add("300", 285);  
                cable_dictionary_1.Add("400", 355);  
                cable_dictionary_1.Add("500", 380);  
                cable_dictionary_1.Add("600", 720);  
                cable_dictionary_1.Add("750", 475);  
            }  
     
            private void InitializeAmperageLookup()  
            {  
                //Amperage Dictionary       
                amperage_dictionary = new Dictionary<stringdouble>();  
     
                amperage_dictionary.Add("400", 400);  
                amperage_dictionary.Add("800", 800);  
                amperage_dictionary.Add("1000", 1000);  
                amperage_dictionary.Add("1200", 1200);  
                amperage_dictionary.Add("1600", 1600);  
                amperage_dictionary.Add("2000", 2000);  
                amperage_dictionary.Add("2500", 2500);  
                amperage_dictionary.Add("3000", 3000);  
                amperage_dictionary.Add("3200", 3200);  
                amperage_dictionary.Add("4000", 4000);  
                amperage_dictionary.Add("5000", 5000);  
                amperage_dictionary.Add("6000", 6000);  
            }  
        } 

    =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Tejas_Grande Tuesday, February 3, 2009 6:21 PM
    Tuesday, February 3, 2009 2:47 PM
    Moderator
  • I guess I was not as close as I thought. Thank you for showing me the proper way to structure this code. I did run into a couple problems implementing this code. I had to add the following in the "form load"

                           amperage_dictionary = new Dictionary<string, double>(); 
                    cable_dictionary_1 = new Dictionary<string, double>(); 
                    system_type = new Dictionary<string, double>(); 

    Another issue I am having is that the code is throwing a KeyNotFoundException for the amperage_dictionary (both the 400 and 800A selections). Do you have any suggestions of where I might look to fix this error?

    Again thank you very much for the help. From here on out I am going to let you devote your time to helping others with their programming issues rather than consistently taking me through the basics, I should already know.

    Greg
    Tuesday, February 3, 2009 6:27 PM
  •  I included those Dictionary<> initializations in the each of the last three methods.  They are called during Form.Load.  As for your exceptions, sometimes it a good programming practice to verify that a key exists in a dictionary before you use it. 

    Which is why I have a habit of using the enum type as my Dictionary TKey.  As long as I use the Enum as the type for my key, then I have a type-safe guarantee that my key will be found.  No "key not found" exception to worry about.

    I think you just need to be careful as to how you create your lookups.  Your code needs a zillion more if statements.  You may wish to change those last three methods that initialize the lookups to look something like this.

     private void InitializeSystemTypeLookup()  
            {  
                //System Type Dictionary    
                system_type = new Dictionary<stringdouble>();  
                system_type.Add(this.system_type_list[0], 0);  
                system_type.Add(this.system_type_list[1], 3);  
                system_type.Add(this.system_type_list[2], 4);  
            } 

    So look at what happens to your if statements in the Button.Click handler....

    private void button1_Click(object sender, EventArgs e)  
            {               
                if (!String.IsNullOrEmpty(comboBox1.Text) &&   
                    (!String.IsNullOrEmpty(comboBox2.Text) &&   
                    (!String.IsNullOrEmpty(comboBox3.Text))))    
                    {       
                        double a;    //  Set 2nd breakpoint here.  
                        if (comboBox1.Text == ("400"))  // SelectedText means highlighted text.  
                            {    
                                x = amperage_dictionary["400"];                     
                            }  
     
                            if (comboBox1.Text == ("800"))    // SelectedText means highlighted text.  
                            {    
                                x = amperage_dictionary["800"];                     
                            }  
     
                            if (comboBox2.Text == ("#1"))    // SelectedText means highlighted text.  
                            {    
                                y = cable_dictionary_1["#1"];    // wrong lookup                  
                            }  
     
                            if (comboBox2.Text == ("1/0"))   // SelectedText means highlighted text.  
                            {  
                                y = cable_dictionary_1["1/0"];   //  wrong lookup               
                            }  
     
                            //if (comboBox3.Text == ("3 Phase 3 Wire"))    // SelectedText means highlighted text.  
                            //{    
                            //    z = system_type["3 Phase 3 Wire"];                      
                            //}  
                            //if (comboBox3.Text == ("3 Phase 4 Wire"))    // SelectedText means highlighted text.  
                            //{    
                            //    z = system_type["3 Phase 4 Wire"];    
                            //}    
                            if (comboBox3.SelectedIndex != 0)  
                            {  
                                z = system_type[system_type_list[comboBox3.SelectedIndex]];  
                            }  
                                        
                        a = ((x / y)*z);   // set 1st breakpoint here.  
                        this.textBox1.Text = Math.Round(a,2).ToString();    
                                
                    }   
     
            } 

    ...instead of an if statement for EVERY possible value---it is easy to forget to set an if for a given value---one statement handles all of them.




    Mark the best replies as answers. "Fooling computers since 1971."

    EDIT:  That is a bug with the highlight tool!
    Tuesday, February 3, 2009 7:11 PM
    Moderator
  • I know I said that was the last post but........I have one last question for you; hopefully its an easy one. I am getting the Exception "System.ArgumentOutOfRangeException" (Index was out of range. Must be non_negitive and less than the size of the collection) on the dictionaries when the code loads. What you posted last was exactly what I was looking for!
    Tuesday, February 3, 2009 9:21 PM
  • Tejas_Grande said:

    I know I said that was the last post but........I have one last question for you; hopefully its an easy one. I am getting the Exception "System.ArgumentOutOfRangeException" (Index was out of range. Must be non_negitive and less than the size of the collection) on the dictionaries when the code loads. What you posted last was exactly what I was looking for!


    You force me to guess.  Which Dictionary?  Which line?  Code I wrote, or what you wrote?  What was the bad index value that was used?

    Make sure that your List<> and the Dictionary<> contain the same number of elements.  They must be in sync top to bottom.  Notice how my sample adds an element at index zero with a value of zero.  Why would I do that?  The first element at index zero in your list is not data.  It is a message. 

    comboBox3.DataSource = system_type_list;     
    comboBox3.SelectedIndex = 0;     
    comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;    
     

    Notice how I set the initial index to zero, the message.  The Dictionary needs an entry at element zero that "does nothing".  You also need to watch out for selections that can result in a divide by zero.  There are combinations that will cause it.  Program defensively.

    Look at the button.Click handler.  Notice how I check to makes sure that the comboBox.SelectedIndex != 0.  For every element in the List<> make sure you add an entry into the Dictionary<>, including an entry at index zero.
    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, February 4, 2009 12:18 AM
    Moderator
  • I should have posted the code; Im sorry. As far as I can tell I am getting that exception right off the bat. If I comment out that line to see if I can get the program to compile it simply errors out on the next entry of the dictionary. It starts with the amperage dictionary but I think it is probably the same for all of them.

    EDIT Some additional information: If you swap the InitializeXX(); for the dictionaries the exception moves to the next dictionary initialized.

    partial class MainForm 
        { 
            double x, y, z; 
             
            List<string> amperage; 
            List<string> cable_size1; 
            List<string> system_type_list; 
             
            Dictionary<string, double> amperage_dictionary; 
            Dictionary<string, double> cable_dictionary_1; 
            Dictionary<string, double> system_type;      

    private void MainFormLoad(object sender, EventArgs e) 
            { 
                    //Each of the following lists populate the ComboBoxes 
                    //to be selected on the main form. 
                    amperage = new List<string>(); 
                    cable_size1 = new List<string>(); 
                    system_type_list = new List<string>(); 
                     
                    amperage_dictionary = new Dictionary<string, double>(); 
                    cable_dictionary_1 = new Dictionary<string, double>(); 
                    system_type = new Dictionary<string, double>(); 
                    // 
                    InitializeCurrentLoadCB(); 
                    InitializeCableSizeCB(); 
                    InitializeSystemTypeCB(); 
                     
                    //---------------Dictionaries--------------------------------------------------------- 
                    InitializeAmperageLookup(); 
                    InitializeCableLookup(); 
                    InitializeSystemTypeLookup(); 
            } 
            private void InitializeCurrentLoadCB() 
            { 
                    //Amperage List, No Exclusions----------------------------------------------------------- 
                    List<string> amperage = new List<string>(); 
                    amperage.Add("Please Select Amperage"); 
                    amperage.Add("400"); 
                    amperage.Add("800"); 
                    amperage.Add("1000"); 
                    amperage.Add("1200"); 
                    amperage.Add("1600"); 
                    amperage.Add("2000"); 
                    amperage.Add("2500"); 
                    amperage.Add("3000"); 
                    amperage.Add("3200"); 
                    amperage.Add("4000"); 
                    amperage.Add("5000"); 
                    amperage.Add("6000"); 
                 
                    cb_test_1.DataSource = amperage
                    cb_test_1.SelectedIndex = 0
                    cb_test_1.DropDownStyle = ComboBoxStyle.DropDownList; 
            } 
            private void InitializeCableSizeCB() 
            { 
                    //Cable List, No Exclusions -------------------------------------------------------------- 
                    List<string> cable_size1 = new List<string>(); 
                    cable_size1.Add("Please Select Cable Size"); 
                    cable_size1.Add ("#1"); 
                    cable_size1.Add ("1/0"); 
                    cable_size1.Add ("2/0"); 
                    cable_size1.Add ("3/0"); 
                    cable_size1.Add ("4/0"); 
                    cable_size1.Add ("250"); 
                    cable_size1.Add ("300"); 
                    cable_size1.Add ("400"); 
                    cable_size1.Add ("500"); 
                    cable_size1.Add ("600"); 
                    cable_size1.Add ("700"); 
                    cable_size1.Add ("750"); 
                 
                    cb_test_2.DataSource = cable_size1
                    cb_test_2.SelectedIndex = 0
                    cb_test_2.DropDownStyle = ComboBoxStyle.DropDownList; 
                    //Initial DataBind for cable size ComboBox 
            } 
            private void InitializeSystemTypeCB() 
            { 
                    //System Type List   
                    List<string> system_type_list = new List<string>(); 
                    system_type_list.Add("Select System Type"); 
                    system_type_list.Add("3 Phase 3 Wire"); 
                    system_type_list.Add("3 Phase 4 Wire"); 
                 
                    cb_test_3.DataSource = system_type_list
                    cb_test_3.SelectedIndex = 0
                    cb_test_3.DropDownStyle = ComboBoxStyle.DropDownList; 
                    //Initial DataBind for cb_system type ComboBox               
            } 
             
             
            private void Button1Click(object sender, System.EventArgs e) 
            {    
                 
                if (!String.IsNullOrEmpty(cb_test_1.Text) && 
                   (!String.IsNullOrEmpty(cb_test_2.Text) &&  
                   (!String.IsNullOrEmpty(cb_test_3.Text)))) 
                    {    
                        double a; 
                        if (cb_test_1.SelectedIndex != 0) 
                            { 
                                x = amperage_dictionary[amperage[cb_test_1.SelectedIndex]];                  
                            }                                                        
                     
                        if (cb_test_2.SelectedIndex != 0) 
                            { 
                                y = cable_dictionary_1[cable_size1[cb_test_2.SelectedIndex]];                    
                            }    
                                     
                        if (cb_test_3.SelectedIndex != 0) 
                            { 
                                z = system_type[system_type_list[cb_test_3.SelectedIndex]]; 
                            }            
                                     
                        a = ((x / y)*z); 
                        this.tb_1.Text = Math.Round(a,2).ToString(); 
                             
                    } 
                                 
            } 
            private void InitializeSystemTypeLookup() 
            { 
                //System Type Dictionary 
                system_type = new Dictionary<string, double>(); 
                system_type.Add(this.system_type_list[0], 0); 
                system_type.Add(this.system_type_list[1], 3); 
                system_type.Add(this.system_type_list[2], 4); 
            } 
            private void InitializeCableLookup() 
            { 
                //Cable Dictionary 1 used for cable quantity calculation 
                cable_dictionary_1 = new Dictionary<string, double>(); 
                cable_dictionary_1.Add (this.cable_size1[0], 0); 
                cable_dictionary_1.Add (this.cable_size1[1], 130); 
                cable_dictionary_1.Add (this.cable_size1[2], 150); 
                cable_dictionary_1.Add (this.cable_size1[3], 175); 
                cable_dictionary_1.Add (this.cable_size1[4], 200); 
                cable_dictionary_1.Add (this.cable_size1[5], 230); 
                cable_dictionary_1.Add (this.cable_size1[6], 255); 
                cable_dictionary_1.Add (this.cable_size1[7], 285); 
                cable_dictionary_1.Add (this.cable_size1[8], 355); 
                cable_dictionary_1.Add (this.cable_size1[9], 380); 
                cable_dictionary_1.Add (this.cable_size1[10], 720); 
                cable_dictionary_1.Add (this.cable_size1[11], 475); 
            } 
            private void InitializeAmperageLookup() 
            { 
                //Amperage Dictionary    
                amperage_dictionary = new Dictionary<string, double>(); 
                amperage_dictionary.Add(this.amperage[0], 0); 
                amperage_dictionary.Add(this.amperage[1], 400); 
                amperage_dictionary.Add(this.amperage[2], 800); 
                amperage_dictionary.Add(this.amperage[3], 1000); 
                amperage_dictionary.Add(this.amperage[4], 1200); 
                amperage_dictionary.Add(this.amperage[5], 1600); 
                amperage_dictionary.Add(this.amperage[6], 2000); 
                amperage_dictionary.Add(this.amperage[7], 2500); 
                amperage_dictionary.Add(this.amperage[8], 3000); 
                amperage_dictionary.Add(this.amperage[9], 3200); 
                amperage_dictionary.Add(this.amperage[10], 4000); 
                amperage_dictionary.Add(this.amperage[11], 5000); 
                amperage_dictionary.Add(this.amperage[12], 6000);        
            } 
             
             
        } 

    • Edited by Tejas_Grande Wednesday, February 4, 2009 12:57 AM Additional Info
    Wednesday, February 4, 2009 12:27 AM
  • You try to redeclare the List<> variables.  This is my code.

    private void InitializeSystemTypeCB()  
            {  
                //System Type List      
                system_type_list = new List<string>();  
                system_type_list.Add("Select System Type");  
                system_type_list.Add("3 Phase 3 Wire");  
                system_type_list.Add("3 Phase 4 Wire");  
     
                comboBox3.DataSource = system_type_list;  
                comboBox3.SelectedIndex = 0;  
                comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;  
                //Initial DataBind for cb_system type ComboBox                  
            } 


    This is your code.

    private void InitializeSystemTypeCB()  
            {  
                //System Type List      
                List<string>  system_type_list = new List<string>();  
                system_type_list.Add("Select System Type");  
                system_type_list.Add("3 Phase 3 Wire");  
                system_type_list.Add("3 Phase 4 Wire");  
     
                comboBox3.DataSource = system_type_list;  
                comboBox3.SelectedIndex = 0;  
                comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;  
                //Initial DataBind for cb_system type ComboBox                  
            } 

    Get rid of that.  You declaring a local variable with the same name.  The variable declared at the class level never gets initialized, so it remains null.  That mistake appears 3 times, inside each of the Initialize****CB() methods.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Edited by Rudedog2Moderator Wednesday, February 4, 2009 1:03 AM wrong method posted
    • Marked as answer by Tejas_Grande Wednesday, February 4, 2009 2:03 AM
    Wednesday, February 4, 2009 12:59 AM
    Moderator
  •  I think i fixed more than I just posted.  You have too many definition declarations.  You had the same problem with the Dictionaries<>, too. 

    When you place the class' type name to the left of the variable name you are declaringa new variable definition, not working with the same one.  We declared the variables names and their types at the top of the code.

    double x, y, z;  
     
            List<string> amperage;  
            List<string> cable_size1;  
            List<string> system_type_list;  
     
            Dictionary<stringdouble> amperage_dictionary;  
            Dictionary<stringdouble> cable_dictionary_1;  
            Dictionary<stringdouble> system_type; 

    Once we do that, we only need to use the variable name.  I was able to observe the error by setting a watch on "amperage", and noticed that it went to null when the method to initialize it was called ended.   I blame myself for this error.  I should have used the "this" reference to access those fields listed above.  I suggest that you modify your code to the same.  I left them out partially to avoid confusion, but wound up creating it.  My bad.

    private void InitializeSystemTypeLookup()  
            {  
                //System Type Dictionary    
                this.system_type = new Dictionary<stringdouble>();  
                this.system_type.Add(this.system_type_list[0], 0);  
                this.system_type.Add(this.system_type_list[1], 3);  
                this.system_type.Add(this.system_type_list[2], 4);  
            } 

    Make all of your code look like that.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Tejas_Grande Wednesday, February 4, 2009 2:03 AM
    Wednesday, February 4, 2009 1:11 AM
    Moderator
  • A thousand times thank you!!!!!! I works!!!!! I am sorry it took so long. You have been beyond helpful and patient.

    Kind Regards,
    Greg
    Wednesday, February 4, 2009 2:06 AM