Connecting Tech Pros Worldwide Forums | Help | Site Map

Property question

Aamir Mahmood
Guest
 
Posts: n/a
#1: Nov 17 '05
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



James
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Property question


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:ux4Xp3uLFHA.2988@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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
>[/color]


Peter van der Goes
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Property question



"Aamir Mahmood" <a@b.c> wrote in message
news:ux4Xp3uLFHA.2988@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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
>[/color]
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.


James Curran
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Property question


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:ux4Xp3uLFHA.2988@TK2MSFTNGP14.phx.gbl...[color=blue]
> Is it possible to make a property in C# in which 'set' is private (or
> protected or internal) but 'get' is public?[/color]


fred christianson
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Property question


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).
[color=blue]
> 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.
>[/color]
Jason Black [MSFT]
Guest
 
Posts: n/a
#6: Nov 17 '05

re: Property question


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_vandergoes@toadstool.u> wrote in message
news:evZnqQvLFHA.1472@TK2MSFTNGP14.phx.gbl...[color=blue]
>
> "Aamir Mahmood" <a@b.c> wrote in message
> news:ux4Xp3uLFHA.2988@TK2MSFTNGP14.phx.gbl...[color=green]
>> 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
>>[/color]
> 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.
>[/color]


Peter van der Goes
Guest
 
Posts: n/a
#7: Nov 17 '05

re: Property question



"Peter van der Goes" <p_vandergoes@toadstool.u> wrote in message
news:evZnqQvLFHA.1472@TK2MSFTNGP14.phx.gbl...[color=blue]
>
> "Aamir Mahmood" <a@b.c> wrote in message
> news:ux4Xp3uLFHA.2988@TK2MSFTNGP14.phx.gbl...[color=green]
>> Hi All,
>>[/color]
> 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.
>[/color]
Thanks, Fred and Jason, for opening my eyes to possibilities I'd not
considered.
These groups are a great learning place :)


Closed Thread