Home All Groups Group Topic Archive Search About

Accessing inherited variables

Author
5 Jun 2006 3:06 PM
MrJim
How should variables be declared and referenced in both the base and
derived form so they can be accessed?

Author
5 Jun 2006 4:17 PM
Ken Tucker [MVP]
Hi,

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

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

Show quoteHide quote
"MrJim" wrote:

> How should variables be declared and referenced in both the base and
> derived form so they can be accessed?
>
Author
6 Jun 2006 3:29 PM
MrJim
Ken Tucker [MVP] wrote:
> Hi,
>
>         You should be able to access stuff in a base class with
> mybase.VariableName
>
> Ken
> ---------------------
>
> "MrJim" wrote:
>
>> How should variables be declared and referenced in both the base and
>> derived form so they can be accessed?
>>
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.
Author
6 Jun 2006 5:11 PM
AlanT
> 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.

The important point is not whether or not how the subs are declared but
how the variable is declared.

>From the description it sounds like you have something along the lines
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.
Author
7 Jun 2006 9:23 AM
MrJim
AlanT wrote:
Show quoteHide quote
>> 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.
>
> The important point is not whether or not how the subs are declared but
> how the variable is declared.
>
>>From the description it sounds like you have something along the lines
> 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.
>
Thanks a lot for your help, that makes things clear
Author
5 Jun 2006 5:31 PM
AlanT
MrJim wrote:
> How should variables be declared and referenced in both the base and
> derived form so they can be accessed?


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.