creating 2 indexers in dotnet 2?

  • Thread starter Thread starter Elhanan
  • Start date Start date
E

Elhanan

hi..

i would like to create something very much like DataTable, only for it
to hold actuall objects (not their string representation)

i saw that DataTable as DataColumn Collection which has to 2 indexes,
it can bring up a columns by name or by it's ordinal number, is there
any easy way to do this, or do i need to 2 collection objects (an array
list and a Dictionary) that will hold both references
 
Elhanan,

No, you just have to put two indexers on your object that is exposed:

public class MyCollection
{
public this[int index]
{
get
{

}
set
{

}
}

public this[string index]
{
get
{

}
set
{

}
}
}

Hope this helps.
 
i meant to ask how do i manage this internally?

right now i a DIctionary which saves a key as a string, and it's index
in the value part.

so if i want to retrieve the name by the index i do this:

public string this[int index]
{
get
{
foreach (KeyValuePair<string, int> kvp in fields)
{
if (kvp.Value == index)
return kvp.Key;
}
throw new KeyNotFoundException("Key not Found according
To Index " + index);
}
}

Elhanan,

No, you just have to put two indexers on your object that is exposed:

public class MyCollection
{
public this[int index]
{
get
{

}
set
{

}
}

public this[string index]
{
get
{

}
set
{

}
}
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Elhanan said:
hi..

i would like to create something very much like DataTable, only for it
to hold actuall objects (not their string representation)

i saw that DataTable as DataColumn Collection which has to 2 indexes,
it can bring up a columns by name or by it's ordinal number, is there
any easy way to do this, or do i need to 2 collection objects (an array
list and a Dictionary) that will hold both references
 
"Elhanan" <[email protected]> a écrit dans le message de (e-mail address removed)...

|i meant to ask how do i manage this internally?
|
| right now i a DIctionary which saves a key as a string, and it's index
| in the value part.
|
| so if i want to retrieve the name by the index i do this:

Just use two dictionaries, one in either direction and keep them in sync.

Joanna
 
Elhanan said:
hi..

i would like to create something very much like DataTable, only for it
to hold actuall objects (not their string representation)

i saw that DataTable as DataColumn Collection which has to 2 indexes,
it can bring up a columns by name or by it's ordinal number, is there
any easy way to do this, or do i need to 2 collection objects (an array
list and a Dictionary) that will hold both references

KeyedCollection<TKey, TItem> provides indexing by a key and by index.

SP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top