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);
}
}
}