While we're at it, put the Random variable as a private member in the
class so that it won't keep on generating the same number within a
second (iirc the random is seeded with the time value in seconds!)
Oh and for convenience the class that does the random thingie
shouldn't be Program but MyCoolRandomNess or something like that. And
mixing uppercase variables and uppercase class names is a bad idea as
well, so we change "Random" to "random".
And once that happens you can wonder what's the use of the class and
the "RandomNumber" method in the first place, since it does exactly
the same as the build-in Random class...
Yes, everyday considerations reflected to a hello world example.
Cheers,
Stefan.
--- Step 1
namespace ConsoleApplication1
{
class Program
{
private Random random = new Random();
static void Main(string[] args)
{
Program Random = new Program()
int returnValue = Random.RandomNumber(5, 20);
Console.WriteLine(returnValue);
}
private int RandomNumber(int min, int max)
{
return random.Next(min, max);
}
}
}
---
--- Step 2
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyCoolRandomnessClass random = new
MyCoolRandomnessClass();
int returnValue = random.RandomNumber(5, 20);
Console.WriteLine(returnValue);
}
}
public class MyCoolRandomnessClass
{
private Random random = new Random();
public int RandomNumber(int min, int max)
{
return random.Next(min, max);
}
}
}
---
--- Step 3
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
int returnValue = random.Next(5, 20);
Console.WriteLine(returnValue);
}
}
}
---
Change the method to STATIC instead of PRIVATE, then it will work
Better still, join the world of objects and get out of static mode. I know
it's overkill for this project, but it's good practise for the future IMHO.
Something like:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program Random = new Program()
int returnValue = Random.RandomNumber(5, 20);
Console.WriteLine(returnValue);
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
}
}
Peter