|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Accessing inherited variablesHow should variables be declared and referenced in both the base and
derived form so they can be accessed? 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? > Ken Tucker [MVP] wrote:
> Hi, I've tried this method, but all I got was the variable is not a member > > 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? >> of deviceapp1.etc. Both the base and derived sub are declared as protected but still no luck. > I've tried this method, but all I got was the variable is not a member The important point is not whether or not how the subs are declared but> of deviceapp1.etc. > Both the base and derived sub are declared as protected but still no luck. how the variable is declared. >From the description it sounds like you have something along the lines BaseClassof 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. AlanT wrote:
Show quoteHide quote >> I've tried this method, but all I got was the variable is not a member Thanks a lot for your help, that makes things clear>> 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. > MrJim wrote:
> How should variables be declared and referenced in both the base and You might try it this way> derived form so they can be accessed? 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.
Can't create AutoFiltered Excel worksheet using VB.NET
CODE to Test ALPHA Transparency Textbox Lines Limit form.load don't exist anymore ? Please. join 2 tables and datagrid Read value of all columns from datatable together CreateObject error Printing to screen in VB.NET Failed to decrypt Need to start word 2003 and show existing rtf doc in VB.Net |
|||||||||||||||||||||||