Click here to Skip to main content
15,899,475 members
Home / Discussions / C#
   

C#

 
GeneralRe: Collections, plus collection of base type - concept Pin
PIEBALDconsult15-Dec-11 11:51
mvePIEBALDconsult15-Dec-11 11:51 
GeneralRe: Collections, plus collection of base type - concept Pin
Luc Pattyn15-Dec-11 19:55
sitebuilderLuc Pattyn15-Dec-11 19:55 
GeneralRe: Collections, plus collection of base type - concept Pin
DaveyM6915-Dec-11 23:54
professionalDaveyM6915-Dec-11 23:54 
GeneralRe: Collections, plus collection of base type - concept Pin
BillWoodruff16-Dec-11 4:03
professionalBillWoodruff16-Dec-11 4:03 
GeneralRe: Collections, plus collection of base type - concept Pin
DaveyM6916-Dec-11 14:20
professionalDaveyM6916-Dec-11 14:20 
GeneralRe: Collections, plus collection of base type - concept Pin
BillWoodruff16-Dec-11 18:19
professionalBillWoodruff16-Dec-11 18:19 
AnswerRe: executive summary Pin
Luc Pattyn17-Dec-11 2:23
sitebuilderLuc Pattyn17-Dec-11 2:23 
GeneralRe: Collections, plus collection of base type - concept Pin
DaveyM6917-Dec-11 2:15
professionalDaveyM6917-Dec-11 2:15 
How about getting the enumerator of each collection to enumerate both of them?
I have knocked up an example of the general outline of my classes, I have used this in the MyFooMyBarPool.MyBases property.
C#
using System;
using System.Collections;
using System.Collections.Generic;

public enum BaseType
{
    MyFoo,
    MyBar
}

public abstract class MyBase
{
    private BaseType baseType;
    private string name;

    internal MyBase(BaseType baseType, string name)
    {
        this.baseType = baseType;
        this.name = name;
    }

    public string Name
    {
        get { return name; }
    }

    public override string ToString()
    {
        return string.Format("Type: {0}, Name: {1}", baseType, name);
    }
}

public sealed class MyFoo : MyBase
{
    internal MyFoo(string name)
        : base(BaseType.MyFoo, name)
    { }
}

public sealed class MyBar : MyBase
{
    internal MyBar(string name)
        : base(BaseType.MyBar, name)
    { }
}

public class SimpleCollection<T> : IEnumerable<T>
{
    private List<T> innerList;

    public SimpleCollection(IEnumerable<T> items)
    {
        innerList = new List<T>(items);
    }

    public T this[int index]
    {
        get { return innerList[index]; }
    }
    public int Count
    {
        get { return innerList.Count; }
    }

    public IEnumerator<T> GetEnumerator()
    {
        return innerList.GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return innerList.GetEnumerator();
    }
}

public class MyFooCollection : SimpleCollection<MyFoo>
{
    internal MyFooCollection(IEnumerable<MyFoo> myFoos)
        : base(myFoos)
    { }
}

public class MyBarCollection : SimpleCollection<MyBar>
{
    internal MyBarCollection(IEnumerable<MyBar> myBars)
        : base(myBars)
    { }
}

// In my real code this is a singleton so there will only ever be one pool
public class MyFooMyBarPool
{
    private MyFooCollection myFooCollection;
    private MyBarCollection myBarCollection;

    public MyFooMyBarPool()
    {
        // Items and collections are instanciated here only.
        myFooCollection = new MyFooCollection(new MyFoo[] { new MyFoo("A"), new MyFoo("B"), new MyFoo("C") });
        myBarCollection = new MyBarCollection(new MyBar[] { new MyBar("A"), new MyBar("B"), new MyBar("C") });
    }

    public IEnumerable<MyBase> MyBases
    {
        get
        {
            IEnumerator<MyFoo> myFooEnumerator = myFooCollection.GetEnumerator();
            while (myFooEnumerator.MoveNext())
                yield return myFooEnumerator.Current;
            IEnumerator<MyBar> myBarEnumerator = myBarCollection.GetEnumerator();
            while (myBarEnumerator.MoveNext())
                yield return myBarEnumerator.Current;
            yield break;
        }
    }
    public MyFooCollection MyFoos
    {
        get { return myFooCollection; }
    }
    public MyFooCollection MyBars
    {
        get { return myFooCollection; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyFooMyBarPool pool = new MyFooMyBarPool();
        foreach (MyBase myBase in pool.MyBases)
            Console.WriteLine(myBase);
        Console.WriteLine(
            "{0} MyFoo and {1} MyBar items", 
            pool.MyFoos.Count, pool.MyBars.Count);
        Console.ReadKey();
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



AnswerRe: Collections, plus collection of base type - concept Pin
Luc Pattyn17-Dec-11 2:32
sitebuilderLuc Pattyn17-Dec-11 2:32 
GeneralRe: Collections, plus collection of base type - concept Pin
Luc Pattyn17-Dec-11 2:42
sitebuilderLuc Pattyn17-Dec-11 2:42 
AnswerRe: Collections, plus collection of base type - concept Pin
Luc Pattyn17-Dec-11 2:48
sitebuilderLuc Pattyn17-Dec-11 2:48 
GeneralRe: Collections, plus collection of base type - concept Pin
DaveyM6917-Dec-11 3:10
professionalDaveyM6917-Dec-11 3:10 
AnswerRe: Collections, plus collection of base type - concept Pin
Luc Pattyn17-Dec-11 3:50
sitebuilderLuc Pattyn17-Dec-11 3:50 
AnswerRe: Collections, plus collection of base type - concept Pin
BillWoodruff16-Dec-11 20:48
professionalBillWoodruff16-Dec-11 20:48 
QuestionBoundColumns Not Working Pin
AmbiguousName15-Dec-11 6:45
AmbiguousName15-Dec-11 6:45 
AnswerRe: BoundColumns Not Working Pin
PIEBALDconsult15-Dec-11 7:02
mvePIEBALDconsult15-Dec-11 7:02 
AnswerRe: BoundColumns Not Working Pin
SilimSayo15-Dec-11 13:34
SilimSayo15-Dec-11 13:34 
QuestionC# help? Pin
Brian Reiber15-Dec-11 5:50
Brian Reiber15-Dec-11 5:50 
AnswerRe: C# help? Pin
Paladin200015-Dec-11 6:14
Paladin200015-Dec-11 6:14 
GeneralRe: C# help? Pin
harold aptroot15-Dec-11 6:28
harold aptroot15-Dec-11 6:28 
AnswerRe: C# help? Pin
Richard MacCutchan15-Dec-11 6:29
mveRichard MacCutchan15-Dec-11 6:29 
AnswerRe: C# help? Pin
PIEBALDconsult15-Dec-11 6:36
mvePIEBALDconsult15-Dec-11 6:36 
AnswerRe: C# help? Pin
Deborah Palmer McCain15-Dec-11 8:34
Deborah Palmer McCain15-Dec-11 8:34 
GeneralRe: C# help? Pin
Richard MacCutchan15-Dec-11 22:23
mveRichard MacCutchan15-Dec-11 22:23 
GeneralRe: C# help? Pin
Deborah Palmer McCain16-Dec-11 6:35
Deborah Palmer McCain16-Dec-11 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.