Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 9th, 2011, 03:25 AM
Authorized User
 
Join Date: Sep 2011
Posts: 24
Thanks: 7
Thanked 0 Times in 0 Posts
Default Collections

In the topic "Defining Collections"
we cant do-------
Code:
animalCollection[0].Feed();
can someone explain it little more and also the indexers and use of 'this' in this part.
Ch11 Page 285 and286
 
Old November 9th, 2011, 11:33 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

animalCollection[0] returns the first element in the collection by using an indexer. animalCollection[0].Feed() invokes the Feed method on the first element of the collection.
The book on page 285 states this:
Quote:
You can't, however, do the following:
animalCollection[0].Feed();
To access items via their indices in this way, you need to use an indexer.
animalCollection is of type Animals and so far an indexer is not defined. On page 286 an indexer is defined.
Code:
public Animal this[int animalIndex]
{
   get
   {
      return (Animal)List[animalIndex];
   }
   set
   {
      List[animalIndex] = value;
   }
}
To explain "this" in this context, the book states this - I'm just changing the order of the sentence:
The indexer looks much like any other property. The difference to properties is that the this keyword is used along with parameters in square brackets.

Hope this helps.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel





Similar Threads
Thread Thread Starter Forum Replies Last Post
Collections arun_babu_a BOOK: Beginning Visual C# 2010 2 October 29th, 2011 04:02 PM
collections help bennyt BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 July 26th, 2008 01:30 AM
Collections in C#? shenku C# 1 August 17th, 2005 06:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.