1

I am trying to compare the two randomly generated lists, they need to compare the totals of the dice within the code and return the amount of duplicates of the number selected by the user. The user has a numeric up and down box to select the number between 1-12 and the program will return how many of the specified number is list between the two lists.

       int die1, die2;
        int[] roll1 = new int[101];
        int[] roll2 = new int[101];

        //set Array
        Random dice = new Random();

    private void DiceButton_Click_1(object sender, EventArgs e)
    {




        //sets value to the variables and for loop to generate 100 rolls
        for (int i = 1; i < 101; i++)
        {
            die1 = dice.Next(1, 7);
            die2 = dice.Next(1, 7);

            //displays picture of dice
            LblDie1.ImageIndex = die1;
            LblDie2.ImageIndex = die2;

            //displays roll in listbox
            roll1.SetValue(die1 + die2, i);
            listBox1.Items.Add(die1.ToString() + " + " + die2.ToString() + " = " + (die1 + die2).ToString() + "  Roll: " + i.ToString());
        }
        for (int i = 1; i < 101; i++)
        {
            die1 = dice.Next(1, 7);
            die2 = dice.Next(1, 7);

            //displays picture of dice
            LblDie1.ImageIndex = die1;
            LblDie2.ImageIndex = die2;

            //displays roll in listbox
            roll2.SetValue(die1 + die2, i);
            listBox2.Items.Add(die1.ToString() + " + " + die2.ToString() + " = " + (die1 + die2).ToString() + "  Roll: " + i.ToString());
        }
    }

    private void SearchButton_Click(object sender, EventArgs e)
    {
        var count = roll1.Intersect(roll2);

        foreach(var number in count)

            listBox4.Items.Add(count.ToString()); 
    }
}

}

6
  • 2
    Why not use List1.Intersect(List2)?
    – yu_ominae
    Jul 9, 2015 at 1:14
  • what is your question? Jul 9, 2015 at 1:14
  • I am using the intersect method, but I need to have the program respond to a user selecting the value of 1-12 on a numerical up and down box. I cannot figure out how to write the code for that.
    – kyrosds
    Jul 9, 2015 at 1:21
  • By the way, why are you not starting your indices at 0? Looking at your code I don't see any obvious reason for going from 1 to 101 instead of 0 to 100?
    – yu_ominae
    Jul 9, 2015 at 1:21
  • Well, you need to have a ListBox with values 1-12. When the user changes his selection, you get the value, say UserSelectedValue using the SelectedIndexChanged method and then do something like Results.Contains(UserSelectedValue). Is that what you're looking for? But then, what you should do is keep a list of the sum of each roll, so you can compare the selection to each roll.
    – yu_ominae
    Jul 9, 2015 at 1:25

1 Answer 1

1

You get the elements of both list boxes, so you get two lists, say List1 and List2, then you do List1.Intersect(list2) and the result is a third list containing all common elements to both List1 and List2.

What you then do with the third list is up to you, but as I understand your question, this seems to be what you are looking for.

Have a look here for more information https://msdn.microsoft.com/en-us/library/vstudio/bb910215(v=vs.90).aspx

UPDATE

I see what you are trying to do now.

NumericUpDown has got a ValueChanged event. Use that to get the current value selected by the user, UserSelectedValue. If you want to know how many times the selected value occurs in both lists simultaneously, you can do something like List1.Count(UserSelectedValue) + List2.Count(UserSelectedValue), which should return how many times the value selected by the user has popped up in both lists

6
  • Thank you very much for your input on the question. How would I go about linking a numeric up and down box to have the user select the value to be compared within the lists?
    – kyrosds
    Jul 9, 2015 at 1:23
  • Updated my answer, but still not sure if I fully understand what result you are trying to get...
    – yu_ominae
    Jul 9, 2015 at 1:35
  • OK I'm unsure of how I implement what you are saying into my code now. I understand what you are saying with the NumericUpDown and having a ValueChanged event but I need to figure out how to link it with the search button event and return the total of the selected value in the NumericUpDown to the list box.
    – kyrosds
    Jul 9, 2015 at 1:55
  • The first part is not very hard, you forget about the ValueChanged event and instead just get the currently selected value when the search button is clicked (using the .Value property). Then you carry out the calculation of the total on your lists and load that into the ListBox.
    – yu_ominae
    Jul 9, 2015 at 2:16
  • OK I've figured it out I needed to add like you said the var count1 = list1.Count(x => x.ToString == numericUpDown.Value.ToString);
    – kyrosds
    Jul 9, 2015 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.