0

I'm trying to assign an array to an object. Bear with me, but I'm brand new to C# and am slowly getting the hang of it I think. In the program below I've created a class that has two fields: a string (Name) and an array (dimensions). I also have one method called Introduce().

public class Person
    {
        public string Name;
        public int[] dimensions;


        public void Introduce()
        {
            var x = 5;
            var y = 7;

            Console.WriteLine("Hello my name is " + Name);
            Console.WriteLine($"Here we have our calculator class displayed. X + Y equals {Calculator.Add(x, y)}");


            //Seeing if it our other file connects to here (Math.cs) it does because we can now use the function Add in here 
            //We have to specify that the Class is PUBLIC though and then we have to use the using Class.Math or whatever it is 

            Console.WriteLine($"{Calculator.Add(3, 9)}");



        }
    }

I've instantiated the Person by creating an object in the Program class. I'm allowed to assign a name, but for some reason it won't let me assign an array. I must have the syntax messed up somehow.

 public class Program
    {
        static void Main(string[] args)
        {
            var person = new Person();

            person.Name = "Jake";
            person.Introduce();

            person.dimensions = { 10, 9, 10 };
        }
    }

Again the problem lies with person.dimensions, I'm allowed to create an array in a class but can't use it to assign values in an object?

  • If you are new to C#, i would suggest getting to know the documentation, https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays. every question you think to ask is likely to be there somewhere, likely more descript and concise than any answer you will get here, and will have more subsequent information than a Q and A. If you cant understand the documentation or have a specific use case you are unsure of, then ask a question, you will get better answers and learn more this way (and faster), additionally less likely to get a question ban for downvotes – TheGeneral Feb 11 '21 at 02:55

1 Answers1

0

You need to instantiate the array.

person.dimensions = new [] { 10, 9, 10 };

or

person.dimensions = new int[] { 10, 9, 10 };
Tim
  • 115
  • 7
  • This worked and I greatly appreciate it, my problem conceptually with this is haven't I already instantiated the object person which contains the parameters for Person() ? My person class contains a string name and an array. When instantiate the top line by writing var person = new Person(); am I not already instantiating that array as is? –  Feb 11 '21 at 03:10
  • If you don't assign a value to class fields (or property backing fields) they are initialised to `default` (eg all zero bytes). Array's, like strings, are separate objects in memory. Your `new Person()` would leave both properties `null`. Either add mandatory constructor arguments, or bind the properties immediately after the constructor `new Person() {Name = ..., dimensions=...}`. – Jeremy Lakeman Feb 11 '21 at 03:18