473,221 Members | 2,153 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,221 software developers and data experts.

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 2716
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
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.