473,397 Members | 2,028 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,397 software developers and data experts.

Friend and Protected confusion

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.
Nov 1 '07 #1
5 1361
On Nov 1, 12:03 pm, "Jameson" <rtgro...@hotmail.comwrote:
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

Nov 1 '07 #2
Jameson wrote:
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?

--

(O)enone
Nov 1 '07 #3
Make the property Private, and use functions to do the Set and Get?
Public Function Getindex ...

Friend Function SetIndex ...

Guy

"Jameson" wrote:
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.
Nov 2 '07 #4
guy wrote:
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.

--

(O)enone
Nov 2 '07 #5

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 ;)

"(O)enone" <oe****@nowhere.comwrote in message
news:en**************@TK2MSFTNGP02.phx.gbl...
Jameson wrote:
>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?

--

(O)enone

Nov 2 '07 #6

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

Similar topics

15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
8
by: Carlos J. Quintero | last post by:
Hi, As you know the current keywords "protected internal" (C#) or "Protected Friend" (VB.Net) means "Protected Or internal" (C#) or "Protected Or Friend" (VB.Net), that is, the member is...
7
by: Steve | last post by:
I'm just curious, why did give VB.Net a Friend keyword but not C#?
7
by: Jesper | last post by:
I need to grant a class access to protected fields of another class in the way its possible in C++ with the friend keyword. However I would like to keep the class protected towards other class...
5
by: | last post by:
hello there, what is the difference between Shared and Protected Shared? where can I read more about theses kind of variables (or whatever they are....sorry, don't know the word in eng.) ...
8
by: toton | last post by:
Hi, I have a parser interface , which is class IParser{ public: void readHeader(Document& doc)= 0; void readCC(CC& cc) = 0; }; Now, there are 3 kinds of parser which extends (implements)...
3
by: Neal | last post by:
Hi all Why do I get a compiler error with the following code. Friend Class SomeClass End Class Public Class SomePublicClass Protected Friend Function AMethod As SomeClass
12
by: MiG | last post by:
Hello, Consider the following code snippet: class B { protected: B() {} ~B() {} };
3
by: tonvandenheuvel | last post by:
Hi all, the following code does not compile in gcc 4.0.2: 3 class Outer 4 { 5 class B; 6 7 template<typename T>
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.