Hallo,
I have defined one array of class in the main program and I get an error when I try to use it.
The definition in the main program is
public const int NumColumns = 10;
public static GridViewComp[] gridView = new GridViewComp[NumColumns];
GridViewComp Class contains: string Title, int Width, bool Show.
In another form, I try to assign the values from an XML file
private void AssignGridSetup()
{
var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
int iLoop = 0;
foreach (GridViewComp myComp in Program.gridView)
{
var item = columns.Element(Program.columnElement[iLoop]);
myComp.Title = item.Element("Title").Value;
myComp.Width = Convert.ToInt32(item.Element("Width").Value);
myComp.Show = Convert.ToBoolean(item.Element("Show").Value);
iLoop++;
}
}
(the xml part works fine)
When I execute the code, I become the following error:
"Object reference not set to an instance of an object" in the line
myComp.Title = item.Element("Title").Value;
I have looked for a solution, and I should initialize every element of the array, including the initialize instruction
private void AssignGridSetup()
{
var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
int iLoop = 0;
foreach (GridViewComp myComp in Program.gridView)
{
var item = columns.Element(Program.columnElement[iLoop]);
myComp = new GridViewComp();
myComp.Title = item.Element("Title").Value;
myComp.Width = Convert.ToInt32(item.Element("Width").Value);
myComp.Show = Convert.ToBoolean(item.Element("Show").Value);
iLoop++;
}
}
But when I do so, the compiler shows the following error:
myNamespace.GridViewComp.GridViewComp() is inaccessible due to its protection level
and marks the underlined part.
I also tried with a "for" loop instead of "foreach"
private void AssignGridSetup()
{
var columns = XDocument.Load(Program.setupFileName).Element("Setup").Element("Columns");
for (int iLoop = 0; iLoop < Program.NumColumns; iLoop++ )
{
var item = columns.Element(Program.columnElement[iLoop]);
Program.gridView[iLoop] = new GridViewComp();
Program.gridView[iLoop].Title = item.Element("Title").Value;
Program.gridView[iLoop].Width = Convert.ToInt32(item.Element("Width").Value);
Program.gridView[iLoop].Show = Convert.ToBoolean(item.Element("Show").Value);
}
}
but the result is the same.
Has anybody any idea of how can I fill the array with the xml data, or what I could be doing wrong?
Thank you very much in advance.
David
(PS: I use MS VS2010)