0

Hello I am doing some tests in C# with nesting properties which return objects, but I am getting an object reference exception.

I want to be able to access the arrays in nested Properties, but in the current context I can see that I'm not instancing any new objects inside the properties.

This is where the basic question comes up... Where do I declare a 'new' object instance in the middle of all this? Do I even need to declare and new object reference inside the 'foo' class or 'bar' class?

namespace CustomProperties_TEST
{
    class Program
    {
        public foo[] Blatherskite { get; set; }

        static void Main(string[] args)
        {
            Program myProgram = new Program();

            myProgram.Blatherskite[0].CustomProperty1[0].CustomProperty2 = 999999999;
            myProgram.Blatherskite[1].CustomProperty1[0].CustomProperty2 = 999999999;

            foreach (var item in myProgram.Blatherskite)
            {
                Console.WriteLine(item.CustomProperty1[0].CustomProperty2);
            }
        }
    }

    class foo
    {
        private bar[] customevariable1;

        public bar[] CustomProperty1
        {
            get { return customevariable1; }
            set { customevariable1 = value; }
        }
    }

    class bar
    {
        private int customintvariable2;

        public int CustomProperty2
        {
            get { return customintvariable2; }
            set { customintvariable2 = value; }
        }
    }
}

1 Answer 1

1

You would want to do something like the following, since arrays are initialized to null by default.

static void Main(string[] args)
{
    Program myProgram = new Program();

    // This is your missing initialization
    myProgram.Blatherskite = new foo[2] {
          new foo{CustomProperty1 = new bar[2]{new bar{CustomProperty2 = 1},new bar{CustomProperty2 = 2}}}
        , new foo{CustomProperty1 = new bar[2]{new bar{CustomProperty2 = 3},new bar{CustomProperty2 = 4}}}};

    myProgram.Blatherskite[0].CustomProperty1[0].CustomProperty2 = 999999999;
    myProgram.Blatherskite[1].CustomProperty1[0].CustomProperty2 = 999999999;

    foreach (var item in myProgram.Blatherskite)
    {
        Console.WriteLine(item.CustomProperty1[0].CustomProperty2);
    }
}

Using arrays means you'll have to set their size. If you would want more flexibility, use a List, and then you can simply add items to it.

Sign up to request clarification or add additional context in comments.

2 Comments

This is awesome. I knew I was missing one of the blocks. But searching for Nested Properties didn't yield much. A newbie question but I hope this helps others like me.Thank you.
Glad it helped. If you find it helpful, you can vote and mark as answered. Keep on coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.