Page 1 of 2 12 LastLast
Results 1 to 40 of 43

Thread: [RESOLVED] Sum Frequency Trouble

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Resolved [RESOLVED] Sum Frequency Trouble

    Hello Everyone,

    I am creating a program that rolls two dice 5 times and adds the values together. This information is shown in a listbox. The issue I am having is that I need the frequency of the sums to show in a second listbox. I have been searching everywhere and am having trouble finding something that will work. Here is the code for the program I have so far:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void RollDicebtn_Click(object sender, EventArgs e)
            {
                int firstDice = 0;
                int secondDice = 0;
                int sum = 0;
                Random randomNumber = new Random();
                int[] frequencies = new int[13]; //frequencies[2] is the frequency of the sum 2, frequencies[12] is for sum 12
    
                for (int i = 0; i < 5; i++)
                {
                    firstDice = randomNumber.Next(1, 6);
                    secondDice = randomNumber.Next(1, 6);
                    sum = firstDice + secondDice;
                    this.RollSumbox.Items.Add(
                        String.Format("1 Dice = {0}\t2 Dice = {1}\tSum = {2}", firstDice, secondDice, sum));
                    frequencies[sum]++;
                }
            }
        }
    }
    I need help with how to get the frequencies of the sums to appear in the second box. I am not asking for the codes to be done for me, I just need an example or something to get me started in the right direction. Like I said before, I have been searching everywhere with how to do this and nothing seems to be working for me. I am also attaching an image of the program running to help give you an idea of what I am doing.
    Attached Images Attached Images  

  2. #2

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Just to add a little more clarity, here is an example of what I am looking for in the second box:

    Frequency of sum 2 = 0
    Frequency of sum 3 = 0
    Frequency of sum 4 = 0
    Frequency of sum 5 = 2

    ... and so on all the way through 12. Thanks for any help, I truly appreciate it.

  3. #3
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Sum Frequency Trouble

    First thing that pops into my mind is to use a dictionary. Once you've rolled both dice, get the sum and check the dictionary to see if the sum exists as a key. If it does, increment the value by 1, if it doesn't then add the key and set the value to 1.

    Then, once you're done, you can loop through the dictionary to list all the sums you rolled and their frequency

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Thanks kfcSmitty, I was starting to think that no one would offer me any help. I had tried to use a dictionary once, but I wasn't able to get it to work. Basically, I am having trouble figuring out how to get it to loop through to list all the sums. I am not very good with coding and am trying to learn the best I can. I will continue to research how to use the dictionary properly but would also appreciate anymore help that I can get. I will check in throughout the day to see if anymore advice is giving or to let you know how I am doing. Thanks again.

  5. #5
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Sum Frequency Trouble

    Here is an example. I created an <int,int> dictionary object and initialized it. Then I loop through the dictionary and print it.

    C# Code:
    1. Dictionary<int, int> numberCount = new Dictionary<int, int>() {{1,1}, {4,1}, {5,3} };
    2.  
    3. foreach (int key in numberCount.Keys)
    4. {
    5.   Console.WriteLine(key + ": " + numberCount[key]);
    6. } //end foreach

    And below is the code to increment/add the value to the dictionary. The value I'm checking for is 1, but you would need to integrate it into your code appropriately.

    C# Code:
    1. if (numberCount.Keys.Contains(1))
    2. {
    3.   //if it contains the value you're trying to add -- aka 1 in this instance, then increment the call
    4.   numberCount[1]++;
    5. }
    6. else
    7. {
    8.   //otherwise, add the key to the dictionary and set the count to 1
    9.   numberCount.Add(1, 1);
    10. } //end if
    Last edited by kfcSmitty; Feb 3rd, 2013 at 09:03 PM.

  6. #6
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    Quote Originally Posted by kfcSmitty View Post
    Here is an example. I created an <int,int> dictionary object and initialized it. Then I loop through the dictionary and print it.

    C# Code:
    1. Dictionary<int, int> numberCount = new Dictionary<int, int>() {{1,1}, {4,1}, {5,3} };
    2.  
    3. foreach (int key in numberCount.Keys)
    4. {
    5.   Console.WriteLine(key + ": " + numberCount[key]);
    6. } //end foreach

    And below is the code to increment/add the value to the dictionary. The value I'm checking for is 1, but you would need to integrate it into your code appropriately.

    C# Code:
    1. if (numberCount.Keys.Contains(1))
    2. {
    3.   //if it contains the value you're trying to add -- aka 1 in this instance, then increment the call
    4.   numberCount[1]++;
    5. }
    6. else
    7. {
    8.   //otherwise, add the key to the dictionary and set the count to 1
    9.   numberCount.Add(1, 1);
    10. } //end if
    I support the use of a dictionary here. Although a dictionary is a collection of KeyValuePair's, you don't need to loop through the keys, and index the original collection if you just loop through the KeyValuePair's:

    C# Code:
    1. Dictionary<int, int> numberCount = new Dictionary<int, int>() { { 1, 1 }, { 4, 1 }, { 5, 3 } };
    2.  
    3. foreach (KeyValuePair<int, int> pair in numberCount)
    4. {
    5.     Console.WriteLine("{0} : {1}", pair.Key, pair.Value);
    6. }
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Ok thanks for the info guys, but I have to say I am completely lost on what is going on. Sorry if I am frustrating you all a bit.

  8. #8
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    Quote Originally Posted by ncgp81 View Post
    Ok thanks for the info guys, but I have to say I am completely lost on what is going on. Sorry if I am frustrating you all a bit.
    What part are you lost on?
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    When I try to create a dictionary the way explained by you guys, I get a few errors. One error is with Console.WriteLine, I get an error under WriteLine that says "System.Console.WriteLine(string, params object[]) is a 'method' but is used like a 'type'". Another problem I am having is calling my sum. I get an error and the options to either create a new type or a new method. I am sure that it this is due to mistakes that I am making by not fully understanding how to code, but I am having trouble figuring out how I am suppose to properly do this.

  10. #10
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    Let's see your code then I can't predict exactly what you're doing wrong, but if I can see what you're doing, maybe I can show you.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  11. #11

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Sorry I deleted everything and tried something else. I do not remember exactly what I had so I just copied what Smitty had and tried to change it the way I had it, which I did not do correctly. I rushed to try it again so that I did not keep you waiting long so here is what it is now:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void RollDicebtn_Click(object sender, EventArgs e)
            {
                int firstDice = 0;
                int secondDice = 0;
                int sum = 0;
                Random randomNumber = new Random();
                int[] frequencies = new int[13]; //frequencies[2] is the frequency of the sum 2, frequencies[12] is for sum 12
    
                for (int i = 0; i < 5; i++)
                {
                    firstDice = randomNumber.Next(1, 6);
                    secondDice = randomNumber.Next(1, 6);
                    sum = firstDice + secondDice;
                    this.RollSumbox.Items.Add(
                        String.Format("1 Dice = {0}\t2 Dice = {1}\tSum = {2}", firstDice, secondDice, sum));
                    frequencies[sum]++;
                }
            }
    
                Dictionary<int, int> numberCount = new Dictionary<int, int>() {{1,1}, {4,1}, {5,3} };
     
                foreach (int sum in tSum.RollSumbox)
        {
                Console.WriteLine(sum + ": " + numberCount[sum]);
        }
                if (numberCount.Keys.Contains(1))
        {
                numberCount[1]++;
        }
                else
        {
                numberCount.Add(1, 1);
        }
    
        }
    }
    I am probably going to delete this again and try some other things that I am finding through research, but I will definitely check back in a few minutes so that I can get your advice.

  12. #12
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    That looks like a bunch of jumble to me.. some of that code is trying to run outside of the scope of a method as if it was some kind of code routine that should be in a method...

    I'm not going to just fix the code for you though, if this is different than what you had before, but you haven't even mentioned what you were having trouble with in this new "revision". I'll point out that you shouldn't be defining a new instance of the Random class each time that button is clicked though:
    Code:
    Random randomNumber = new Random();
    Put that outside of the button click method. Take a look here for more information: http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx

    ~Ace
    Last edited by AceInfinity; Feb 3rd, 2013 at 11:50 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  13. #13

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    This new revision was just thrown together quickly, I didn't want to keep you waiting long and was hoping to remember what it was I had exactly but couldn't. The first part of my code works fine, it is just where I try creating the dictionary that I start getting errors. It does still show the same error with the Console.WriteLine part though. I will try to get it back the way I had it and repost but it might take me a little bit to recall it exactly how it was. And I would not want you to just fix it for me because I would not learn anything from that and the program would be pointless if I did not learn how the codes work. On a side note, I just found something about Enumerators. Do you think this is something that might help me? Sorry I am trying but I always seem to have great difficulties with learning and understanding coding.

  14. #14
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    Do you think this is something that might help me?
    Not unless you want to understand more about how the foreach works or if you're building your own collection. But this is much more advanced than what you are probably currently expected to know at your skill level so far.

    it is just where I try creating the dictionary that I start getting errors. It does still show the same error with the Console.WriteLine part though
    I know: "That looks like a bunch of jumble to me.. some of that code is trying to run outside of the scope of a method as if it was some kind of code routine that should be in a method... "

    Can't just paste code in and expect it to work
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  15. #15

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Ok, then I will go back to what I was trying to do with the information you provided me, I do not want to get too much past anything within my skill level. Let me mess with the coding a bit and I will post again, it may take me a little while because I am also trying to read things to try getting a better understanding of what I am trying to do and what the codes mean.

  16. #16
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    If you want to get the sense of figuring this out on your own, i'll give you a hint. Look at the brackets in your code and the way they match up. All bits of code that 'actually do something' should be within a method.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  17. #17

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Hello Ace and anyone else that would like to take a look and provide assistance,
    I had trouble with figuring out the Dictionary and making it work. I had someone in my class give me some direction on how else I could do this. Unfortunately, I am not allowed to post my code to my classmate only discuss what we are doing. So I am posting my updated code here and would like any advice:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void RollDicebtn_Click(object sender, EventArgs e)
            {
                int firstDice = 0;
                int secondDice = 0;
                int sum = 0;
                Random randomNumber = new Random();
                int [] frequencies = new int[11];
                int offset = 2; //frequencies[2] is the frequency of the sum 2, frequencies[12] is for sum 12
    
                for (int i = 0; i < 5; i++)
                {
                    firstDice = randomNumber.Next(1, 6);
                    secondDice = randomNumber.Next(1, 6);
                    sum = firstDice + secondDice;
                    this.RollSumbox.Items.Add(
                        String.Format("1 Dice = {0}\t2 Dice = {1}\tSum = {2}", firstDice, secondDice, sum));
                    frequencies[sum]++;
                }
    
                // TODO - display the frequencies array elements as listbox2 items
                for (int i = 0; i<11; i++)
                Console.WriteLine("{0} : {1}", i + offset, frequencies[i]);
                ListBox.Add(String.Format("{0} : {1}", i + offset, frequencies[i]);
                }
    
            }
        }
    }
    I am having an issue with the Add command. The error I get is: "'System.Windows.Forms.ListBox' does not contain a definition for 'Add'". Any help or suggestions will be greatly appreciated.

  18. #18
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Sum Frequency Trouble

    What is the name of your ListBox? You're trying to reference it in an incorrect way.. you're referencing the object type rather than the object itself.

    Example:

    C# Code:
    1. //right way to do it
    2. ListBox listbox_to_add_to = new ListBox();
    3. listbox_to_add_to.Add("woozle");
    4.  
    5. //what you're doing
    6. ListBox.Add("woozle");

    Find out what you've called your ListBox and use that name to reference the object in the code-behind

  19. #19
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    kfcSmitty is right, what you're doing, and hopefully you did not name your ListBox, "ListBox"... Is referencing the System.Windows.Forms.ListBox class, instead of the instance of the ListBox, that you want to add the item to.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  20. #20

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Sorry guys,

    The name of my listbox is 'Freqbox' since that is where the frequencies are to appear. I really do not know why I changed it to listbox before I posted this. When I have it as 'Freqbox' I am still getting the same error.

  21. #21
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    Quote Originally Posted by ncgp81 View Post
    Sorry guys,

    The name of my listbox is 'Freqbox' since that is where the frequencies are to appear. I really do not know why I changed it to listbox before I posted this. When I have it as 'Freqbox' I am still getting the same error.
    What error are you getting? There's no way you should be getting the same error unless Freqbox is some kind of custom ListBox class.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  22. #22

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    The error I am getting when I use Freqbox is:
    "'System.Windows.Forms.ListBox' does not contain a definition for 'Add' accepting a first argument of type 'System.Windows.Forms.ListBox' could be found (are you missing a using directive or an assembly reference?)"

  23. #23

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Ok, here is where I am at now. I tried using the suggestion from smitty and I still get a couple errors. When I use this code:
    Code:
    Listbox Freqbox = new ListBox();
                Freqbox.Add(String.Format("{0} : {1}", i + offset, frequencies[i]);
    I get this error under listbox:
    "The type or namespace 'ListBox' could not be found (are you missing a using directive or an assembly reference?)"
    Then when I try this code:
    Code:
    Freqbox listbox_to_add_to = new ListBox();
                listbox_to_add_to.Add(String.Format("{0} : {1}", i + offset, frequencies[i]);
    I get the following error with Freqbox:
    "'WindowsFormApplication1.Form1.Freqbox' is a 'field' but is used like a 'type'".
    What am I not doing right?

  24. #24
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    Why are you declaring a new instance of a ListBox dynamically though?

    This is wrong though:
    Code:
    Freqbox listbox_to_add_to = new ListBox();
                listbox_to_add_to.Add(String.Format("{0} : {1}", i + offset, frequencies[i]);
    You're trying to use Freqbox, which is already declared as a ListBox, as a type for a new variable named listbox_to_add_to... Which makes no sense.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  25. #25

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Here is my full code as it is right now:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void RollDicebtn_Click(object sender, EventArgs e)
            {
                int firstDice = 0;
                int secondDice = 0;
                int sum = 0;
                Random randomNumber = new Random();
                int [] frequencies = new int[11];
                int offset = 2; //frequencies[2] is the frequency of the sum 2, frequencies[12] is for sum 12
    
                for (int i = 0; i < 5; i++)
                {
                    firstDice = randomNumber.Next(1, 6);
                    secondDice = randomNumber.Next(1, 6);
                    sum = firstDice + secondDice;
                    this.RollSumbox.Items.Add(
                        String.Format("1 Dice = {0}\t2 Dice = {1}\tSum = {2}", firstDice, secondDice, sum));
                    frequencies[sum]++;
                }
    
                // TODO - display the frequencies array elements as listbox2 items
                for (int i = 0; i<11; i++)
                Console.WriteLine("{0} : {1}", i + offset, frequencies[i]);
                Freqbox listbox_to_add_to = new ListBox();
                listbox_to_add_to.Add(String.Format("{0} : {1}", i + offset, frequencies[i]);
                }
    
            }
        }
    }

  26. #26
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    Re-read my post:
    Code:
    Freqbox listbox_to_add_to = new ListBox();
    ListBox is a type, Freqbox is not, so I'm not sure what you're doing there. Freqbox was declared as an instance of a ListBox, but it is not a Type. The ListBox is a Type, of Control.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  27. #27

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    So should I replace Freqbox with ListBox and then replace new Listbox with Freqbox? Sorry, I am not too sure what I am doing and all of my research has not really helped me to understand any better either. I just cannot seem to get the hang of this.

  28. #28
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    If this is making you struggle, then you should be going back to the fundamentals of C# first with variables and types. Declaring a ListBox dynamically won't do anything unless you add it to the form container and you assign it's other properties to define a placement and all of that... Why aren't you using the Designer view for this? I don't understand. :S

    But if this confuses you, I really think it would be helpful and beneficial to your understanding if you just backtrack a little and learn about the Designer and adding/managing controls.

    Frequencies, assuming it's a ListBox , looks like it's already on the form, so you shouldn't have to declare a new instance of the ListBox anyways... It's already on your Form.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  29. #29

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    That's where I get confused too. This is my first C# class and only the third project I have had to do. I do not know why, but I have always had trouble grasping coding and the different languages (Java, HTML, etc...). This is why programming is not my major but I have to take these classes to meet my IT requirements. I will continue to do research but it all just seems to confuse me more the more I try. You have been the most help that I have been able to find and I truly thank you for the help and for your time. I will continue trying to figure this all out though I feel that it may be a lost cause. Thank you again for all of your assistance.

  30. #30
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    When you add a control to a form, in the hidden designer code (which you can see in your solution explorer if you choose the option to show all files), a variable is already assigned kind of like a 'handle' to that control. By default, your first ListBox added should follow the naming standard 'listBox1' which is assigned by the time you drag the ListBox to the form automatically. Your next ListBox if you were to add another, 'listBox2', and so on. If I were a betting man, that Frequencies listbox should have the name 'listBox2' if I'm right. Assuming you didn't change the designer names of any of the controls, and you added the Frequencies ListBox second.

    By trying to dynamically create a new instance of a System.Windows.Forms.ListBox, you're creating a new listbox, and one that is definitely not the one you are seeing on the form, when you added it via the designer view.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  31. #31

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    You are correct, the frequencies box was indeed the second listbox that I added, but I did change the name to Freqbox when I added it. I am looking at the designer code (it is called Form1.Designer.cs if I am looking at the right thing) and I see Freqbox there. It is shown in the codes as this:
    Code:
    private System.Windows.Forms.ListBox Freqbox;
    This is what I am looking for and how it should be, correct?

  32. #32
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    Yes... See how it's already a ListBox, and that ListBox is the type of that variable?

    Freqbox.Add(). You don't need to do ANYTHING like this:
    Code:
    Freqbox listbox_to_add_to = new ListBox();
    Any line where you're using the 'new' keyword and trying to create a new instance of a ListBox with it, no.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  33. #33

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Ok, so if I am following correctly then do I want my code to be:
    Code:
    Console.WriteLine("{0} : {1}", i + offset, frequencies[i]);
                Freqbox.Add();
    If so I am still getting the error that it does not contain a definition for 'Add'. I don't want to add it to the Designer codes do I?
    Here is my entire code right now if you need to see it:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void RollDicebtn_Click(object sender, EventArgs e)
            {
                int firstDice = 0;
                int secondDice = 0;
                int sum = 0;
                Random randomNumber = new Random();
                int [] frequencies = new int[11];
                int offset = 2; //frequencies[2] is the frequency of the sum 2, frequencies[12] is for sum 12
    
                for (int i = 0; i < 5; i++)
                {
                    //Sets the dice to roll a random number
                    firstDice = randomNumber.Next(1, 6);
                    secondDice = randomNumber.Next(1, 6);
                    //Adds the values of the two dice
                    sum = firstDice + secondDice;
                    this.RollSumbox.Items.Add(
                        String.Format("1 Dice = {0}\t2 Dice = {1}\tSum = {2}", firstDice, secondDice, sum));
                    frequencies[sum]++;
                }
    
                // TODO - display the frequencies array elements as listbox2 items
                for (int i = 0; i<11; i++)
                Console.WriteLine("{0} : {1}", i + offset, frequencies[i]);
                Freqbox.Add();
                
                }
    
            }
        }
    And here is the code from the Designer:
    Code:
    namespace WindowsFormsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.RollDicebtn = new System.Windows.Forms.Button();
                this.RollSumbox = new System.Windows.Forms.ListBox();
                this.Freqbox = new System.Windows.Forms.ListBox();
                this.SuspendLayout();
                // 
                // RollDicebtn
                // 
                this.RollDicebtn.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.RollDicebtn.Location = new System.Drawing.Point(78, 25);
                this.RollDicebtn.Name = "RollDicebtn";
                this.RollDicebtn.Size = new System.Drawing.Size(285, 81);
                this.RollDicebtn.TabIndex = 0;
                this.RollDicebtn.Text = "Roll Dice and Display";
                this.RollDicebtn.UseVisualStyleBackColor = true;
                this.RollDicebtn.Click += new System.EventHandler(this.RollDicebtn_Click);
                // 
                // RollSumbox
                // 
                this.RollSumbox.FormattingEnabled = true;
                this.RollSumbox.Location = new System.Drawing.Point(78, 140);
                this.RollSumbox.Name = "RollSumbox";
                this.RollSumbox.Size = new System.Drawing.Size(285, 134);
                this.RollSumbox.TabIndex = 1;
                // 
                // Freqbox
                // 
                this.Freqbox.FormattingEnabled = true;
                this.Freqbox.Location = new System.Drawing.Point(78, 304);
                this.Freqbox.Name = "Freqbox";
                this.Freqbox.Size = new System.Drawing.Size(285, 134);
                this.Freqbox.TabIndex = 2;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(457, 467);
                this.Controls.Add(this.Freqbox);
                this.Controls.Add(this.RollSumbox);
                this.Controls.Add(this.RollDicebtn);
                this.Name = "Form1";
                this.Text = "Everett- Dice Sum Frequencies";
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button RollDicebtn;
            private System.Windows.Forms.ListBox RollSumbox;
            private System.Windows.Forms.ListBox Freqbox;
        }
    }

  34. #34

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Ok, I solved the Add definition problem by changing it to
    Code:
    Freqbox.Items.Add(String.Format("{0} : {1}", i + offset, frequencies[i]));
    All I needed was to add 'Items' to the code. Now my only issue is with the two instances of 'i' that I have in that line of code. I am getting this error: "The name 'i' does not exist in the current context". I am confused by this since it is used in the lines above it without any errors.

  35. #35

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    To add to my last post, I do get two options with the 'i' errors: Generate property stub or Generate field stub. I am not sure if I should choose one of these or should I try something else?

  36. #36
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    If that code you have is in this loop:
    Code:
    for (int i = 0; i<11; i++)
                Console.WriteLine("{0} : {1}", i + offset, frequencies[i]);
                Freqbox listbox_to_add_to = new ListBox();
                listbox_to_add_to.Add(String.Format("{0} : {1}", i + offset, frequencies[i]);
                }
    Then there would be no problem, otherwise, it is, as it says: i is not declared. There's no i to reference a value for a variable in the scope in which you're trying to use i from. Forgot about Items, i'm writing on my Surface so bare with me... And I cannot install Visual Studio either on this RT device.

    You shouldn't be doing anything with a stub here. There's no i variable being seen in that scope, you need to declare it. I think you're moving too fast however in C# programming ...

    I'm going to provide you with some references on scope: http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx

    You probably declared it before this loop or after it, and you'll see in that link why it's invalid when you try to use i. Trying to just copy and paste in code, you'll get no where, if you want to pass this course you're going to have to slow down, read, and learn. I would suggest looking up the C# reference on MSDN.

    I really urge you to read though, I can't teach you anything at this point if you don't understand the fundamentals yet. You'll be using code and taking in advice you don't understand, which is basically no more productive than running in circles, until you get some traction and know your do's/don'ts and some of the basics for the language you're trying to use.

    Good luck

    ~Ace
    Last edited by AceInfinity; Feb 5th, 2013 at 02:39 AM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  37. #37

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    I have actually gotten rid of all errors by making a couple of changes to my code. Here is my entire code now:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void RollDicebtn_Click(object sender, EventArgs e)
            {
                int firstDice = 0;
                int secondDice = 0;
                int sum = 0;
                Random randomNumber = new Random();
                int[] frequencies = new int[13]; //frequencies[2] is the frequency of the sum 2, frequencies[12] is for sum 12
    
                for (int i = 0; i < 5; i++)
                {
                    firstDice = randomNumber.Next(1, 6);
                    secondDice = randomNumber.Next(1, 6);
                    sum = firstDice + secondDice;
                    this.RollSumbox.Items.Add(
                        String.Format("1 Dice = {0}\t2 Dice = {1}\tSum = {2}", firstDice, secondDice, sum));
                    frequencies[sum]++;
                }
    
                // TODO - display the frequencies array elements as listbox2 items
                 for (int i = 0; i<11; i++)
                Console.WriteLine(String.Format("1 Dice = {0}\t2 Dice = {1}\tSum = {2}", firstDice, secondDice, sum));
                Freqbox.Items.Add(String.Format("Frequency of sum 2 = ", sum + 1, frequencies[sum + 1]));
                }
    
            }
        }
    Everything is running good and I am close to getting what I need into my Freqbox listbox. When I run the program and press the button to roll the dice, I get "Frequency of sum 2 =" to appear into the Freqbox. Now I just have to figure out how to get it to actually show the counts of the total sums of 2, and do this for all sums up to 12. Once I figure this out then I am good to go. Thank you again Ace for all of your help, you have been a terrific person during this (as well as very patient). If you have any suggestions to give to lead me to getting the actual count and add each sum into the Freqbox, I would appreciate it. And I think you are right, it tends to seem that I am just running in circles. I just get so anxious, then frustrated when I can't seem to figure out the proper codes. I probably just need to slow down a bit. Anyways, all errors are gone and I feel that I am finally getting somewhere, and all that is left is figuring out how to get the sum count to actually show.

  38. #38

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    I am going to call it a night and will return in the morning to continue on this program before I go to work. Again Ace, I cannot thank you enough for your time, help, and patience. All that is left is to get the sum frequency counts to appear in the Freqbox. I just might need to be pointed in the right direction. I will also check out the link you posted tomorrow after I get some sleep and refresh. Thank you again for everything, it really has meant a lot to me.

  39. #39
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Sum Frequency Trouble

    The value for the sums should be in the Value in the KeyValuePair for the Dictionary. Note: You're not using String.Format correctly, there is no format for the parameters you've specified.
    Code:
    Freqbox.Items.Add(String.Format("Frequency of sum 2 = ", sum + 1, frequencies[sum + 1]));
    Code:
    "Frequency of sum 2 = "
    Where are they? {0} and {1}? Read this: http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  40. #40

    Thread Starter
    Member
    Join Date
    Dec 2012
    Posts
    51

    Re: Sum Frequency Trouble

    Hey Ace,
    It has been a lot of work and time, but I finally got my program to work the way I needed it to. It took a lot of reading, and trial and error, but it finally works properly. I cannot say that I fully understand everything yet, I was just trying things as I researched them. I thank you for all of your help and time. I still have a lot to learn with coding but I definitely would not have succeeded without your help. Thank you so much. Before I mark this thread as resolved, is there anything that I need to do to give you a rating or credit or anything to inform that you provided excellent assistance? I just want to make sure because I see you have "Please rate my post if this helped you out." in your signature and I definitely want you to get the credit you deserve. Also, I am posting my final code just so you can see how I finished it:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void RollDicebtn_Click(object sender, EventArgs e)
            {
                int firstDice = 0;
                int secondDice = 0;
                int sum = 0;
                Random randomNumber = new Random();
                int[] frequencies = new int[13]; //frequencies[0] is the frequency of the sum 2, frequencies[10] is for sum 12
                int offset = 0;
    
                for (int i = 0; i < 5; i++)
                {
                    firstDice = randomNumber.Next(1, 6);
                    secondDice = randomNumber.Next(1, 6);
                    sum = firstDice + secondDice;
                    this.RollSumbox.Items.Add(
                        String.Format("1 Dice = {0}\t2 Dice = {1}\tSum = {2}", firstDice, secondDice, sum));
                    frequencies[sum - offset]++;
                }
    
                for (int i = 2; i < 13; i++)
                {
                    Freqbox.Items.Add(String.Format("Frequency of sum {0} = {1}", i + offset, frequencies[i]));
                }
            }
        }
    }
    Again thank you and just let me know what I have to do to give you your rating.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width