Announcement

Collapse
No announcement yet.

MyBase during create? (inheritance)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Edwin Knoppert
    replied
    Sorry for coming back on this.
    I still would like to find me a way to call the inherit class from the base class.
    It may not be possible..., if i just could reach the object from inside the baseclass i would be fine.
    I already understand the comparison may not do but this is somewhat what i need:

    (Baseclass)
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    abstract class Class1
    {
        public abstract string GetABC(int colIndex);
    
        public string GetXYZ(int colIndex)
        {
            return GetABC(colIndex);
        }
    }
    The class to use:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    class Class2 : Class1
    {
        public override string GetABC(int colIndex)
        {
            switch (colIndex)
            {
                case 1:
                    return "abc";
                case 2:
                    return "def";
            }
            return "";
        }
    }
    Being called like:
    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                Class2 c = new Class2();
                MessageBox.Show( c.GetXYZ(1));
            }
    The conclusion for com was that i may need to initialize but there is no automated moment to do so (this is actually what i am asking here).
    Unless you run the init call by testing a variable and place this line in every call.
    Call's not being overridden will never initialize.

    Conclusion(?):
    This means right after create i must call an initialize.
    (But that wouldn't be an automated call then)

    Leave a comment:


  • Edwin Knoppert
    replied
    >There may be instances where you don't want the base class create called.
    Imo not (never) and then.. an easy override, just a matter of logical programming imo.

    Leave a comment:


  • Bob Zale
    replied
    Actually, not true. The very good reason is that you may not want CREATE called in the inherited class.

    Leave a comment:


  • Brian Chirgwin
    replied
    Originally posted by jcfuller View Post
    As I said previous this is NOT other oop languages and is not a bug. I really see no reason for it when you can just call MyBase.Create? There may be instances where you don't want the base class create called.

    James
    If a constructor isn't created, PB could generate one with a call to MyBase.Create.

    If a constructor exists, PB doesn't generate one. Developer must call MyBase.Create in the created constructor if wanted.

    With generation of a default constructor typing and clutter of code would be saved. It would also prevent bugs where the developer forgets to initialize (call the base constructor) in sub classes.

    There is no reason why PB should not do this. One could write a constructor if the developer wanted to explicitly show it is being called.

    Leave a comment:


  • jcfuller
    replied
    It would be nice if the base class constructor was automatically called if the child/sub class had no constructor defined. In other words, MyBase.Create called automatically if a Constructor was not written. This process occurs in other OOP languages. Should this already be happening? Should this be reported as a bug or a request?
    As I said previous this is NOT other oop languages and is not a bug. I really see no reason for it when you can just call MyBase.Create? There may be instances where you don't want the base class create called.

    James

    Leave a comment:


  • Edwin Knoppert
    replied
    Oh boy, that does work!
    I thought that Mybase couldn't be used in the class method of the inh. class.
    But that restriction applies to the int. members..

    Yes, it would be nice if PB invoked the base class.

    Note that any other class method can not be invoked this way.
    Only Create and Destroy are accepted.
    (Maybe a logical restriction since this is a vtable system)

    Leave a comment:


  • Brian Chirgwin
    replied
    Call MyBase.Create

    It's a little bit of code but call the base class constructor.

    Code:
    ...
    
    Class Class2
        
        Class Method Create()
            ' Call parent object constructor
            mybase.create
    
            ' Do class 2 stuff here
    
        End Method
    
    ...
    If there is a constructor of the class, the base class constructor may be overridden. It should be the responsibility of the developer to call the base class constructor, if necessary. It should be first, called before any other code is done in the class 2.

    It would be nice if the base class constructor was automatically called if the child/sub class had no constructor defined. In other words, MyBase.Create called automatically if a Constructor was not written. This process occurs in other OOP languages. Should this already be happening? Should this be reported as a bug or a request?

    Leave a comment:


  • Edwin Knoppert
    replied
    It may be an incorrect design.
    The base class must do all code except the few Override functions.
    But the base class constructor/destructor is never executed and i am not sure if this is an implementation issue or simply undesired behaviour.
    The overrides should make it easy for me and let the base maintain it's instance variables.

    Here is an example, the brush handle remains 0.
    I know solvable but that implies more code.
    Better to investigate now than a rebuild for 2 or 300(!) classes

    Code:
    [color=#0000FF]Class[/color] Class1
    
        [color=#0000FF]Instance[/color] m_Handle [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
    
        [color=#0000FF]Class[/color] [color=#0000FF]Method[/color] [color=#0000FF]Create[/color]
    
            [color=#0000FF]MsgBox[/color] "Class1 Method Create"
            m_Handle = CreateSolidBrush( 0 )
    
        [color=#0000FF]End[/color] [color=#0000FF]Method[/color]
    
        [color=#0000FF]Class[/color] [color=#0000FF]Method[/color] [color=#0000FF]Destroy[/color]
            [color=#0000FF]MsgBox[/color] "Class1 Method Destroy"
        [color=#0000FF]End[/color] [color=#0000FF]Method[/color]
    
        [color=#0000FF]Interface[/color] Interface1: [color=#0000FF]Inherit[/color] [color=#0000FF]IUnknown[/color]
    
            [color=#0000FF]Property[/color] [color=#0000FF]Get[/color] [color=#0000FF]Handle[/color]() [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
                [color=#0000FF]Property[/color] = m_Handle
            [color=#0000FF]End[/color] [color=#0000FF]Property[/color]
    
        [color=#0000FF]End[/color] [color=#0000FF]Interface[/color]
    
    [color=#0000FF]End[/color] [color=#0000FF]Class[/color]
    
    [color=#0000FF]Class[/color] Class2
    
        [color=#0000FF]Interface[/color] Interface2: [color=#0000FF]Inherit[/color] Class1, Interface1
    
    [color=#007F00]'        Override Property Get Handle() As Long[/color]
    [color=#007F00]'            Property = MyBase.Handle            [/color]
    [color=#007F00]'        End Property[/color]
    
        [color=#0000FF]End[/color] [color=#0000FF]Interface[/color]
    
    [color=#0000FF]End[/color] [color=#0000FF]Class[/color]
    
    [color=#0000FF]Function[/color] TEST() [color=#0000FF]As[/color] [color=#0000FF]Long[/color]
    
        [color=#0000FF]Local[/color] o [color=#0000FF]As[/color] Interface2
        o = [color=#0000FF]Class[/color] "Class2"
        
        [color=#0000FF]MsgBox[/color] [color=#0000FF]Str$[/color]( o.Handle )     
    
    [color=#0000FF]End[/color] [color=#0000FF]Function[/color]

    Added: i forgot the deleteobject() in the destroy.

    Leave a comment:


  • jcfuller
    replied
    Edwin,
    I'm not sure what you are trying to do but it may be a faulty design decision.
    Do not look at Pb Classes as you would c++,java,c#. Look at it more as COM with a bit of extras. COM has no inheritance so it may help to look at your design problems from that aspect first, then see if you can use the extras.

    In a number of situations I decided against inheritance and used containment instead.

    James

    Leave a comment:


  • Edwin Knoppert
    replied
    Though, i have this serious design issue.

    The base class maintains a handle.
    The destroy is never executed so the handle remains open.

    The 'inheritance class''s destroy is also not able to call some kind of 'dispose' method in the base class's interface.

    Since i will have hundreds of inheritance classes i can not embed them in the same base class.

    Any idea to destroy the handle in the base class when the inh. class get's unloaded?

    Thanks,

    Leave a comment:


  • Edwin Knoppert
    replied
    Thank you.

    Leave a comment:


  • Steve Rossell
    replied
    Classes (aka class methods) do not inherit other interfaces, only interfaces inherit other interfaces. Just like you said, you probably need a Init method in an interface that inherits this other interface.

    Leave a comment:


  • Edwin Knoppert
    started a topic MyBase during create? (inheritance)

    MyBase during create? (inheritance)

    I am trying to set a property from the inherited class:
    Code:
        Class Method Create
             MyBase.ID = 1234
        End Method
    I get:
    PowerBASIC for Windows
    PB/Win Version 9.01
    Copyright (c) 1996-2009 PowerBasic Inc.
    Venice, Florida USA
    All Rights Reserved

    Error 596 in C:\.............
    (33:009): Inherited interface expected
    Line 33: MyBase.ID = 1234
    Is there a simple workaround?
    Now it seems i need to call an initialization method first..
Working...
X