Accessing inherited variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MrJim

    #1

    Accessing inherited variables

    How should variables be declared and referenced in both the base and
    derived form so they can be accessed?
  • Ken Tucker [MVP]

    #2
    RE: Accessing inherited variables

    Hi,

    You should be able to access stuff in a base class with
    mybase.Variable Name

    Ken
    ---------------------

    "MrJim" wrote:
    [color=blue]
    > How should variables be declared and referenced in both the base and
    > derived form so they can be accessed?
    >[/color]

    Comment

    • AlanT

      #3
      Re: Accessing inherited variables


      MrJim wrote:[color=blue]
      > How should variables be declared and referenced in both the base and
      > derived form so they can be accessed?[/color]


      You might try it this way

      Declare all variables in a class as private.

      If the variable needs to be access in derived classes, then provide a
      protected property

      e.g.
      dim _conn as Connection

      Protected Property Connection as Connection
      ...
      end property


      If the variable needs to be access outside the class and its
      descendents then create a public property.

      NOTE: In both of the above cases, you will probably want to access the
      variable through the property even in the base class. Although you
      have direct access to the variable it is useful to chokepoint all
      access, e.g. if you want to raise an event when the value is changed.

      Form specific:

      By default, the IDE makes all control variables Friend.
      This translates as 'public to other classes in the same assembly' which
      can cause problems if you inherit from them in another assembly.

      there are a number of choices here

      1) make them protected
      2) make them protected friend
      3) Add wrapper functions/properties to access them

      e.g.
      if you have a listbox in the base class to which you want to add
      items, then instead of directly access the listbox in the derived
      class [mybase.listbox1 .items.add(xxx)], in the base class create a
      protected sub to do it for you

      Protected Sub AddItem(w as widget)
      Listbox1.Items. add(w)
      end sub

      why? Say, halfway through you decide that you want to use a treeview
      instead of a listview, all you need change is the base class.

      hth,
      Alan.

      Comment

      • MrJim

        #4
        Accessing inherited variables

        Ken Tucker [MVP] wrote:[color=blue]
        > Hi,
        >
        > You should be able to access stuff in a base class with
        > mybase.Variable Name
        >
        > Ken
        > ---------------------
        >
        > "MrJim" wrote:
        >[color=green]
        >> How should variables be declared and referenced in both the base and
        >> derived form so they can be accessed?
        >>[/color][/color]
        I've tried this method, but all I got was the variable is not a member
        of deviceapp1.etc.
        Both the base and derived sub are declared as protected but still no luck.

        Comment

        • AlanT

          #5
          Re: Accessing inherited variables

          [color=blue]
          > I've tried this method, but all I got was the variable is not a member
          > of deviceapp1.etc.
          > Both the base and derived sub are declared as protected but still no luck.[/color]

          The important point is not whether or not how the subs are declared but
          how the variable is declared.
          [color=blue]
          >From the description it sounds like you have something along the lines[/color]
          of

          BaseClass

          Protected overridable Sub Foo()

          dim bar as integer = 19

          End Sub


          DerivedClass

          Protected Overrides Sub Foo()

          bar = 23

          End Sub


          If so, you cannot access bar within the derived sub. it is not a
          instance variable, it is local to the Base:Foo() sub and can only be
          seen within that sub. If you want a variable that can be accessed in
          both Base and Derived classes it needs to be a member of the class

          e.g.


          BaseClass

          protected _bar as integer

          Protected overridable Sub Foo()
          _bar = 19
          End Sub


          DerivedClass

          Protected Overrides Sub Foo()
          _bar = 23
          End Sub

          or,

          BaseClass

          Private _bar as integer

          Protected Property Bar as integer
          get
          return _bar
          end get
          set (value as integer)
          _bar = value
          end set
          end property

          Protected overridable Sub Foo()
          Bar = 19
          End Sub


          DerivedClass

          Protected Overrides Sub Foo()
          Bar = 23
          End Sub


          hth,
          Alan.

          Comment

          • MrJim

            #6
            Re: Accessing inherited variables

            AlanT wrote:[color=blue][color=green]
            >> I've tried this method, but all I got was the variable is not a member
            >> of deviceapp1.etc.
            >> Both the base and derived sub are declared as protected but still no luck.[/color]
            >
            > The important point is not whether or not how the subs are declared but
            > how the variable is declared.
            >[color=green]
            >>From the description it sounds like you have something along the lines[/color]
            > of
            >
            > BaseClass
            >
            > Protected overridable Sub Foo()
            >
            > dim bar as integer = 19
            >
            > End Sub
            >
            >
            > DerivedClass
            >
            > Protected Overrides Sub Foo()
            >
            > bar = 23
            >
            > End Sub
            >
            >
            > If so, you cannot access bar within the derived sub. it is not a
            > instance variable, it is local to the Base:Foo() sub and can only be
            > seen within that sub. If you want a variable that can be accessed in
            > both Base and Derived classes it needs to be a member of the class
            >
            > e.g.
            >
            >
            > BaseClass
            >
            > protected _bar as integer
            >
            > Protected overridable Sub Foo()
            > _bar = 19
            > End Sub
            >
            >
            > DerivedClass
            >
            > Protected Overrides Sub Foo()
            > _bar = 23
            > End Sub
            >
            > or,
            >
            > BaseClass
            >
            > Private _bar as integer
            >
            > Protected Property Bar as integer
            > get
            > return _bar
            > end get
            > set (value as integer)
            > _bar = value
            > end set
            > end property
            >
            > Protected overridable Sub Foo()
            > Bar = 19
            > End Sub
            >
            >
            > DerivedClass
            >
            > Protected Overrides Sub Foo()
            > Bar = 23
            > End Sub
            >
            >
            > hth,
            > Alan.
            >[/color]
            Thanks a lot for your help, that makes things clear

            Comment

            Working...