Why can't a writeonly property overload a readonly property?

C

Codemonkey

Just a silly question, but why can't you overload a writeonly property with
a readonly one? Surely the compiler can tell the difference of which one to
call at compile time, depending on if it is an assignment or a get?

I know there isn't much point to this anyway, but until Properties with
different scope for Get and Set methods are brought back (as asked by George
in a previous post about "properties with different scope", it would have
been nice to do the following:

--------------------------

Private mName As String
Public ReadOnly Property Name() As String
Get
Return mName
End Get
End Property
Protected Friend WriteOnly Property Name() As String
Set(ByVal Value As String)
mName = Value
End Set
End Property

--------------------

Saying that, it would be nicer to do this (as George and numerous others
mentioned):

-----------------------------
Private mName As String
Public Property Name() As String
Public Get
Return mName
End Get
Protected Friend Set(ByVal Value As String)
mName = Value
End Set
End Property
----------------------------
 
A

Armin Zingler

Codemonkey said:
Just a silly question, but why can't you overload a writeonly
property with a readonly one? Surely the compiler can tell the
difference of which one to call at compile time, depending on if it
is an assignment or a get?

I know there isn't much point to this anyway, but until Properties
with different scope for Get and Set methods are brought back (as
asked by George in a previous post about "properties with different
scope", it would have been nice to do the following:

It is a contradiction that a property is readonly *and* writeonly. If you
can only read, you can not write or even write only.
 
B

Bill McCarthy

Hi,

At the PDC they showed the new syntax:

Private mName As String
Public Property Name() As String
Get
Return mName
End Get
Protected Friend Set(ByVal Value As String)
mName = Value
End Set
End Property
 
C

Cor

Hi Codemonkey,

I sometimes get the idea that people want to use C with VB.
I am thinking than always: "when those things are so important, why don't
those people switch to C?

What I find crazy is that I cannot simple write

Set
mName = Value
End Set

And when I want to use another value

Set (AnotherValue)
mName = AnotherValue
End Set

I write this, to give a contra opinion about this in this newsgroup, not
that I find it really important.

Cor
 
O

One Handed Man [ OHM# ]

I can see how this could be useful actually and its a shame it doesent work.

I can only assume that due to the way the compiler is designed internally
that this would have caused complications for Microsoft which were too much
to overcome. Alternatively, maybe they simply didnt think of it ?

Who knows!, perhaps you should suggest it to them

Regards - OHM#

Just a silly question, but why can't you overload a writeonly
property with a readonly one? Surely the compiler can tell the
difference of which one to call at compile time, depending on if it
is an assignment or a get?

I know there isn't much point to this anyway, but until Properties
with different scope for Get and Set methods are brought back (as
asked by George in a previous post about "properties with different
scope", it would have been nice to do the following:

--------------------------

Private mName As String
Public ReadOnly Property Name() As String
Get
Return mName
End Get
End Property
Protected Friend WriteOnly Property Name() As String
Set(ByVal Value As String)
mName = Value
End Set
End Property

--------------------

Saying that, it would be nicer to do this (as George and numerous
others mentioned):

-----------------------------
Private mName As String
Public Property Name() As String
Public Get
Return mName
End Get
Protected Friend Set(ByVal Value As String)
mName = Value
End Set
End Property
----------------------------

Regards - OHM# (e-mail address removed)
 
C

Codemonkey

Even better. Cheers Bill.


Bill McCarthy said:
Hi,

At the PDC they showed the new syntax:

Private mName As String
Public Property Name() As String
Get
Return mName
End Get
Protected Friend Set(ByVal Value As String)
mName = Value
End Set
End Property
----------------------------

Note: you declare the property with the most permissible modifier, then
apply the more restrictive modifier to the Set or Get, such as Private ot
Protected etc.
 
O

One Handed Man [ OHM# ]

By new you mean Whidbey ?

regards - OHM#

Bill said:
Hi,

At the PDC they showed the new syntax:

Private mName As String
Public Property Name() As String
Get
Return mName
End Get
Protected Friend Set(ByVal Value As String)
mName = Value
End Set
End Property
----------------------------

Note: you declare the property with the most permissible modifier,
then apply the more restrictive modifier to the Set or Get, such as
Private ot Protected etc.

Regards - OHM# (e-mail address removed)
 
C

Codemonkey

Who knows!, perhaps you should suggest it to them

No point. Properties with different scope are gonna be in the next version
as Bill mentioned anyway. Aside from Properties, I can't really see there
being any need for overloading Fields with different scope modifiers.
 
C

Codemonkey

when those things are so important, why don't
those people switch to C?

I have switched to C# in the past for some things, but what's wrong with
trying to improve VB?
What I find crazy is that I cannot simple write

Set
mName = Value
End Set

That doesn't really bother me as the IDE puts in the syntax automatically
when I press return after typing in the first line of the Property. Jeeze,
I'm lazy ;)

Trev.
 
C

Codemonkey

I understand this, I was making the point about using this to get around the
problem of having different scope for proeprty set and property let (Public
Readlonly Get, Protected Writeonly Set etc.)

It's supposedly gonna be included in the next version, so no matter.

Trev.
 
C

Cor

Hi Codemonkey,

I am also a kind of lazy when it is about simple things and I said that it
did not botter me either.But when this becomes complex the IDE cannot do it automaticly anymore for
me I think,

Just a thought

Cor
 
C

Chris Dunaway

Just a silly question, but why can't you overload a writeonly property with
a readonly one? Surely the compiler can tell the difference of which one to

If you wrote a class and set a particular property to ReadOnly because you
wanted to protect the property from unauthorized modification, if a user of
your class can inherit your class and override the protection, that defeats
the purpose of protecting it in the first place.

And what about private fields?

If a user of a class can inherit and override a private field, we may as
well only have public fields and properties and forget protections.

As pointed out by other posters, however, Whidbey has new syntax to allow
separate protections on the Get and Set portions of a property.
 
P

Peter Huang

Hi,
I understand this, I was making the point about using this to get around the
problem of having different scope for proeprty set and property let (Public
Readlonly Get, Protected Writeonly Set etc.)

If this works then the class's inherit class can inherit its Public and
protected property, so the inherit class will use the Mybase.PropertyName
to get the value, that is to say, the inherit class will read and write the
property.

If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
C

Codemonkey

Thanks for the reply, Peter.

I'm not really bothered by this limitation. It was more of a "wondering"
than a question. As others have mentioned, properties with different scope
for the get and set will be in the next version, so I guess I can wait until
then ;)

Thanks again,

Trev.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top