ICollection (named index) 
Author Message
 ICollection (named index)

Hi,

I have the following collection :

I can access items like this :
ToolCollection oTools = new ToolCollection;
oTools.Add(new Tool("PAINT"));
Tool mytool = oTools[0];

But what i want to do is to access the tool like this :

Tool mytool = oTools["PAINT"]
OR
Tool mytool = oTools[0]

Exactly the same way ADO.NET work with the DataTable object.

Any idea ?

public class ToolCollection : System.Collections.CollectionBase

{

public ToolCollection()

{

Quote:
}

public void Add(Tool oTool)

{

List.Add(oTool);

Quote:
}

public void Remove(int nIndex)

{

if (nIndex > Count - 1 || nIndex < 0)

{

//throw error (todo)

Quote:
}

else

{

List.RemoveAt(nIndex);

Quote:
}
}

public Tool this[int nIndex]

{

get

{

return (Tool) List[nIndex];

Quote:
}
}
}



Wed, 24 Nov 2004 20:55:16 GMT  
 ICollection (named index)

Quote:
> Hi,

> I have the following collection :

> I can access items like this :
> ToolCollection oTools = new ToolCollection;
> oTools.Add(new Tool("PAINT"));
> Tool mytool = oTools[0];

> But what i want to do is to access the tool like this :

> Tool mytool = oTools["PAINT"]
> OR
> Tool mytool = oTools[0]

> Exactly the same way ADO.NET work with the DataTable object.

> Any idea ?

public Tool this[string key]
{
    get { return ... }

Quote:
}

---
Mickey Williams
Author "Microsoft Visual C# .NET", MS Press
www.CodevTech.com


Wed, 24 Nov 2004 22:56:14 GMT  
 ICollection (named index)
Pierre, one way you could do this would be to override your get method to
take either a string or an int.  For example your gets would like like(keep
in mind that I'm not implementing a very efficient search algorithm) :

public Tool this[int nIndex]
{
get
{
return (Tool) List[nIndex];

Quote:
}
}

public Tool this[string key]
{
get
{
for (int i = 0; i < List.Count; i++)
{
if (((Tool)List[i]).name == key) return (Tool) List[i];

Quote:
}
return null;
}
}



Quote:
> Hi,

> I have the following collection :

> I can access items like this :
> ToolCollection oTools = new ToolCollection;
> oTools.Add(new Tool("PAINT"));
> Tool mytool = oTools[0];

> But what i want to do is to access the tool like this :

> Tool mytool = oTools["PAINT"]
> OR
> Tool mytool = oTools[0]

> Exactly the same way ADO.NET work with the DataTable object.

> Any idea ?

> public class ToolCollection : System.Collections.CollectionBase

> {

> public ToolCollection()

> {

> }

> public void Add(Tool oTool)

> {

> List.Add(oTool);

> }

> public void Remove(int nIndex)

> {

> if (nIndex > Count - 1 || nIndex < 0)

> {

> //throw error (todo)

> }

> else

> {

> List.RemoveAt(nIndex);

> }

> }

> public Tool this[int nIndex]

> {

> get

> {

> return (Tool) List[nIndex];

> }

> }

> }



Thu, 25 Nov 2004 13:13:09 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Getting an item by it's name (instead of it's index#)

2. Best practice question: By name or by index?

3. I assume there are not named indexed properties in C#

4. Need help with a choice: ICollection, Managed or unmanaged

5. ICollection.CopyTo( Array, int )

6. double inheritance with interface IDictionary : ICollection, IEnumerable

7. CollectionBase vs ICollection

8. Strongly typed implementation of ICollection CopyTo().

9. ICollection

10. Remoting passing an ICollection

11. Style Question: [] vs. ICollection

12. array indexing by pointer and long index

 

 
Powered by phpBB® Forum Software