I'm learning c# and have decided to try and create a functioning chance game, part by part. I previously created a method that would create a random (yet likely inefficient) array of natural numbers that would not appear more than once.
However, as I try to piece together OOP I realised if I create multiple of these arrays they would be objects, thus should be created by a class.
I have the array created inside a constructor. Yet I cannot access this array from either within the constructor's class or in another class entirely.
class randomArray
{
Random rng = new Random();
protected int amountOfNumbers;
protected int rangeOfNumbers;
public randomArray(int amountOfNumbers, int rangeOfNumbers)
{
this.amountOfNumbers = amountOfNumbers;
this.rangeOfNumbers = rangeOfNumbers;
int[] randomizedArray = new int[amountOfNumbers];
for (int i = 0; i < amountOfNumbers; i++)
{
randomizedArray[i] = rng.Next(1, rangeOfNumbers + 1);
// A test to ensure that each new number generate is not
already part of the array.
for (int j = 0; j < i; j++)
{
while (randomizedArray[i] == randomizedArray[j])
{
randomizedArray[i] = rng.Next(1, rangeOfNumbers + 1);
j = 0;
}
if (randomizedArray[i] != randomizedArray[j])
continue;
}
}
}
public int RangeOfNumbers { get; set; }
public int AmountOfNumbers { get; set; }
I believe I'm failing to either understand the fundamentals of OOP or I am I failing to understand how to utilize classes.