Click here to Skip to main content
15,914,074 members
Home / Discussions / C#
   

C#

 
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 
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 
I know I'm late to the party here, but this question, and what's been exposed in the ensuing discussion, has "fit" with some aspects of use of classes I've been exploring.

1. I noted that in one of colleague PieBald's responses, he defined the Lists (alist, blist) that held collections of instances of classes A,B as static, and outside the scope of all three classes, which I found intriguing.

2. It seems to me that you could exploit the fact that each instantiation of Class A or B does invoke the constructor in class C:
C#
private abstract class C
{
    public static readonly List<A> alist = new List<A>();
    public static readonly List<B> blist = new List<B>();
    public static readonly List<C> clist = new List<C>();

    public string Name { get; private set; }

    public C(string Name)
    {
        this.Name = Name;

        clist.Add(this);

        if(this is A)
        {
            alist.Add(this as A);
        }
        else
        {
            blist.Add(this as B);
        }
    }

    public override string ToString() { return (this.Name); }
}
Using this implementation of Class C, you guarantee that all three lists are updated with each new instantiation of A or B.

Disclaimers-Dunno's:

1. don't know if this would play nice with .NET 2.0

2. thread safety ?

I've tested this code with the following:
A myA1 = new A("a1");
A myA2 = new A("a2");
B myB1 = new B("b1");
B myB2 = new B("b2");

foreach (var a in C.alist)
{
    Console.WriteLine("This A's Name is : " + a.Name);
}

foreach (var b in C.blist)
{
    Console.WriteLine("This B's Name is : " + b.Name);
}

foreach (var c in C.clist)
{
    Console.WriteLine("This C is a Type: " + c.GetType() + " : Name = " + c.Name);
}
The results were as expected:
CSS
This A's Name is : a1
This A's Name is : a2
This B's Name is : b1
This B's Name is : b2
This C is a Type: TestImmutable.Form1+A : Name = a1
This C is a Type: TestImmutable.Form1+A : Name = a2
This C is a Type: TestImmutable.Form1+B : Name = b1
This C is a Type: TestImmutable.Form1+B : Name = b2
Be happy to have a critique of this approach.

thanks, Bill
When I consider the brief span of my life, swallowed up in the eternity before and after, the little space which I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which knows me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.
          Blaise Pascal

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 
AnswerRe: C# help? PinPopular
DaveyM6915-Dec-11 9:25
professionalDaveyM6915-Dec-11 9:25 
GeneralRe: C# help? Pin
Luc Pattyn15-Dec-11 19:59
sitebuilderLuc Pattyn15-Dec-11 19:59 
AnswerRe: C# help? Pin
gymmy15-Dec-11 12:31
gymmy15-Dec-11 12:31 
AnswerRe: C# help? Pin
DaveyM6915-Dec-11 13:18
professionalDaveyM6915-Dec-11 13:18 
QuestionC# question? Pin
Brian Reiber15-Dec-11 5:14
Brian Reiber15-Dec-11 5:14 
AnswerRe: C# question? Pin
DaveyM6915-Dec-11 5:32
professionalDaveyM6915-Dec-11 5:32 

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.