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

Property question

Hi All,

Is it possible to make a property in C# in which 'set' is private (or
protected or internal) but 'get' is public?

-
Aamir
Nov 17 '05 #1
6 1112
Yes, do not implement the set method, and then add an internal property with
the set implemented. The language itself does not have special features for
what you want, sometimes you just have to be creative.

JIM

"Aamir Mahmood" <a@b.c> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
Hi All,

Is it possible to make a property in C# in which 'set' is private (or
protected or internal) but 'get' is public?

-
Aamir

Nov 17 '05 #2

"Aamir Mahmood" <a@b.c> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
Hi All,

Is it possible to make a property in C# in which 'set' is private (or
protected or internal) but 'get' is public?

-
Aamir

I may be missing the point, but what purpose would a private set method
serve? Private members are directly accessible to code within a class, so no
set method is needed from within.
I think all you'd need is a get method, effectively making the property
"read only" as seen from outside the class.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 17 '05 #3
Not in VC# 1.0, but it will be available when C# 2.0 is released later
this year.

"Aamir Mahmood" <a@b.c> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
Is it possible to make a property in C# in which 'set' is private (or
protected or internal) but 'get' is public?

Nov 17 '05 #4
Using a private set lets you validate (or trace, log, persist) easily
(assuming you always use set instead of assigning to the private
variable directly).
I may be missing the point, but what purpose would a private set method
serve? Private members are directly accessible to code within a class, so no
set method is needed from within.
I think all you'd need is a get method, effectively making the property
"read only" as seen from outside the class.

Nov 17 '05 #5
Sometimes a property is helpful, if only as syntactic sugar, within the
class too. Just because you can access the internal state directly doesn't
mean that it's always the nicest way. For instance:

class JaggedArray<T> {
T[][] manyTs;
foo(int[] cols, int rows) {
manyTs = new T[rows];
int colsIndex = 0;
foreach (T[] S in manyTs) {
S = new T[cols[colsIndex++]];
}
}
public T this[int col, int row] {
get {
if(row < manyTs.Count &&
col < manyTs[row].Count) {
return manyTs[row][col];
} else {
throw System.IndexOutOfRangeException;
}
}
set {
if(row < manyTs.Count &&
col < manyTs[row].Count) {
manyTs[row][col] = value;
} else {
throw System.IndexOutOfRangeException;
}
}
}
}

Note that the get and set accessors are somewhat complex. Even within the
class, say in an iterator or something, you might well like to be able to
use the 'this[col,row]' notation to access an individual element in the
jagged array rather than repeating the index-checking logic every time you
needed to access an element. If, for some reason, you needed your
JaggedArray class to be read only then, as the original poster indicated,
you might well want different protection levels for 'get' and 'set'.

--
Anything I post is solely my opinion, and is not necessarily representative
of my employer's positons.
My answers are not always correct, but I do my best to be accurate and
helpful.
"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:ev**************@TK2MSFTNGP14.phx.gbl...

"Aamir Mahmood" <a@b.c> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
Hi All,

Is it possible to make a property in C# in which 'set' is private (or
protected or internal) but 'get' is public?

-
Aamir

I may be missing the point, but what purpose would a private set method
serve? Private members are directly accessible to code within a class, so
no set method is needed from within.
I think all you'd need is a get method, effectively making the property
"read only" as seen from outside the class.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Nov 17 '05 #6

"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:ev**************@TK2MSFTNGP14.phx.gbl...

"Aamir Mahmood" <a@b.c> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
Hi All,

I may be missing the point, but what purpose would a private set method
serve? Private members are directly accessible to code within a class, so
no set method is needed from within.
I think all you'd need is a get method, effectively making the property
"read only" as seen from outside the class.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Thanks, Fred and Jason, for opening my eyes to possibilities I'd not
considered.
These groups are a great learning place :)
Nov 17 '05 #7

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

Similar topics

2
by: Edward Diener | last post by:
How does one specify in a component that a property is a pointer to another component ? How is this different from a property that is actually an embedded component ? Finally how is one notified in...
3
by: Mark Jones | last post by:
I am quite new to ASP and .Net development and I am building a web-based multiple choice exam application. The web page displays the questions using a Repeater control and the answers are nested...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
0
ADezii
by: ADezii | last post by:
The motivation for this Tip was a question asked by one of our Resident Experts, FishVal. The question was: How to Declare Default Method/Property in a Class Module? My response to the question was...
8
by: Hussein B | last post by:
Hey, I noted that Python encourage the usage of: -- obj.prop = data x = obj.prop -- to set/get an object's property value. What if I want to run some logic upon setting/getting a property?...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
tracyyun
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...

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.