"Jeff Callahan" <JeffCal@online.microsoft.com> wrote in message
news:f6GcdL6rDHA.3024@cpmsftngxa07.phx.gbl...
> greg,
>
> it does sound like your design could be organized in a better way. i'm
not
> exactly sure what behavior you're going for, but it seems like you want
> this:
>
> when C is constructed, the C object calls the B constructor, which calls
> the A constructor, which adds stuff for A, then the B constructor adds
> stuff for B, then the C constructor adds stuff for C.
>
> if AddStuff works in different ways depending on which class it is called
> from, eg it does action 1 when A calls it, action 2 when B calls it, and
> action 3 when C calls it, then AddStuff should really be a different
method
> in each of these classes. i suggest looking into the virtual and new
> keywords for this. this is a common problem in object oriented
development
> which is solved by Polymorphism. i've posted one example below - notice i
> defined A's AddStuff with the virtual keyword and the others are defined
> with the new keyword in the method signature. when you run this code, the
> output is A->B->C.
>
> let me know if you have any other questions.
>
> jeff.
>
> namespace ConsoleApplication4
> {
> class Class1
> {
> static void Main(string[] args)
> {
> C c = new C();
> Console.ReadLine();
> }
> }
>
> class A
> {
> public A()
> {
> this.AddStuff();
> }
>
> public virtual void AddStuff()
> {
> Console.WriteLine("AddStuff - A");
> }
> }
> class B : A
> {
> public B()
> {
> this.AddStuff();
> }
>
> public new void AddStuff()
> {
> Console.WriteLine("AddStuff - B");
> }
> }
> class C : B {
> public C()
> {
> this.AddStuff();
> }
>
> public new void AddStuff()
> {
> Console.WriteLine("AddStuff - C");
> }
> }
> }
>
> --
>
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Use of included script samples are subject to the terms specified at
>
http://www.microsoft.com/info/cpyright.htm >
> Note: For the benefit of the community-at-large, all responses to this
> message are best directed to the newsgroup/thread from which they
> originated.
>