473,327 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Accessing inherited variables

How should variables be declared and referenced in both the base and
derived form so they can be accessed?
Jun 5 '06 #1
5 5807
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?

Jun 5 '06 #2

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.

Jun 5 '06 #3
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.
Jun 6 '06 #4
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.

Jun 6 '06 #5
AlanT wrote:
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
Jun 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: keepyourstupidspam | last post by:
Hello, I have a class X with a member function setConfigValues(). I want to access this member function in another class Y. Y is not inherited from X. A member function of class X called run()...
6
by: Jack Addington | last post by:
I've got a bit of a problem acceessing a class that I am using as a 'global' placeholder of numerous static variables I'm using across the application. Basically I need a place to store a reference...
4
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
8
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
3
by: Jeff User | last post by:
Hello I am using C#, .net1.1 Vis Studio 2003 I am using homeBase.aspx.cs page as a base for several other aspx/aspx.cs web pages. The base page handles some operations that are common to all...
2
by: MrJim | last post by:
How should variables be declared so that those in the base form can be changed in a form 'inherited' from it?
1
by: David Belohrad | last post by:
Dear All, could someone give a hint? I'd like to share the resources as follows: A shared class which works as counter of number of shared resources: class Shared { public: Q_PCBShared()...
16
by: gwoodhouse | last post by:
Good Morning, Ive been programming in c# for a few months now, and one of the things i havnt quite figured out is this: I would like to have inherited classes with their own set of variables....
13
by: PragueExpat | last post by:
I (think) that I've come up with a pattern that I haven't seen in any publications so far and I would like some feedback. Basically, I was looking for a way to inherit private functions and I came...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.