GetItem
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Retrieves the object at the specified index from the collection.
retval = object.GetItem(index)
Arguments
index |
integer The index at which the object should be retrieved. |
Return Value
Type: object
Returns a reference to the object if successful; otherwise, returns null.
Managed Equivalent
Managed collections generally support an indexer.
Remarks
The type of item you retrieve depends on the particular collection where GetItem is called. The JavaScript API for Silverlight does not provide true type-safe collections. However, the returned object can generally be expected to be of a particular type, because the Add and Insert methods for each Silverlight collection are implemented to prevent additions of unexpected objects. Some collections expect only one object type, but others such as VisualCollection can contain multiple object types that share a common base type. The expected type for items within each collection object is documented in the reference topic for that collection.
The JavaScript API for Silverlight does not support an indexer syntax for collections. For example, the syntax myObject = rootCanvas.Children[0] does not work. You must call myObject = rootCanvas.Children.GetItem(0) instead.
All existing Silverlight collections use a zero-based index.
Example
You can retrieve a specific child of a parent object by referencing the index of the collection of the parent object. The following JavaScript example shows how to retrieve a child of a parent Canvas object by using the GetItem method.
function getObject(parent, index)
{
// Determine if the index is valid.
if (index < parent.children.count)
{
// Retrieve the child object at the specified index in the collection.
var object = parent.children.getItem(index);
}
return object;
}
You can enumerate the child of a parent object by accessing the children collection of the parent object. The following JavaScript example shows how to enumerate the children of a parent Canvas object by using the GetItem method.
function getChildren(parent, index)
{
// Enumerate the children of the Canvas object.
for (i = 0; i < parent.children.count; i++)
{
var child = parent.children.getItem(i);
// Display the index and type of the child object.
alert(i + ": " + child.toString());
}
}