Properties with different scope

  • Thread starter Thread starter George Boyd Ratcliff
  • Start date Start date
G

George Boyd Ratcliff

Hi all,

In VB6 you could declare a property with different scope
between the set & get.

I have a base class where I want properties with Protected
Set and Public Get (ie the class that inherits the base
class can set properties, but the end user of the class
can only read them).

I've read lots about "you can't do this in VB.Net" but
can't imagine that Microsoft would take such a drastic
backwards step (though the loss of edit-and-continue may
prove they will!)

Lots of people have recommended naming the properties
differently, ie Protected Property SetWhatever and Public
Property GetWhatever. But I have lots of properties and
doing this kind of undoes all the benefits of OOP!!

Any suggestions? Thanks in advance. George
 
George Boyd Ratcliff said:
Hi all,

In VB6 you could declare a property with different scope
between the set & get.

I have a base class where I want properties with Protected
Set and Public Get (ie the class that inherits the base
class can set properties, but the end user of the class
can only read them).

Not possible.
I've read lots about "you can't do this in VB.Net" but
can't imagine that Microsoft would take such a drastic
backwards step (though the loss of edit-and-continue may
prove they will!)

But they did.
Lots of people have recommended naming the properties
differently, ie Protected Property SetWhatever and Public
Property GetWhatever. But I have lots of properties and
doing this kind of undoes all the benefits of OOP!!

Any suggestions? Thanks in advance. George

Wait for the next version. AFAIR different scope levels will be
re-introduced.

http://msdn.microsoft.com/vbasic/whidbey
 
George,
I've read lots about "you can't do this in VB.Net" but
can't imagine that Microsoft would take such a drastic
backwards step (though the loss of edit-and-continue may
prove they will!)

It's true. But the feature should be back in the next version of
VB.NET.



Mattias
 
I've heard rumours that this may be brought back in. They're just rumours
though and I don't know for sure.

In the meantime, you can use the following (messy in my opinion) way of
accomplishing this:
----------------------------------------
Private mName As String
Public ReadOnly Property Name() As String
Get
Return mName
End Get
End Property
Protected Sub SetName(Name As String)
mName = Name
End Property
 
Thanks Mattias. I'm amazed that Microsoft have taken two
versions before sorting this. I think I may have found a
workaround at http://addressof.com/blog/posts/177.aspx

Why not have a Public ReadOnly Property and a Protected sub to set the
property? Why is the interface solution any better? The code below seems
more intuitive to me.

'\\\\\\\\\\\\\\\
Private m_prop As Integer

Public ReadOnly Property SomeProp() As Integer
Get
Return m_prop
End Get
End Property

Protected Sub SetSomeProp(value As Integer)
m_prop = value
End Sub

'//////////////
 
Yes

And if this is going on, maybe people are saying in future:

use C++ that has much simpler to understand code than VB

Just a thought,

Cor
 
Back
Top