Re: Which generic?
- From: Jeroen Mostert <jmostert@xxxxxxxxx>
- Date: Tue, 01 Jul 2008 23:41:15 +0200
wsmckenz wrote:
On Jul 1, 3:50 pm, Jeroen Mostert <jmost...@xxxxxxxxx> wrote:wsmckenz wrote:I want a collection of things, Columns in this case, which are indexedEither SortedList<> or SortedDictionary<> should do.
by name (which would imply a Dictionary<string, Column>) but are also
in a guaranteed order (which would imply List<Column>). I want to be
able to write:
m_columns[i];
// or
m_columns["FRED"];
and have both work.
i also want
foreach(Column col in m_columns)
{
}
To enumerate in the order that the columns were appended to the list.
Is there a generic that serves the purpose or do I have to write my
own?
I dont think that works. For instance, the following code
using System;
using System.Collections.Generic;
public struct Column
{
public string name;
public string description;
public int type;
public Column(string nameIn, string descIn, int typeIn)
{
name = nameIn;
description = descIn;
type = typeIn;
}
}
public class foo
{
static Column[] myCols =
{
new Column("ABC", "abc", 2),
new Column("DEF", "def", 3),
new Column("XYZ", "xyz", 1),
new Column("QRS", "qrs", 2)
};
static void Main()
{
SortedList<string, Column> cols = new SortedList<string,
Column>();
foreach(Column col in myCols)
{
cols.Add(col.name, col);
}
foreach(string name in cols.Keys)
{
System.Console.Out.WriteLine(cols[name].name);
}
System.Console.Out.WriteLine("====================");
for (int i=0; i<cols.Count; ++i)
{
System.Console.Out.WriteLine(cols.Values[i].name);
}
}
}
Produces the following output:
C:\work>generic
ABC
DEF
QRS
XYZ
====================
ABC
DEF
QRS
XYZ
This is not what I want. I want the items to remain in the list in
the order i put them there, but also be accessible by name.
Ah, I see. In that case, you indeed have to roll your own. This shouldn't be hard: you want to aggregate a List<Column> and a Dictionary<string, Column>. The class basically implements IList<Column> and IDictionary<string, Column> and delegates as appropriate:
class ColumnCollection : IList<Column>, IDictionary<string, Column> {
private readonly IList<Column> columnsByIndex = new List<Column>();
private readonly IDictionary<string, Column> columnsByName = new Dictionary<string, Column>();
#region IList<Column> Members
public int IndexOf(Column item) {
return columnsByIndex.IndexOf(item);
}
public void Insert(int index, Column item) {
columnsByIndex.Insert(index, item);
columnsByName.Add(item.name, item);
}
...
and so on. Aside from being a fair bit of typing, there's little difficulty involved. You'll have to decide at a few points whether you want a list-centric view or a dictionary-centric view of your class (such as when you're enumerating).
--
J.
.
- Follow-Ups:
- Re: Which generic?
- From: qglyirnyfgfo
- Re: Which generic?
- From: wsmckenz
- Re: Which generic?
- References:
- Which generic?
- From: wsmckenz
- Re: Which generic?
- From: Jeroen Mostert
- Re: Which generic?
- From: wsmckenz
- Which generic?
- Prev by Date: Re: C# skills questionnaire
- Next by Date: Re: Which generic?
- Previous by thread: Re: Which generic?
- Next by thread: Re: Which generic?
- Index(es):
Relevant Pages
|