Kian Kian - 21 days ago 9
C# Question

How can I Use this Implicitly-typed Arrays class

I have a class like this and I cannot change and only have to use it. this class defines an Implicitly-typed array such a list that in each row we have an index and a data(as producer says):

public partial class LightPenMeta {

private long lightPenIDField;

private byte[] lightPenDataField;

/// <remarks/>
public long LightPenID {
get {
return this.lightPenIDField;
}
set {
this.lightPenIDField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")]
public byte[] LightPenData {
get {
return this.lightPenDataField;
}
set {
this.lightPenDataField = value;
}
}
}


my problem is I have to use it and define an array type of LightPenMeta[] which contains for example LightPenID=0 ,LightPenData= '' in row 0.

this code returns null exception:

var lp = new LightPenMeta[1];

lp[0].LightPenData = Data;
lp[0].LightPenID = 0;


the question is how can I use this class and fill it's row?

Answer

You have to initialize the first element within your array

LightPenMeta[] lp = new LightPenMeta[1]; //instantiate your array 
lp[0] = new LightPenMeta(); //initialize the first element here
lp[0].LightPenData = Data;
lp[0].LightPenID = 0;

In one line

LightPenMeta[] lp = new LightPenMeta[1];
lp[0] = new LightPenMeta() { LightPenData = Data, LightPenID = 0 };
Comments