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

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 1161
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.google.c om...
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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"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.com>
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.google. 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*******@discussions.microsoft.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.google. 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*******@discussions.microsoft.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.google .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
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...
4
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? ...
3
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...
3
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:...
2
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...
2
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
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...
9
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...
2
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"...
2
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.