-4

I have just started learning OOP. I am making my own class which builds on an array. It has an attribute called length and an array that is of that length.

However, the actual value of length is only declared in the constructor, so my array is stuck as a private variable within the constructor.

How do I implement one such that the array has a certain user-chosen length and is able to be accessed by the class' methods?

public class myClass
{
    private int length; //This is an attribute of my class
    public myClass(int myLength)
    {
            length = myLength;
            int[] myArray = new int[length];
    }
}

I want myArray to be accessible but this is not possible because it is a local variable in the constructor. I think if it was in Python I could just make it a global variable. (Although I think that I would still like to keep this array private as it is also an attribute).

Thanks!

Note: This is not homework but rather something I've been challenging myself to do.

2 Answers2

0

Here's how your class could look like, the OOP way:

public class MyClass
{
    public readonly int Length;
    public int[] Data { get; private set; }

    public MyClass(int dataLength)
    {
        Length = dataLength;
        Data = new int[dataLength];
    }
}

Here:

  • The Data array is constructed with the user-specified length.
  • You can access both Length and Data from inside and outside the class
  • Once instantiated, the Length property cannot be modified

Another way would be to make Length return directly the Length property of the array, as long as it was instantiated:

public class MyClass
{
    public int Length { get { return Data == null ? 0 : Data.Length; } }
    public int[] Data { get; private set; }

    public MyClass(int dataLength)
    {
        Data = new int[dataLength];
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
-1

Revised answer as you have added more code to the question:

You have unwittingly solved your own problem. Take a look at your private int length; declaration. After the object the initialized with the constructor public myClass(int myLength), the length variable is still accessible within the object.

The sample code below has a new public int GetLengthPlusOne() method to access the length variable. Similarly, you can now use the myArray variable in another method.

MyOtherClass

public class MyOtherClass
{
    public void SampleMethod()
    {
        MyClass cls = new MyClass(5); 
        Console.WriteLine(cls.GetLengthPlusOne()); //Output: 6
        var arr = cls.myArray;
    }
}

MyClass

public class MyClass
{
    private int length; //This is an attribute of my class

    /* 
    * Declaring this within the class instead of the constructor allows it 
    * to be persisted in an instance of the class. This is a property.
    */
    public int[] myArray { get; set; }


    public MyClass(int myLength)
    {
            length = myLength;
            myArray = new int[length];
    }

    public int GetLengthPlusOne()
    {
        return length + 1;
    }
}

Side note on naming conventions:

For C#, Class names start capitalized (MyClass), whilst instances of a class start with a lower-case (myClass). Have a look at the documentation for more info.

Edwin Chua
  • 658
  • 1
  • 8
  • 20
  • 2
    public fields are generally considered to be a bad practice. If the array needs to be accessed outside of the class a property should be used instead. – juharr Apr 26 '18 at 02:56
  • @EdwinChua Thanks for your reply but it doesn't seem to work. I have found a solution which is to replace private int length with static int length. I don't what the difference it makes but I'm about to research it so don't downvote me ;) – The_VoltKnight Apr 26 '18 at 06:21
  • @Ezra a static variable is shared across all instances of the same class, whereas each instance of the class has its own copy of a non-static variable. See this question for more information: https://stackoverflow.com/questions/3903537/what-is-the-difference-between-a-static-method-and-a-non-static-method – Edwin Chua Apr 26 '18 at 06:43
  • This answer is incorrect at many levels. You shouldn't call a variable a property, you shouldn't create methods that represent property getters and this still doesn't answer the question's how to declare the array as an instance level field. By the way, `int[] myArray; = new int[length];` doesn't even compile – Camilo Terevinto Apr 26 '18 at 10:15
  • @CamiloTerevinto I've revised my answer, replacing "property" with "variable". I had to verify what you said about the `int[] myArray; = new int[length];`. Please see [this answer](https://stackoverflow.com/a/5678244/7029064) and let me know if I misunderstood something about array constructors... – Edwin Chua Apr 27 '18 at 00:41