0

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.

2
  • Read about Properties. Commented Oct 17, 2019 at 7:34
  • besides your array, you also implement two members amountOfNumbers and AmountOfNumbers, that do not relate to each other. If you use the {get;set;} syntax, the field backing a property is created automaticly, you don't need to create it manually. Commented Oct 17, 2019 at 7:40

2 Answers 2

2

Make your array a member of the actual class, ie property

public class randomArray
{

    public int[] RandomizedArray { get; set; }

    ...

At about this time, you should probably have a read through this

Update

public randomArray(int amountOfNumbers, int rangeOfNumbers)
{
    ...
    RandomizedArray = new int[amountOfNumbers]
    // do stuff here
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I see exactly what you mean. How do I link this property to the array I create in the constructor?
0

Although the response of @TheGeneral contains the solution and points you towards more documentation for learning about OOP. I think it is better to give an explanation why your code did not work.


Everything in OOP has a Scope (a certain "area" where it is available) which is, for most of the things in OOP, fenced of by the brackets. In this instance the scope is based around the constructor, which causes the variables declared in the brackets to only be available inside the brackets. Except when you use an "outside" link like a class variable or property.

public class Example
{
    // this is a class variable, this variable is now reachable from outside the class 
    // definition.
    public int aClassVariable;

    // this is a class property which because we added the get and set calls generate 
    // automatically an get and set method (internally)
    public bool aClassProperty { get; set; }

    public Example()
    {
        // to set the class variable and property you just give them a value.
        aClassVariable = 42;
        aClassProperty = true;

        // this variable is not available outside the scope of this function, 
        // this is because you declared the variable inside this function.
        // So the variable is only available inside this function as long as this 
        // function runs (or as it is called "is in scope").
        int[] arr = new int[10]; 
    }
}

Also pay attention about the differences between variables and properties, a variable is something every OOP language contains. But the properties are actually an extension for the variables where the accessing and setting can be modified with a definition of the get and set method.

I would strongly suggest to read the documentation linked from the answer of TheGeneral because it contains far more information about the intricacies of OOP and C# itself.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.