Question

I have my loop for childobjects as follows,

List<float?> LValues = new List<float?>();
List<float?> IValues = new List<float?>();
List<float?> BValues = new List<float?>();
List<HMData>[] data = new List<HMData>[4];
float? Value_LfromList = 0;
float? Value_IfromList = 0;
float? Value_BfromList = 0;

foreach (var item in Read_xml_for_childobjects_id.Root.Descendants("object"))
{
    data[item] = new List<HMData>();  // Error occuring on this line
    for (int k = 0; k < 7; k++)
    {
        Value_LfromList = LValues.ElementAt(k);
        Value_IfromList = IValues.ElementAt(k);
        Value_BfromList = BValues.ElementAt(k);
        data[k].Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });
    } 
}

on the following line,

data[item] = new List<HeatMapData>();

I get an error as follows,

Cannot implicitly convert type 'System.XML.Linq.XElement' to 'int'.An Explicit conversion exist(are you missing a cast?),

-------Updated Question------

int indexer=0;
foreach (var item in Read_xml_for_childobjects_id.Root.Descendants("object"))
{
 data[indexer] = new List<HMData>();
 for (int k = 0; k < 7; k++)
 {
   Value_LromList = LValues.ElementAt(k);
   Value_IfromList = IValues.ElementAt(k);
   Value_BfromList = BValues.ElementAt(k);
   data[k].Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });
  }    
  indexer++;
}

but I get error as object reference not set to instance of an object as soon as I enter into second for loop of k value, I have total 4 childobjects, where each childobjects populate each of the list by 7 elements..please check this link for detailed question..Question with explanation Having real tough time,will appreciate your help,Thank You,

Était-ce utile?

La solution

Although the exception is self explanatory here that you are trying to pass an XElement to indexer where int is required. In order to pin-point it

data[item]

requires an int value in place of item. Which in your case is XElement. Hence the error.

Example (just example, don't use it): It should be something like

int indexer= 0;
data[indexer++] = new List<HeatMapData>();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top