-2

I am wanting to click the button and choose a number between 1, 20 then display the results in lblPickFive_1.Text When I try to run it I get errors all over the place. Error 1 A field initializer cannot reference the non-static field, method, or property 'LotteryTickets.Form1.random'

Warning 4 Unreachable code detected

    string random;
        string num = random.Next();

        string num = random.Next(20);



    private int RandomNumber(int min, int max)
    {
    Random random = new Random();
    return random.Next(min, max); 
    lblPickFive_1.Text=num;
    }        
 }
user770022
  • 2,899
  • 19
  • 52
  • 79
  • 1
    The return statement before `lblPickFive_1.Text=num;` is an issue. That's the unreachable code. – Gabe Oct 15 '10 at 16:55
  • 3
    Is this homework? This really smells of homework. – jcolebrand Oct 15 '10 at 16:57
  • Just curious...but what's wrong with getting homework help on SO? I got coding help when I was in college... – Jeff Oct 15 '10 at 17:05
  • Homework, per se, is not a problem - what people don't like is when the homework assignment is posted and no attempt was made to solve it. That said, if it's homework, it's way better to tag it as such ('cause a lot of people can smell it a mile away and have an aversion to doing it for other people). I don't care, if they've made a real effort I'm happy to lend expertise. – KevinDTimm Oct 15 '10 at 17:11

4 Answers4

1

Try this instead:

private void SetRandomNumber(int min, int max)
{
   int num = new Random().Next(min, max); 
   lblPickFive_1.Text=num;
}    
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • 1
    Nope, the randoms will all have the same seed and thus the same number will be generated all the time... – Richard J. Ross III Oct 15 '10 at 16:59
  • 1
    @Richard, that is not true. What do you suppose the seed will be? (it's derived from the current time, which usually changes...) However, you're correct that better practice is to store it somewhere rather than creating it anew to avoid issues when the method gets called rapidly within a short time. – Kirk Woll Oct 15 '10 at 17:00
  • In my experience, that is what is expected, but not what actually happens, try it yourself... – Richard J. Ross III Oct 15 '10 at 17:01
  • 1
    @Richard J. Ross III, you are not correct. The documentation for the empty constructor states: Initializes a new instance of the Random class, using a time-dependent default seed value. – Klaus Byskov Pedersen Oct 15 '10 at 17:01
  • @Kirk Woll, I agree, but the OP does not seem ready to declare class wide variables just quite yet which is why I went with the simplistic approach ;-) – Klaus Byskov Pedersen Oct 15 '10 at 17:03
  • Well then I will try It and see, but I am not expecting any difference from my past experience... – Richard J. Ross III Oct 15 '10 at 17:03
  • @Richard, prepare to be surprised! ;) – Kirk Woll Oct 15 '10 at 17:04
  • I tried on parallels virtual desktop, and the result I expected happened. The numbers are the same... I don't know if that is a problem with some visual studio setting I have, but all I know is, in the example I wrote, all numbers were the same... – Richard J. Ross III Oct 15 '10 at 17:07
  • Ok, heres my problem, there was no time in between method calls :) adding a Thread.Sleep(100) made numbers random again... but if the method must be called repeatedly, then that will result in the same number, as there is no time in between method calls... – Richard J. Ross III Oct 15 '10 at 17:09
  • @Kirk, @klaus : http://stackoverflow.com/questions/3945322/c-same-numbers-in-all-5-boxes-and-its-suppose-to-be-5-different-numbers-in-5-dif – PaulG Oct 15 '10 at 19:19
0

From the looks of it, these are the reasons why:

Error 1 A field initializer cannot reference the non-static field, method, or property 'LotteryTickets.Form1.random'

is because you method is private and you're trying to call it from outside

private int RandomNumber(int min, int max)

make it internal or public instead

Warning 4 Unreachable code detected

is because you return before the textbox gets set;

return random.Next(min, max); 
lblPickFive_1.Text=num; // This will never get reached

and if it were me I would write the Random method like so:

 internal static int RandomNumber(int min, int max)
 {
    Random random = new Random(DateTime.Now.Millisecond); // Use a seed to reduce the chance of re-ocurring numbers
    return random.Next(min, max); 
 }       

and would probably have it in a RandomHelper class instead of on the form itself.

Joel Beckham
  • 18,254
  • 3
  • 35
  • 58
Iain Ward
  • 9,850
  • 5
  • 34
  • 41
0

Your call to random.Next() is outside of the scope where the random object is declared. Then you're getting the unreachable code because you return before you set the text of the label to num.

Try this:

  class MyClass
  {
    Random random;
    public MyClass()
    {
      random = new Random(Guid.NewGuid().GetHashCode());
    }

    public void RandomNumber(int min, int max)
    {
      lblPickFive_1.Text = random.Next(min, max).ToString();      
    }
  }
steinar
  • 9,383
  • 1
  • 23
  • 37
  • Error 1 Cannot access a non-static member of outer type 'LotteryTickets.Form1' via nested type 'LotteryTickets.Form1.MyClass' – user770022 Oct 15 '10 at 17:14
  • Sorry. The surrounding class was just there to show the idea. Pull the content of it into your form class (Form1) instead. – steinar Oct 15 '10 at 17:16
-1

Most of you are wrong in some fashion...

First, you need to only have one instance of Random, because they will all be seeded with the same number...

Second, as some of them said, the code lblPickFive_1.Text=num; will never be called. Change you method to this:

Random rand = new Random();

private int RandomNumber(int min, int max)
{
    int i = rand.Next(min, max);
    lblPickFive_1.Text=i;
    return i;
}
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201