having some real difficulties with a loop I have that checks lottery numbers against users' selected numbers stored in a database. At the moment, the loop (below) will get all the users who have 1 ball that matches the lottery, but I don't know how to store this and keep cycling so that I only display users with 5 balls or 6 balls :-(. here is my loop so far:

if (ball1 == usersball1 || ball2 == usersball1 || ball3 == usersball1 || ball4 == usersball1 || ball5 == usersball1 || ball6 == usersball1 || ball1 == usersball2 || ball2 == usersball2 || ball3 == usersball2 || ball4 == usersball2 || ball5 == usersball2 || ball6 == usersball2 || ball1 == usersball3 || ball2 == usersball3 || ball3 == usersball3 || ball4 == usersball3 || ball5 == usersball3 || ball6 == usersball3 || ball1 == usersball4 || ball2 == usersball4 || ball3 == usersball4 || ball4 == usersball4 || ball5 == usersball4 || ball6 == usersball4 || ball1 == usersball5 || ball2 == usersball5 || ball3 == usersball5 || ball4 == usersball5 || ball5 == usersball5 || ball6 == usersball5 || ball1 == usersball6 || ball2 == usersball6 || ball3 == usersball6 || ball4 == usersball6 || ball5 == usersball6 || ball6 == usersball6)
                {
                    
                    lblResult.Visible = true;
                    lblResult.Text = lblResult.Text + "flag: " + System.Convert.ToString(flag) + lblOthers.Text + System.Convert.ToString(row2["UserName"]) + " matched at least 1 ball!<br />";
                }

the ball1 etc are the numbers of the lottery and usersball1 etc are the users numbers in the database.

As I mentioned, this successfully outputs the users with 1 matching ball. I want to output users with 5 balls or over ideally. Any ideas how I can go about this?

thanks a LOT!

hey although the way your writing this can be done, it will be hard to maintain with all your permutations in the if statement. I have put together an example for you that uses lists to iterate through and uses built in Array methods to find out how many each person has.

public void StartCheck()
{
   /** Holds the lottery results, see the LotteryTicket class at the bottom **/
    LotteryTicket results = new LotteryTicket("The Lotto Results", 5, 12, 15, 9, 34, 23);

    /** Create a List of type LotteryTicket to hold each users results, you could populate this using a database **/
    List<LotteryTicket> listToCheck = new List<LotteryTicket>();
    listToCheck.Add(new LotteryTicket("Alan", 1, 2, 3, 4, 5, 6));
    listToCheck.Add(new LotteryTicket("John", 59, 58, 57, 56, 55, 54));
    listToCheck.Add(new LotteryTicket("Cheryl", 9, 4, 34, 1, 2, 23));

    /** Iterate through each ticket **/
    foreach(LotteryTicket ticket in listToCheck)
    {
        /** Counts the number of matches **/
        int numberOfMatches = 0;
        foreach (int number in ticket.Balls)
        {
            /** Compares the lottery results with the persons lottery ticket **/
            if (results.Balls.Contains(number))
            {
                /** If a match is found the number is incremented **/
                numberOfMatches++;
            }
        }
        /** Outputs a message to the console, you can change this to display your message to the web page **/
        Console.Out.WriteLine("{0} has matched {1} lottery numbers.", ticket.Name, numberOfMatches);
    }
}

/** Class to model a lottery ticket and the person who owns it **/
private class LotteryTicket
{
    /** The lottery tickets balls **/
    private int[] balls;
    /** The persons name **/
    private string name;

    /** Constructor, pass through the persons names and the balls selected **/
    public LotteryTicket(string name, int ball01, int ball02, int ball03, int ball04, int ball05, int ball06)
    {
        this.name = name;
        this.balls = new int[] {ball01, ball02, ball03, ball03, ball04, ball05, ball06};
    }

    /** Property to get the balls **/
    public int[] Balls
    {
        get { return this.balls; }
    }

    /** Property to get the name **/
    public string Name
    {
        get { return this.name; }
    }
}

the above code uses generic collections to hold the LotteryTicket object, these can be very powerful when used properly, but the simple use of the Array method 'Contains' does the check you were performing before.

This article has been dead for over six months. Start a new discussion instead.