473,395 Members | 1,969 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,395 software developers and data experts.

accessing local properties

Ok,

kind of a silly question I just want to know what the proper method is,

When I have local Properties, ie:

private bool _someVal;
public bool SomeVal {
get { return _someVal; }
}

When I accessing this property within the class it exists in, should I
use _someVal or always use the Property to retrieve the value? I guess
it really doesn't make a different but I noticed this is one part of my
code that isn't consistent, sometimes I use the property sometimes I
don't is there any OOP rules here to follow? THank you

Nov 17 '05 #1
6 1211
Dkode,

Personally, I use the property, not the field that is backing it. The
reason for this is that with properties, there can be code that affects the
return value. More often than not, it is this kind of encapsulation that
you want when using properties, so it is best to use them everywhere that
you expose them (otherwise, it would be pointless to have the property in
the first place).

I say only in places where there is an explicit reason to not use the
property, you should use the field.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DKode" <dk****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Ok,

kind of a silly question I just want to know what the proper method is,

When I have local Properties, ie:

private bool _someVal;
public bool SomeVal {
get { return _someVal; }
}

When I accessing this property within the class it exists in, should I
use _someVal or always use the Property to retrieve the value? I guess
it really doesn't make a different but I noticed this is one part of my
code that isn't consistent, sometimes I use the property sometimes I
don't is there any OOP rules here to follow? THank you

Nov 17 '05 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eT**************@TK2MSFTNGP14.phx.gbl...
Dkode, I say only in places where there is an explicit reason to not use the
property, you should use the field.


Yeah, like when it's ReadOnly ;P

Mythran

Nov 17 '05 #3
I don't see why that is a reason to use the field over the property.
Just because the backing field is read only, or the property is read only,
doesn't mean that the the relationship between the backing field and the
return result from the property is always going to return the same value.

It's ^because^ of this that you should use the property instead of the
backing field.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eT**************@TK2MSFTNGP14.phx.gbl...
Dkode,

I say only in places where there is an explicit reason to not use the
property, you should use the field.


Yeah, like when it's ReadOnly ;P

Mythran

Nov 17 '05 #4

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ub**************@TK2MSFTNGP09.phx.gbl...
I don't see why that is a reason to use the field over the property.
Just because the backing field is read only, or the property is read only,
doesn't mean that the the relationship between the backing field and the
return result from the property is always going to return the same value.

It's ^because^ of this that you should use the property instead of the
backing field.


I was referring to a developer attempting to "set" the value of a
property...you can't if it's ReadOnly, in which case, you'd set the member
variable instead :)

Mythran

Nov 17 '05 #5

"DKode" <dk****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Ok,

kind of a silly question I just want to know what the proper method is,

When I have local Properties, ie:

private bool _someVal;
public bool SomeVal {
get { return _someVal; }
}

When I accessing this property within the class it exists in, should I
use _someVal or always use the Property to retrieve the value?


Not a silly question at all. In fact, it's the subject of much debate.

There are 3 situations to consider:

1. You want the functionality of the property. Obviously, use the property.
2. You explicitly DO NOT want the functionality of the property. Obviously,
do not use the property.
3. Using the property and the field is functionally equivalent. Here you've
got to see into the future to make a determination. If you use the property,
then your code is dependent on the property getter/setter code. If you use
the field, it is not. If someone should change the getter/setter in the
future, what should happen to the piece of code you are writing? I think you
have to take it on a case by case basis.
Nov 17 '05 #6
thank you very much for your responses.

I am going along, attempting to improve my code to a more normalized
foundation and this is one thing I was still struggling with.

thank you!

DKode

Nov 17 '05 #7

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

Similar topics

2
by: Gijs Beukenoot | last post by:
Hi all, I have an existing application that checks a few things before it launches another application. One thing that the application needs to check is the subscription properties of both the...
18
by: Joe | last post by:
Hi, I am trying to alter the refresh rate of an online webpage in a webbrowser control using MFC. However the Timer ID is stored in a local variable and I don't know how to access it. Is there a...
3
by: prodirect | last post by:
Hi all, I hope someone can help me. I've recently created a database and wanted to put it up on an ftp sight so that multiple people could access the same tables at the same time from different...
0
by: Joergen Bech | last post by:
Fairly new to ASP.NET 1.1. Getting the error below when running application on a web server outside of my control, but only the first time I run it: 1. After a long period of inactivity (or...
3
by: Daniel Liberman | last post by:
Hi, everyone. That's my environment: - I have a pocket pc (iPAQ h4350) application developed with VSNET2003/C# that has a SQL Server CE Database, running. That's working fine. Obs: the pocket...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
1
by: drk.kumar | last post by:
I have an implementation issue with WMI scripts to check the user machine processor. The implementation is working fine in the local machine (Windows XP operating system). It is throwing script...
1
by: Pixaar | last post by:
hello.. I am new to VB and really new to LDAP I am trying to write an application for our lunch card system...where a student's card is read by a barcode reader and the information (a string)...
2
by: Fritjolf | last post by:
Hi. I've written some code to list objects of type "Computer" from our firms primary AD controller. The code is simple: DirectoryEntry myDirEntry = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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,...

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.