1

Can someone please tell me what the fudge I am doing wrong? In c I can define a struct to do this in c# I have been banging my head on a wall for an entire day and cannot figure out this code does not work???? I am trying to define a class / struct (SData) that have two string elements color and value. Then I define a class / struct (DataPacket) that has an array of 13 of the SData objects. I instantiate a DataPacket object dp and try to assign a string to dp.sd[0].color and it throws a null ref exception. The value of dp.sd[] in VS locval variables is SData[13] and every element is a ObjArrayProb.SData = null????

The local variable viewer does not show sd[].color and sd[].value it just shows a single pointer for every ObjArrayProb.SData[] element pointing to null??

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObjArrayProb
{
    class SData
    {
        public string color;
        public string value;
    }

    class DataPacket
    {
        public string soh;
        public string sid;
        public SData[] sd;
        public string sm;
        public string eot;

        public DataPacket()
        {
            sd = new SData[13]; <---- this should instantiate 13 SData objects
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            DataPacket dp = new DataPacket();

            dp.sd[0].color = "1"; <--  this line throws a nullreferenceException and dp.sd[0] -> null and not a SData object???

            Console.WriteLine("sd[0].color = " + dp.sd[0].color);
        }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
ctbram
  • 65
  • 6
  • 3
    In addition to instantiating the `sd` array, you also need to instantiate *every object in that array*. – BradleyDotNET Jan 27 '15 at 20:12
  • 1
    You *just* defined the array of 13 elements, all the elements are still not initialized. You need `dp.sd[0] = new SData();` and later you can access the `color` property. Also, they are classes, not `struct`. – Habib Jan 27 '15 at 20:12
  • As a side not see [General Naming Conventions](https://msdn.microsoft.com/en-us/library/vstudio/ms229045%28v=vs.100%29.aspx). – Habib Jan 27 '15 at 20:14
  • If it has been asked before and answered please link the answer as I could not find an answer. I don't understand why I need to do dp.sd[0] = new SData()? What did sd = new SData[13] do in the DataPacket constructor do? I thought it creates 13 SData objects? – ctbram Jan 27 '15 at 20:46
  • dp.sd[0] = new SData(); did the trick. I am still a bit confused though as I don't quite understand what the line sd = new Sdata[13] did in the constructor. I thought that would instantiate 13 Sdata objects??? – ctbram Jan 27 '15 at 20:51
  • 1
    @ctBram - If your class (SData) had been a struct then you would have 13 instances of the struct without having to use **new** to create the individual objects. But since it is a class, your line of code only created an array with 13 references to the class (SData), but they were all null references. So you need to instantiate each one using **new**. – Chris Dunaway Jan 27 '15 at 22:03

0 Answers0