Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trying To Understand Indexers
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01433919
Message ID:
01434427
Vues:
42
Just think of it a short-cut way of accessing a member of a collection. If you saw the example I posted earlier and added a method:
public  Invoice GetInvoice( string s)
        {
            return Invoices.Find(x => x.Id == s);
        }
then you could get an instance from the collection using either of these methods:
Invoice i = InvoiceList.GetInvoice("A1234");
Invoice x = InvoiceList["A1234"];
the idea being that the second version is more concise....



>Truth is, I don't really know what I'm asking. I'm trying to understand the point behind an indexer. On the surface it appears to be nothing more than a collection attached to a class. But I don't see why you would use one when you can create a collection as a property.
>
>
>
>
>
>>Hi,
>>Sorry, that makes no sense to me. Can you explain more how you envisage that working?
>>
>>>I think I may havee a good use for an indexer:
>>>
>>>Assume I create a Customer class. It has a RecordKey property and a LoadData method. If Recordkey is set to a key value and LoadData is called, then the class is loaded for that customer.
>>>
>>>If Recordkey is set to a 0 and LoadData is called, then the class's indexer is used to load all customers.
>>>
>>>Anyone?
>>>
>>>
>>>
>>>>Does this help? http://stackoverflow.com/questions/609594/what-is-the-use-of-indexers
>>>>
>>>>>I have this customer class:
>public class Customer
>>>>>{
>>>>>    private List<Invoice> _Invoices = new List<Invoice>();
>>>>>    public Invoice this[int item]
>>>>>    {
>>>>>        get
>>>>>        {
>>>>>            return _Invoices[item];
>>>>>        }
>>>>>        set
>>>>>        {
>>>>>            _Invoices[item] = value;
>>>>>        }
>>>>>    }
>>>>>}
>I then do this:
>static void Main(string[] args)
>>>>>{
>>>>>    Invoice InvA = new Invoice();
>>>>>    InvA.InvoiceId = "A0001";
>>>>>
>>>>>    Invoice InvB = new Invoice();
>>>>>    InvB.InvoiceId = "B0002";
>>>>>
>>>>>    Customer cust = new Customer();
>>>>>    cust[0] = InvA;
>>>>>    cust[1] = InvB;
>>>>>}>
>I'm not really seeing a usage for indexers. It seems to me that an Invoices collection would be better. What's the point of indexers?
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform