# How to get the Item of a 0 based intex collection • ### Question

• Dear all,

I have the follwoing code snipet that I try to use in order to retrive each items from a collection :

```for (int i = 0, count = Items.Count; i < count; i++)
{
var angleDelta = 360.0 / count;
var angleShift = ri.HalfShifted ? -angleDelta / 2 : 0;
var startAngle = angleDelta * i + angleShift;
var rotation = startAngle + angleDelta / 2;

ri.AngleDelta = angleDelta;
ri.StartAngle = startAngle;
ri.Rotation = rotation;

//Items[i].AngleDelta = angleDelta;
//Items[i].StartAngle = startAngle;
//Items[i].Rotation = rotation;
}```

What is wierd is that the Items.GetItemAt(i) is returning NULL and my collection is containing 2 items of type RadialMenuItem.

My Items collection is of type ItemCollection from my ItemsControl.Items

How can I retriev the item from a given index position ?

Any idea ?

Thanks for help

regards

Wednesday, November 18, 2015 5:52 PM

• Because if I do:

```            ItemCollection it = new ListBox().Items;
//foreach(var x in it)
//{
//    Debug.WriteLine("I got one");
//}

for (int i = 0; i < it.Count; i++)
{
var x = it.GetItemAt(i) as object;
if(x!= null)
{
Debug.WriteLine("I got one");
}
}```

Then I get two objects.

In other words.

If you cast to the right type then your code should give you 2 things.

`var ri = Items.GetItemAt(i);`

With out the as...

Stick a breakpoint in on the line after.

See what you get out as ri.

Is it a RadialMenuItem which is uninitialized somehow, or is it something else?

.

Your for loop is a strange way to do it.

If it compiles, I can't see how that's your problem though.

Because if I do:

```            for (int i = 0, count = it.Count;  i < count; i++)
//     for (int i = 0; i < it.Count; i++)
{
var x = it.GetItemAt(i) as object;
if(x!= null)
{
Debug.WriteLine("I got one");
}
}```

Then that gets me my two objects OK.

I also wonder what Items is.

If you put a break point in before your for loop, when it breaks add a quickwatch on items and check that's what you think it's supposed to be.

Hope that helps.

Technet articles: WPF: MVVM Step 1; All my Technet Articles

Wednesday, November 18, 2015 6:42 PM
• >>How can I retriev the item from a given index position ?

>>Any idea ?

Use the indexer:

`var ri = Items[i] as RadialMenuItem;`

But your for loop looks a bit strange. You need to make sure that you are increasing the counter variable i as expected. Try this:

```            int count = Items.Count;
for (int i = 0; i < count; i++)
{
var angleDelta = 360.0 / count;
var angleShift = ri.HalfShifted ? -angleDelta / 2 : 0;
var startAngle = angleDelta * i + angleShift;
var rotation = startAngle + angleDelta / 2;

ri.AngleDelta = angleDelta;
ri.StartAngle = startAngle;
ri.Rotation = rotation;
}
```

If ri is still NULL, the Items collection doesn't contain only RadialMenuItem objects.

Hope that helps.

Thursday, November 19, 2015 11:44 AM

### All replies

• Because if I do:

```            ItemCollection it = new ListBox().Items;
//foreach(var x in it)
//{
//    Debug.WriteLine("I got one");
//}

for (int i = 0; i < it.Count; i++)
{
var x = it.GetItemAt(i) as object;
if(x!= null)
{
Debug.WriteLine("I got one");
}
}```

Then I get two objects.

In other words.

If you cast to the right type then your code should give you 2 things.

`var ri = Items.GetItemAt(i);`

With out the as...

Stick a breakpoint in on the line after.

See what you get out as ri.

Is it a RadialMenuItem which is uninitialized somehow, or is it something else?

.

Your for loop is a strange way to do it.

If it compiles, I can't see how that's your problem though.

Because if I do:

```            for (int i = 0, count = it.Count;  i < count; i++)
//     for (int i = 0; i < it.Count; i++)
{
var x = it.GetItemAt(i) as object;
if(x!= null)
{
Debug.WriteLine("I got one");
}
}```

Then that gets me my two objects OK.

I also wonder what Items is.

If you put a break point in before your for loop, when it breaks add a quickwatch on items and check that's what you think it's supposed to be.

Hope that helps.

Technet articles: WPF: MVVM Step 1; All my Technet Articles

Wednesday, November 18, 2015 6:42 PM
• >>How can I retriev the item from a given index position ?

>>Any idea ?

Use the indexer:

`var ri = Items[i] as RadialMenuItem;`

But your for loop looks a bit strange. You need to make sure that you are increasing the counter variable i as expected. Try this:

```            int count = Items.Count;
for (int i = 0; i < count; i++)
{
var angleDelta = 360.0 / count;
var angleShift = ri.HalfShifted ? -angleDelta / 2 : 0;
var startAngle = angleDelta * i + angleShift;
var rotation = startAngle + angleDelta / 2;

ri.AngleDelta = angleDelta;
ri.StartAngle = startAngle;
ri.Rotation = rotation;
}
```

If ri is still NULL, the Items collection doesn't contain only RadialMenuItem objects.

Hope that helps.