473,839 Members | 1,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How 'read-only' are read-only properties?

Hi,

Suppose I have a class definition like this:
public class CBase
{
public string m_Name = "asd";

public string Name
{
get
{
return m_Name;
}
}
}

Even though 'Name' is a read-ionly property, I can do this:
CBase c1 = new CBase();
c1.Name.Remove( 0, 2);

So what's the point is declaring a get-only property?

Thanks,
Rakesh
Nov 15 '05 #1
8 1173
Hi Rakesh,

You can't change the string instance.
You can modify its content, yes, since string is treated as a class and
readonly properties (as you've stated) don't prevent changing instance
fields.

In the case of simple types like int, bool, etc. it works as one would
thought :)

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Rakesh" <ra************ *@hotmail.com> wrote in message
news:d6******** *************** ***@posting.goo gle.com...
Hi,

Suppose I have a class definition like this:
public class CBase
{
public string m_Name = "asd";

public string Name
{
get
{
return m_Name;
}
}
}

Even though 'Name' is a read-ionly property, I can do this:
CBase c1 = new CBase();
c1.Name.Remove( 0, 2);

So what's the point is declaring a get-only property?

Thanks,
Rakesh

Nov 15 '05 #2
It's read only.

cl.Name.Remove( 0,2) doesn't change m_Name in any way, it just returns a
new string.
Though you should make m_Name private or anyone could bypass Name and
handle m_Name directly instead.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3
Hi,
Read-only in this context means you can't assign to this
property (as no setter method is provided). It doesn't
mean that the returned reference (by the getter) can't be
changed. Doesn't work for value classes of course.
BTW, the Remove method actually creates a new instance of
type String reflecting the change, while the original
string remains unaffected. This happens to be so for all
the other methods, which make it immutable.
Regard,
BV
-----Original Message-----
Hi,

Suppose I have a class definition like this:
public class CBase
{
public string m_Name = "asd";

public string Name
{
get
{
return m_Name;
}
}
}

Even though 'Name' is a read-ionly property, I can do this: CBase c1 = new CBase();
c1.Name.Remove( 0, 2);

So what's the point is declaring a get-only property?

Thanks,
Rakesh
.

Nov 15 '05 #4
<"Miha Markic" <miha at rthand com>> wrote:
You can't change the string instance.
You can modify its content, yes, since string is treated as a class and
readonly properties (as you've stated) don't prevent changing instance
fields.


Well, you *would* be able to, if string weren't immutable.

If Name returned a StringBuilder rather than a string, that would
certainly allow modification.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Ouch, right.
Typed too fast, too early in the morning :)

--
Miha Markic - DXSquad/RightHand .NET consulting & software development
miha at rthand com
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<"Miha Markic" <miha at rthand com>> wrote:
You can't change the string instance.
You can modify its content, yes, since string is treated as a class and
readonly properties (as you've stated) don't prevent changing instance
fields.


Well, you *would* be able to, if string weren't immutable.

If Name returned a StringBuilder rather than a string, that would
certainly allow modification.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
Hi Miha,

So do u mean that we can change the value of read-only
properties exposing objects(not primitive types), but
cannot assign values to them?

Rakesh
-----Original Message-----
Hi Rakesh,

You can't change the string instance.
You can modify its content, yes, since string is treated as a class andreadonly properties (as you've stated) don't prevent changing instancefields.

In the case of simple types like int, bool, etc. it works as one wouldthought :)

--
Miha Markic - RightHand .NET consulting & software developmentmiha at rthand com

"Rakesh" <ra************ *@hotmail.com> wrote in message
news:d6******* *************** ****@posting.go ogle.com...
Hi,

Suppose I have a class definition like this:
public class CBase
{
public string m_Name = "asd";

public string Name
{
get
{
return m_Name;
}
}
}

Even though 'Name' is a read-ionly property, I can do this: CBase c1 = new CBase();
c1.Name.Remove( 0, 2);

So what's the point is declaring a get-only property?

Thanks,
Rakesh

.

Nov 15 '05 #7
Hi rakesh,

An example:

public class Tubo
{
public int Value = 0;
}

public class Tubular
{
private Tubo tubo = new Tubo();

public Tubo Tubo
{
get { return tubo; }
}
}

Tubular t = new Tubular();

you are allowed to:
t.Tubo.Value = 5; // changing the internal value of Tubo

while you won't be able to:
t.Tubo = null;
or
t.Tubo = new Tubo();
since you are trying to write the new t.Tubo property value.

HTH,

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"rakesh" <an*******@disc ussions.microso ft.com> wrote in message
news:08******** *************** *****@phx.gbl.. .
Hi Miha,

So do u mean that we can change the value of read-only
properties exposing objects(not primitive types), but
cannot assign values to them?

Rakesh
-----Original Message-----
Hi Rakesh,

You can't change the string instance.
You can modify its content, yes, since string is treated

as a class and
readonly properties (as you've stated) don't prevent

changing instance
fields.

In the case of simple types like int, bool, etc. it

works as one would
thought :)

--
Miha Markic - RightHand .NET consulting & software

development
miha at rthand com

"Rakesh" <ra************ *@hotmail.com> wrote in message
news:d6******* *************** ****@posting.go ogle.com...
Hi,

Suppose I have a class definition like this:
public class CBase
{
public string m_Name = "asd";

public string Name
{
get
{
return m_Name;
}
}
}

Even though 'Name' is a read-ionly property, I can do this: CBase c1 = new CBase();
c1.Name.Remove( 0, 2);

So what's the point is declaring a get-only property?

Thanks,
Rakesh

.

Nov 15 '05 #8
Even though, of course 'read-only' is meant to mean exactly that. One can work-around this fact by eithe
locking the member or class

Thought it would help
Chop

----- Miha Markic wrote: ----

Hi rakesh

An example

public class Tub

public int Value = 0
public class Tubula

private Tubo tubo = new Tubo()

public Tubo Tub

get { return tubo;

Tubular t = new Tubular()

you are allowed to
t.Tubo.Value = 5; // changing the internal value of Tub

while you won't be able to
t.Tubo = null
o
t.Tubo = new Tubo()
since you are trying to write the new t.Tubo property value

HTH

--
Miha Markic - RightHand .NET consulting & software developmen
miha at rthand co

"rakesh" <an*******@disc ussions.microso ft.com> wrote in messag
news:08******** *************** *****@phx.gbl..
Hi Miha
So do u mean that we can change the value of read-onl

properties exposing objects(not primitive types), bu
cannot assign values to them
Rakes
-----Original Message----

Hi Rakesh
You can't change the string instance

You can modify its content, yes, since string is treate

as a class an
readonly properties (as you've stated) don't preven

changing instanc
fields
In the case of simple types like int, bool, etc. i works as one woul
thought :
--

Miha Markic - RightHand .NET consulting & softwar

developmen
miha at rthand co
"Rakesh" <ra************ *@hotmail.com> wrote in messag

news:d6****** *************** *****@posting.g oogle.com..
Hi
>> Suppose I have a class definition like this
public class CBas

public string m_Name = "asd"
>> public string Nam

ge

return m_Name

>> Even though 'Name' is a read-ionly property, I can d

this CBase c1 = new CBase()
c1.Name.Remove( 0, 2)
>> So what's the point is declaring a get-only property
>> Thanks
Rakes
>>

Nov 15 '05 #9

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

Similar topics

2
7227
by: Fred K. | last post by:
Hi, I have form text fields that have the attribute readonly. I wish to use a javascript (invoked by a button on my page) to erase the values and set the readonly attribute to false. How do I do this? Here's what I have that is not working with respect to setting readonly to false: function nsfields ( bDefaults ) {
4
11316
by: harry | last post by:
<input type="text" name="actreqto" maxlength="20" value="" onkeypress="changedDetails();" onchange="changedDetails();" readonly="readonly"> Any ideas why doesn't this work? alert(top.document.forms.actreqto.readonly); displays 'undefined'
3
25322
by: Matt | last post by:
I want to know if readOnly attribute doesn't work for drop down list? If I try disabled attribute, it works fine for drop down list. When I try text box, it works fine for both disabled and readOnly attribute. For example, #1 will work, but #2 doesn't work 1) <SELECT name="streetDirection" class="FormInput" DISABLED> In JavaScript, I have InputForm.streetDirection.disabled = false; 2) <SELECT name="streetDirection" class="FormInput"...
3
7130
by: Michael SL | last post by:
I have a text area in which I have a client side javascript to process a "onclick". Because it is client side, I used a HtmlTextArea <TextArea id="Summary" onmouseup="SumMouseUp()" style="WIDTH: 90%" name="Summary" rows="25" runat="server" / Depending on the circumstance the user can sometimes edit the contents and at other time they cannot. There is a readonly parameter I can use at definition time <TextArea id="Summary"...
2
2201
by: Xarky | last post by:
Hi, I have a checkboxlist and would like to set it as readonly. The properties that I found for it is the Enabled(true, false). Now when I am setting it to false, it is becoming as readonly, but is changing its appearance and does not seems to be so presentable (color becoming greyish, text and check boxes are not clear). I tried to change the fore-color, but in disabled state, it has no effect. Can someone tell me how I can get...
2
5249
by: Daren Hawes | last post by:
Hi I need to add an attribute to a Textbox to make it read only. To add a CSS I use DeptDate.Attributes("Class") = "textInput" That adds 'Class="textinput"', but the readonly is like..
4
2511
by: Doug Bell | last post by:
Hi, I needed to make a TextBox on a DataGrid ReadOnly based on the condition of another cell on its row. I achieved this using a Custom Data Column Class "DacDGTextColLotNo" but I can't get the TextBox to display its value when you leave the TextBox. I figure that I need to use the SetColumnValueAtRow Method but I am struggling to understand how.
9
2747
by: ShaneFowlkes | last post by:
Hey guys... I have a form that asks for several dates. I tried using asp calendar controls to set values of the date textboxes which were readonly. I found this annoying since each time I advanced a month and/or selected any date, it posted back and refreshed the page. So, instead, I resorted to an old javascript function I use a lot. The javascript creates a pop up (html) window and sets the value of the textbox when the user picks a...
2
5452
by: J. McConnell | last post by:
I am having trouble with the following code: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Test</title> </head> <body>
2
1265
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, I think that auto-implemented properties are a wonderful new c# 3.0 feature. However, anyone who knows how I can set a default value (initial value) for the property like in the bad old days: int foo = 5; public int Foo {get ... yada yada}
0
9697
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
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, 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...
0
10586
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 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...
1
10648
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9426
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, 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...
1
7828
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 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...
0
7017
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();...
0
5682
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...
0
5866
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.