1

I get the error below when I try to run the application I am sure its something simple but I dont see it. What I am trying to do it when I click a button I have labeled Play. I want to call a method called randomnumber. Then I want the results to be displayed in lblPickFive_1. I have 2x2,Pick5,and powerball. Each random number is to be displayed in its own label I created.

For now I am just looking to get past the one of generating a random number and having it displayed in the one label then I will move onto the rest. And I am sure post more questions if I cant figure out the rest.

Error 1 No overload for method 'RandomNumber' takes '0' arguments

using System;
using System.Windows.Forms;

namespace LotteryTickets
{
public partial class Form1 : Form
{
    /// <summary>
    /// no-args Constructor
    /// </summary>
    public Form1()
    {
        InitializeComponent();

    }


    #region "== Control Event Handlers =="
    private void Form1_Load(object sender, EventArgs e)
    {
        ClearWinningNumbers();
    }


    #endregion "== End Control Event Handlers =="


    #region "== Methods ==";
    /// <summary>
    /// Clears the text inside the winning number "balls"
    /// </summary>
    private void ClearWinningNumbers()
    {
        this.lblPickFive_1.Text = "";
        this.lblPickFive_2.Text = "";
        this.lblPickFive_3.Text = "";
        this.lblPickFive_4.Text = "";
        this.lblPickFive_5.Text = "";

        this.lblTwoByTwo_1.Text = "";
        this.lblTwoByTwo_2.Text = "";

        this.lblPowerball_1.Text = "";
        this.lblPowerball_2.Text = "";
        this.lblPowerball_3.Text = "";
        this.lblPowerball_4.Text = "";
        this.lblPowerball_5.Text = "";

        this.lblPowerball_PB.Text = "";
    }
    #endregion "== End Methods ==";

    private void cblTwoByTwo_2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void cblTwoByTwo_1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void btnPlay_Click(object sender, EventArgs e)
    {
        RandomNumber();

    }

    private void lblPickFive_1_Click(object sender, EventArgs e)
    {

    }
    private void RandomNumber(int min, int max)
    {
        int num = new Random().Next(min, max);
        lblPickFive_1.Text = num.ToString();
    }
    }
}
user770022
  • 2,899
  • 19
  • 52
  • 79
  • 3
    Your code does not match your error; you have not listed any methods named `SetRandomNumber`. – Randolpho Oct 15 '10 at 18:34
  • Where is `SetRandomNumber` being called? – Gabe Oct 15 '10 at 18:35
  • 1
    And you should only create one instance of Random and call .Next on it for each random number. Else getting two random numers quickly after each other is problematic since Random is seeded with the time. – CodesInChaos Oct 15 '10 at 18:37
  • FWIW, you're effectively shuffling cards and taking the first N "cards" for your numbers (not familiar with all the lotteries listed but Powerball would have two decks -- white balls and the Powerball). If you go this route, you'll spend no time checking for duplicates (the random number approach can lead to dupes). – Austin Salonen Oct 15 '10 at 18:45

6 Answers6

6

First, you shouldn't be newing up a random number generator every time you want a new random number. You should set the generator as a static or member variable and refer to it for each new number.

Second, you have to pass a min and a max to your RandomNumber method.

Randolpho
  • 55,384
  • 17
  • 145
  • 179
3

Well, your code doesn't match the error, but look at this:

private void btnPlay_Click(object sender, EventArgs e)
{
    RandomNumber();
}

private void RandomNumber(int min, int max)
{
    int num = new Random().Next(min, max);
    lblPickFive_1.Text = num.ToString();
}

RandomNumber has two parameters, min and max. You haven't specified any in the call inside btnPlay_Click. That's what the compiler is complaining about. Change the call to something like:

RandomNumber(5, 10);

Even when that's fixed, you shouldn't create a new instance of Random each time. As it happens, it's unlikely to cause problems in this particular case as it's triggered by a user action, but you should read my article on random numbers to see what the problem is and how to avoid it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

You need to pass in values:

private void btnPlay_Click(object sender, EventArgs e)
{
    RandomNumber();

}

should be:

private void btnPlay_Click(object sender, EventArgs e)
{
    RandomNumber(0, 50000);

}
Lucas B
  • 11,793
  • 5
  • 37
  • 50
0

Your RandomNumber method takes two arguments.

If you want to call the method, you need to pass two arguments.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

You're calling RandomNumber(); in btnPlay_Click but the RandomNumber method requires min and max.

Chad Levy
  • 10,032
  • 7
  • 41
  • 69
0

Setup one Random object and initialize it once.

class Form1
{
    ...
    Random rnd = new Random();
}

then to use it every time it is needed

void RandomNumber(int min, int max)
{
    int num = rnd.Next(min, max);
    ...
}

What happens everytime you call new() it re-seeds the random number and you may end up with the same numbers over and over. I have had this happend to me and it killed me

John Alexiou
  • 28,472
  • 11
  • 77
  • 133