Friend and Protected confusion

  • Thread starter Thread starter Jameson
  • Start date Start date
J

Jameson

Hi,

I have a property defined something like:


Public Property Index As Integer

Get
Return m_Index
End Get

Set (value As Integer )
m_Index = Value
End Set

End Property

I want all classes to be able to access the "Get" (hence public) but only a
few classes to be able to access the "Set". How can I specify which classes
can access Set in this context?


Thanks.
 
Hi,

I have a property defined something like:

Public Property Index As Integer

Get
Return m_Index
End Get

Set (value As Integer )
m_Index = Value
End Set

End Property

I want all classes to be able to access the "Get" (hence public) but only a
few classes to be able to access the "Set". How can I specify which classes
can access Set in this context?

Thanks.

I don't believe you can specify what classes can access a property,
but you can apply a more-restrictive access modifier to the Set
statement starting with .Net 2.0.

/////////////////////
Public Property Foo() As String
Get
Return _Foo
End Get
Private Set(ByVal value As String)
_Foo = value
End Set
End Property
/////////////////////

Thanks,

Seth Rowe
 
Jameson said:
I have a property defined something like:

Public Property Index As Integer

Get
Return m_Index
End Get

Set (value As Integer )
m_Index = Value
End Set

End Property

I want all classes to be able to access the "Get" (hence public) but
only a few classes to be able to access the "Set". How can I specify
which classes can access Set in this context?

The scope options work like this:


- If you change Set to be Private, only other code within the same class
will be able to access it. Other classes, inside or outside of your
assembly, will not be able to use Set.

- If you change Set to be Friend, all classes within your assembly will be
able to access the Set. Classes outside of your assembly will not be able to
access it.

- If you change Set to be Protected, only your class and other classes that
inherit from your class (even if they themselves are in different
assemblies) will be able to access it. Other classes within your assembly
will not be able to access it, nor will classes from other assemblies
(unless as already noted they inherit from your class).

- If you change Set to be Protected Friend, the same rules apply as
Protected, except that all classes within your assembly will be able to
access it too, regardless of whether or not they inherit from your class.


Does that help you to decide?
 
Make the property Private, and use functions to do the Set and Get?
Public Function Getindex ...

Friend Function SetIndex ...

Guy
 
guy said:
Make the property Private, and use functions to do the Set and Get?
Public Function Getindex ...

Friend Function SetIndex ...

In VB2003 you would have had to do that if you wanted different scope for
the Set and Get parts of the property.

In VB2005 however, the Set and Get parts of the property may have different
scope. So you can quite happily use:

\\\
Public Property Index As Integer
Get
Return m_Index
End Get
Friend Set (value As Integer)
m_Index = value
End Set
End Property
///

This is much more simple and concise, and leads to a neater and more
consistent API.
 
I think so. I should make it private outside the assembly, although my
intention was to make it clear to the mainatiner of the assembly in future
that only class X should be able to set the given property (if you see what
I mean), because it wouldn't make sense for class Y or Z to do so. I
suppose protecting it and then adding comment will do ;)
 
Back
Top