none
Chain of Command pattern or another solution RRS feed

  • Question

  • I've spent the whole day experimenting but still could not make it. Here is the idea:

     

    Code Snippet

     

    class A {

    A_int variable;

     

    void init();

     

    class A_int {

    }

    }

     

    class B : A {

    new B_int variable;

     

    class B_int : A_int {

    }

    }

     

     

    I need to keep variable "variable" in both classes for consistency but its implementation is different, i.e. A_int vs B_int

     

    Now, I think, the crucial point: class B does not redefine method init(), because there is nothing to redefine. The init logic is the same for both classes (and I do not want to do it in the constructor for other reasons). The problem is the following:

     

    Code Snippet

     

    B var_b = new B();

    b.init();

     

     

    this code jumps to the only defined init method, but this method sees on the definition of variable "variable" from class A and not from class B where it is redefined.

     

    I am really lost here and see no way how I could resolve it.

     

    Friday, February 22, 2008 12:35 AM

Answers

  • Your question is still not clear as to what behavior you are looking for.  Why the nested classes?

     

    Rudedog

     

    http://www.c-sharpcorner.com/UploadFile/rmcochran/instantiationBuilder06232007171715PM/instantiationBuilder.aspx

    http://www.c-sharpcorner.com/UploadFile/rmcochran/instatiationNone06232007185828PM/instatiationNone.aspx

    Friday, February 22, 2008 11:44 AM
    Moderator

All replies

  • I'm not trying to be mean, but puzzle over this.

     

    Code Snippet

    class StaticClass

    {

    public static int A = 0;

    public static int B = 0;

    }

     

     

    ...and this...

     

    Code Snippet

    class InstanceClass

    {

    public int C = 0;

    public int D = 0;

    }

     

     

    ...and this...

     

    Code Snippet

    class MixedClass

    {

    public static int A = 0;

    public static int B = 0;

    public int C = 0;

    public int D = 0;

    public void Method()

    {

    }

    }

     

     

    ...and finally this...

     

    Code Snippet

    class TestStaticInstanceClass : MixedClass

    {

    public static void TestStatic()

    {

    StaticClass.A = 0;

    }

     

    public static void TestInstance()

    {

    InstanceClass ic = new InstanceClass();

    ic.C = 0;

    }

     

    public static void TestMixed()

    {

    StaticClass sc = new StaticClass();

    InstanceClass ic = new InstanceClass();

    MixedClass mc = new MixedClass();

    // test these variables for which members are accessible.

    //this.C = 0;

    A = 0;

    }

    public override void Method()

    {

    }

    public void BaseMethod()

    {

    base.Method();

    }

    }

     

     

    I suggest that you type this final class in, instead of copy/paste.  Watch what the IDE will show, and more importantly not show.

     

    Rudedog

    Friday, February 22, 2008 1:11 AM
    Moderator
  • ...and this...

     

    Code Snippet

    public abstract class aaa 
        {
            public virtual string MyMethod()
            {
                return "Class AAA";
            }

        }

        public class bbb : aaa
        {
            public override string MyMethod()
            {
                return "Class BBB";
            }
            public string MyBaseMethod()
            {
                return base.MyMethod();
            }
        }

        public class ccc : bbb
        {
            public new virtual string MyMethod()
            {
                return base.MyBaseMethod();
            }
        }
        public class ddd : ccc
        {
            // this class cannot be inherited
            public sealed override string MyMethod()
            {
                return "Class DDD";
            }
        }

     

     

    Friday, February 22, 2008 1:24 AM
    Moderator
  • Honestly, I did not understand what you wanted to tell me but I also realized a crucial point is missing from the description of the problem. Here I try it again:

     

    Code Snippet

    class A {

    A_int engine;

     

    void init() {

    engine.DoWork();

    }

     

    class A_int {

    virtual void DoWork() {

    // this is the base implementation of engine

    }

    }

    }

     

    class B : A {

    new B_int engine;

     

    class B_int : A_int {

    override void DoWork() {

    // this is the enhanced implementation

    // can we get here or not?

    base.DoWork();

    }

    }

    }

     

    void main() {

    B var_b = new B();

    b.init();

    }

     

     

    You can see that DoWork is a virtual method which adds (not replaces!) engine functionality to its base implementation.

    What I was hoping for and what seems not to work is that when I redefine "engine" in class B, this redefintion completely erases the inherited engine definition and thus when engine.DoWork() is called by method init() from class A (and class B inherits this method), the reference to engine is taken from class B and not class A (because implementations are different) which seems not to be the case.

     

    Friday, February 22, 2008 8:12 AM
  • Your question is still not clear as to what behavior you are looking for.  Why the nested classes?

     

    Rudedog

     

    http://www.c-sharpcorner.com/UploadFile/rmcochran/instantiationBuilder06232007171715PM/instantiationBuilder.aspx

    http://www.c-sharpcorner.com/UploadFile/rmcochran/instatiationNone06232007185828PM/instatiationNone.aspx

    Friday, February 22, 2008 11:44 AM
    Moderator
  • this builder pattern looks exactly like what I was looking for. many tnx

     

    Friday, February 22, 2008 2:54 PM
  • Great.  I figured that was what you were after. 

    Take a look at those samples I posted. 

    They are good examples of inheritance and overriding methods.

     

    Rudedog

     

    Friday, February 22, 2008 3:15 PM
    Moderator