473,466 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accessing class member variables - properties or variables?

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 expose the
variables or is it OK to just access the variables directly? Keep in
mind that I am talking about accessing the variables from within the
class that they are defined. Thanks!

Nov 21 '05 #1
8 2728
Regardless of what level of access you give an instance variable, it
can be accessed in any instance method in the class definition.

Nov 21 '05 #2
Hi,

I would access the variables directly from within the class. Use
properties for accessing them from outside the class.

Ken
-----------------
"dwok" <de***@wubbafish.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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 expose the
variables or is it OK to just access the variables directly? Keep in
mind that I am talking about accessing the variables from within the
class that they are defined. Thanks!
Nov 21 '05 #3
Only expose your private members if you wish them to be publicly acessible,
that sounded horrible... I only create properties to be externally consumed
by another class, say I have a class that gets loaded from the database and I
have a property that accesses the data:

Public Overridable ReadOnly Property SomeData() As String
Get
Return m_SomeData
End Get
End Property

I'm going to poulate this properties value using the private member
m_SomeData, that way I don't interfere with anyone else's implementation
should they descide to override this property.

"dwok" wrote:
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 expose the
variables or is it OK to just access the variables directly? Keep in
mind that I am talking about accessing the variables from within the
class that they are defined. Thanks!

Nov 21 '05 #4
"dwok" <de***@wubbafish.net> schrieb:
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 expose the
variables or is it OK to just access the variables directly? Keep in
mind that I am talking about accessing the variables from within the
class that they are defined.


I think this is the way most developers do it.

Nevertheless, I use 'Static' [VB.NET] variables inside methods whenever it
makes sense to use them, and I never access member variables which are
holding property values directly, except in the property's 'Get' or 'Set'
block.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5

"dwok" <de***@wubbafish.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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 expose the
variables or is it OK to just access the variables directly? Keep in
mind that I am talking about accessing the variables from within the
class that they are defined. Thanks!


I assume by 'member' variables, you mean 'instance' variables (which can be
thought of as 'properties' of an instance of the class). <<<as a general
rule>>> Normally, instance variables are declared private and normally,
the 'get' and 'set' methods to access them are declared public. individual
mileage may vary.
'Static' variables (and constants) belong to the class without regard to any
instance (VB.NEt's Color.Blue for example).
Nov 21 '05 #6
It all depends upon the situation.

For example, I have a class in which setting a property through the
"set" method causes the class to fire an event saying that the value
changed. Sometimes, from within the class I want to set it in this way;
if I forget, any subscribers won't know that the value changed. Other
times, I definitely want to just set the value, without firing an
event.

What I'm driving at here is that properties often do more than just set
the value of a private member: they do additional processing. This is
particularly common in WinForms applications in which objects must
notify the UI that something has changed and needs to be redisplayed.

In those cases in which a property does nothing more than set the
member variable, most developers just set the member variable directly.

Nov 21 '05 #7
Hal,

"Hal Rosser" <hm******@bellsouth.net> schrieb:
'Static' variables (and constants) belong to the class without regard to
any
instance (VB.NEt's Color.Blue for example).


In VB.NET, variables marked as 'Static' belong to the method they are
decalared in:

\\\
Private Sub Foo()
Static x As Integer
...
End Sub
///

Properties like 'Color.Blue' are shared properties ('Shared' modifier).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #8

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eV**************@TK2MSFTNGP15.phx.gbl...
Hal,

"Hal Rosser" <hm******@bellsouth.net> schrieb:
'Static' variables (and constants) belong to the class without regard to
any
instance (VB.NEt's Color.Blue for example).


In VB.NET, variables marked as 'Static' belong to the method they are
decalared in:

\\\
Private Sub Foo()
Static x As Integer
...
End Sub
///

Properties like 'Color.Blue' are shared properties ('Shared' modifier).


Oh YEAH! you're right! I should not have inserted a reference from VB, since
I was answering the OP in terms of Java. - In VB, 'static' refers to a
variable that keeps its value from call to call - and is really not
applicable in the context of this thread. I was answering the OP in terms of
Java (but foolishly inserted a VB reference - ie: Color.Blue )
In Java - the Color.Blue reference analogy would be referring to a variable
named "Blue" which was declared "static" in the class named "Color". Hence
the ability to reference "Blue" without creating an instance of the "Color"
class.
Nov 21 '05 #9

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()...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
14
by: Lee Franke | last post by:
I can't seem to figure this one out. Here is my class structure namespace name { public class foo { } }
13
by: thomasp | last post by:
I have written my first Class and am posting it to this newsgroup with hopes that I can get some feedback on what is right and what is wrong with it. All comments are welcome, but more interested...
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
5
by: TS | last post by:
is it preferred to access member variables directly in code, on the page that declared them, versus going thru a property accessor? I would think that since theres no security concerns or anything...
1
by: sk.rasheedfarhan | last post by:
Hi , I am using C# I am having 4 classes. like below. public class A { String m_strRuleName; String m_strRuleGuid; // Some member functions. public Object NextItem; }
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
4
by: Joseph Paterson | last post by:
Hi all, I'm having some trouble with the following code (simplified to show the problem) class Counter { protected: int m_counter; }
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.