Hi,
I have a class with a get-only public bool property. This is good because I
want users of this class to ONLY READ it.
So I've defined it thus:
private bool _IsDirty;
public bool IsDirty{
get{ return _IsDirty; }
}
.... no problems there.
But the methods from WITHIN this class needs to write to it, but the
compiler will not allow me. So I guess I have 2 choices:
1. Put a set{ _IsDirty = value; } clause. This will allow the methods from
WITHIN this class to write to it (i.e. IsDirty = true;), but it defeats the
purpose because the users of this class are now ALSO able to modify it -
THIS IS BAD!.
2. When writing to it, write to the private value (i.e. _IsDirty = true; ).
This is my preferred choice at the moment, but it seems to defeat the
purpose of encapsulation and good programming practice; because it the
implementation of IsDirty is to be changed in the future, I'll have to step
through all the code where I reference it.
What is the preferred way, or is there another approach? have I missed
something?
Thanks,
jack. 4 1696
Hi Jack,
"Jack" <ja**@nospam.co m.ukwrote in message
news:45******@q uokka.wn.com.au ...
Hi,
I have a class with a get-only public bool property. This is good because
I want users of this class to ONLY READ it.
So I've defined it thus:
private bool _IsDirty;
public bool IsDirty{
get{ return _IsDirty; }
}
... no problems there.
But the methods from WITHIN this class needs to write to it, but the
compiler will not allow me. So I guess I have 2 choices:
Don't know if this works in VS2002/2003 also but in VS2005 you can have
different visibilities on getter and setter:
public bool IsDirty
{
get { return _IsDirty; } // this is public
internal set { _IsDirty = value; } // this is internal
}
--
SvenC
1. Put a set{ _IsDirty = value; } clause. This will allow the methods from
WITHIN this class to write to it (i.e. IsDirty = true;), but it defeats
the purpose because the users of this class are now ALSO able to modify
it - THIS IS BAD!.
2. When writing to it, write to the private value (i.e. _IsDirty =
true; ). This is my preferred choice at the moment, but it seems to defeat
the purpose of encapsulation and good programming practice; because it the
implementation of IsDirty is to be changed in the future, I'll have to
step through all the code where I reference it.
What is the preferred way, or is there another approach? have I missed
something?
Thanks,
jack.
SvenC, thanks.
This was just what I was looking for: I took a look at this modifier before,
but I had no idea you could place it at the Get & Set level of the public
variable (like your code example)
Again, thank-you.
Jack.
"SvenC" <Sv***@communit y.nospamwrote in message
news:OE******** ******@TK2MSFTN GP06.phx.gbl...
Hi Jack,
"Jack" <ja**@nospam.co m.ukwrote in message
news:45******@q uokka.wn.com.au ...
>Hi,
I have a class with a get-only public bool property. This is good because I want users of this class to ONLY READ it. So I've defined it thus:
private bool _IsDirty; public bool IsDirty{ get{ return _IsDirty; } }
... no problems there.
But the methods from WITHIN this class needs to write to it, but the compiler will not allow me. So I guess I have 2 choices:
Don't know if this works in VS2002/2003 also but in VS2005 you can have
different visibilities on getter and setter:
public bool IsDirty
{
get { return _IsDirty; } // this is public
internal set { _IsDirty = value; } // this is internal
}
--
SvenC
>1. Put a set{ _IsDirty = value; } clause. This will allow the methods from WITHIN this class to write to it (i.e. IsDirty = true;), but it defeats the purpose because the users of this class are now ALSO able to modify it - THIS IS BAD!. 2. When writing to it, write to the private value (i.e. _IsDirty = true; ). This is my preferred choice at the moment, but it seems to defeat the purpose of encapsulation and good programming practice; because it the implementation of IsDirty is to be changed in the future, I'll have to step through all the code where I reference it.
What is the preferred way, or is there another approach? have I missed something?
Thanks, jack.
"Jack" <ja**@nospam.co m.ukwrote in message
news:45******@q uokka.wn.com.au ...
Hi,
I have a class with a get-only public bool property. This is good
because I want users of this class to ONLY READ it.
So I've defined it thus:
private bool _IsDirty;
public bool IsDirty{
get{ return _IsDirty; }
}
... no problems there.
But the methods from WITHIN this class needs to write to it, but the
compiler will not allow me.
<snip>
There is no reason the compiler should prevent you from changing the
value of the private member.I suspect you are attempting to change it
via the non existent setter.
You need to type
_IsDirty = true;
as opposed to
IsDirty = true;
If this is not your problem then I suggest you post your code since this
should not be a problem.
Hope this helps
Bill
Jack wrote:
I have a class with a get-only public bool property. This is good because I
want users of this class to ONLY READ it.
So I've defined it thus:
private bool _IsDirty;
public bool IsDirty{
get{ return _IsDirty; }
}
... no problems there.
But the methods from WITHIN this class needs to write to it, but the
compiler will not allow me. So I guess I have 2 choices:
1. Put a set{ _IsDirty = value; } clause. This will allow the methods from
WITHIN this class to write to it (i.e. IsDirty = true;), but it defeats the
purpose because the users of this class are now ALSO able to modify it -
THIS IS BAD!.
Yes, this is bad. THIS violates incapsulation.
2. When writing to it, write to the private value (i.e. _IsDirty = true; ).
This is my preferred choice at the moment, but it seems to defeat the
purpose of encapsulation and good programming practice; because it the
implementation of IsDirty is to be changed in the future, I'll have to step
through all the code where I reference it.
This doesn't violate encapsulation: "encapsulat ion" refers to the fact
that there are certain things that code _outside_ your class cannot
change without going through your class's code to do it.
"Encapsulat ion" places no restrictions on what the code _inside_ the
class can do. Your class's private code always has wide-open access to
your class's state.
That said, setting the value directly may or may not be what you want.
It comes with advantages and disadvantages. The disadvantage being that
if you want to make some guarantees about the values of that field it's
much easier to put some code between your internal class code and the
value, rather than making sure in every place you set it that you don't
violate the guarantees. I find that I very rarely care about this, and
usually set private members directly in code.
In those cases in which you need to place a code buffer between the
class code and the field, there are two ways to do it. As other posters
pointed out, in .NET 2.0 you can specify a setter and give it a
different scope modifier. However, this isn't possible in .NET 1.1.
Since I'm on 1.1, I do this:
public bool IsSelected
{
get { return this._isSelecte d; }
}
private void SetIsSelected(b ool isSelected)
{
this._isSelecte d = isSelected;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michael Levin |
last post by:
I've got the following problem. I'm a biologist and I have a device at work
which monitors my frog habitat. The device has a bunch of sensors, and runs
an embedded html server with some java functions defined which know how to
read the hardware sensorts. I access it from wherever I am via any browser,
and it displays the measurements (a set of simple numbers) as Java applets
in a simple html page. One entry in this page (a table cell) looks...
|
by: Brett |
last post by:
We have an application where the iusr_<srvr> has write NTFS
permissions to a folder under the wwwroot (i.e. wwwroot\folder) and
also there is a SQL Server database where we must give the iusr_<srvr>
read and write permissions on the database in order for the
application to work. Anonymous access is enabled on the default web
site where this web app is located.
Because we are using the iusr is there simple exploit code that could
allow...
|
by: Jesper |
last post by:
Im running Windows Server 2003 trying to get my ASP.NET applications
to access the file system.
My Web application needs to have write access to some folders in the
virtual directory from which it runs. The server is a domain
controller, so I don't have the ASPNET user to assign rights to.
I've tried several guides from several sites.. but none seem to work,
I keep getting the: "..Access to the path "..." is denied.
|
by: Stephane |
last post by:
Hi,
I'm sorry, this question is also posted in dotnet.framework, I don't know in
which newsgroup my question belongs to...
I'm trying to write in the event log and it looks like it's any of my
applications which are called by ASP.NET that are having trouble with event
log. Here's the
error:
|
by: frekster |
last post by:
All.
I have a folder/files that I have added asp.net to have read/write/etc.
privlidges via the properties of the folder/files and security tab.
However, when I run my asp.net page, I still get that access is denied.
the path is c:\inetpub\app\bin\folder
the "folder has a ton of files in it and every file in the folder must
be able to be opened and binary read by the asp.net worker process.
| |
by: brm6546545 |
last post by:
I am trying to get some C code to work on .net C++ compiler
int writeDatagram(int sock, char *buf, int length) {
return write(sock, buf, length);
}
void killConnection(int sockd) {
close(sockd);
}
|
by: Ed Sutton |
last post by:
Is there a FileSecurity method that can determine if the current
WindowsIdentity has write access to a file?
I can get the current windows identity and use FileSecurity to return
the AuthorizationRuleCollection and then search for the
FileSystemAccessRule's that apply to my identity. The problem is my
current identity may be "MYDOMAIN\JDOE", but I do not know how to
determine if a group this identity belongs, for example "EVERYONE",...
|
by: =?Utf-8?B?Smlt?= |
last post by:
I have code that reads and parses a text file using a schema.ini file. Works
great. When I see the dataGrid it's exactly what I want.
Dim CString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\;" & _
"Extended Properties=""text;HDR=No;"""
Dim TConnect As New System.Data.OleDb.OleDbConnection(CString)
|
by: musosmiffy |
last post by:
Hi,
I have been trying to get the following working for days - I wonder if anyone could help me?
I am trying to list a set of database entries as a newspaper column. I am using classic ASP (not NET). It would seem that things fail when the code gets to if DataArray(i,j) <> "" then
The following code has been working great when connected to an Access database. I wish to swop the database to MySQL. I have proven that I am connecting OK...
|
by: Mark Salsbery [MVP] |
last post by:
"DR" <softwareengineer98037@yahoo.comwrote in message
news:Op0NyzR8IHA.1200@TK2MSFTNGP04.phx.gbl...
If the filestream is opened for write access then it still keeps its write
access.
If you seek to the beginning after writing 8 bytes, and write some more
bytes, you overwrite the original bytes.
I don't believe there's any flush occurring on a seek...whether any buffers
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |