0

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,

Community
  • 1
  • 1
Reshma
  • 864
  • 5
  • 20
  • 38

1 Answers1

3

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>();
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • I am having trouble with passing int to array index,can you please show me with eg how I can do this, – Reshma Jan 06 '14 at 05:47
  • I know the error, I knew I need to pass the int value but don't know how to do that, even when I am trying to parse the item into int as data[int.Parse(item)]..I still get error as The best overloaded method match for int.Parse(item) has some invalid method arguments – Reshma Jan 06 '14 at 05:50
  • you can not parse an XElement to int. Use some int value instead. What you are missing here is that ead_xml_for_childobjects_id.Root.Descendants("object") returns you XElement and not an int value. – Ehsan Jan 06 '14 at 05:52