Answered by:
How to limit access to a containing class?

Question
-
I'm not terribly familiar with protection. Here's an example of what i want to do:
Class Outer Friend Class Inner Private A As Integer Private B As Integer Private C As Long Sub Set_Values(Some_Object As whatever) A = Some_Object.A B = Some_Object.B C = Some_Object.C End Sub Friend Function Get_A() As Integer Return A End Function
Friend Function Get_B() As Integer Return B End Function
Friend Function Get_C() As Long Return C End Function End Class End Class
The idea is to make the variables writeable from Outer, but readable from outside Outer. I did not want to use Property, because all three variables are set from the same object. They are set multiple times. I could probably make Inner itself private, and move the Gets to Outer, but i was wondering if it could be done all in one class. It would also help me understand Access Levels a little better.
Wednesday, August 19, 2020 8:36 PM
Answers
-
In addition to Zhao,
You're not the first one thinking about Access Modifiers in another way that it is meant. Access modifiers are meant to make better programming possible.
It is not about the access of data or something like that, but to make parts of programs invisible for other programmers.
Also try to use the right members of a .Net program. They can have in other program languages different names but they exist from
- Constructors
- Methods (Functions and Sub in VB)
- Properties
The use of properties seems unnecessary but as soon as you start using documentation and by that making your program easier to use for programming, you will recognize the benefits of those.
Success
Cor
- Edited by Cor Ligthert Friday, August 21, 2020 3:31 PM
- Proposed as answer by Xingyu ZhaoMicrosoft contingent staff Monday, August 24, 2020 1:22 AM
- Marked as answer by Brian Tkatch Monday, August 24, 2020 2:59 PM
Friday, August 21, 2020 3:29 PM
All replies
-
Hi Brian Tkatch,
Thank you for posting here.
I'm not sure why you use inner class. You can consider using the following code to do it.
Here's the code of my test:
Module Module1 Sub Main() Dim stu As student = New student() With { .A = 21, .B = 112, .C = 200L } Dim outer As Outer = New Outer(stu) Console.WriteLine($" A: {outer.Get_A()} B: {outer.Get_B()} C: {outer.Get_C()} ") Console.ReadLine() End Sub End Module Class Outer Private A As Integer Private B As Integer Private C As Long Public Sub New(ByVal Some_Object As student) A = Some_Object.A B = Some_Object.B C = Some_Object.C End Sub Public Function Get_A() As Integer Return A End Function Public Function Get_B() As Integer Return B End Function Public Function Get_C() As Long Return C End Function End Class Class student Public Property A As Integer Public Property B As Integer Public Property C As Long End Class
Result:
Hope it could be helpful.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Thursday, August 20, 2020 8:20 AM -
It's just what i was trying at the time. So, i started playing with protection levels, because i do not understand them well. Then i figured i'd ask if it was possible.
- Edited by Brian Tkatch Thursday, August 20, 2020 5:48 PM
Thursday, August 20, 2020 5:48 PM -
They are really access levels. Take a look at this.
Multics - An OS ahead of its time.
"Those who use Application.DoEvents have no idea what it does
and those who know what it does never use it." former MSDN User JohnWein
Thursday, August 20, 2020 6:57 PM -
Reading them and deeply understanding them, i find, are two completely different things. :)Thursday, August 20, 2020 7:22 PM
-
Hi Brian Tkatch,
The following references may help you understand access modifiers in VB.NET.
- Understanding Access Modifiers In VB.NET
- Visual Basic (VB) Access Modifiers (Public, Private, Protected, Friend)
Hope them could be helpful.
Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
Best Regards,
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Friday, August 21, 2020 1:48 AM -
Thank you. I am looking to learn through my own examples rather than study, as they are not really important to me right now, so i am not that interested. When a situation comes up like the one above, i get the interest to learn it, and that is why i asked for that specific example. The one thing i have learnt in this thread is that specific case above cannot be done. :)
I may come back to those links next time such a situation arises. Thank you for listing them.
- Edited by Brian Tkatch Friday, August 21, 2020 3:26 PM
Friday, August 21, 2020 3:25 PM -
In addition to Zhao,
You're not the first one thinking about Access Modifiers in another way that it is meant. Access modifiers are meant to make better programming possible.
It is not about the access of data or something like that, but to make parts of programs invisible for other programmers.
Also try to use the right members of a .Net program. They can have in other program languages different names but they exist from
- Constructors
- Methods (Functions and Sub in VB)
- Properties
The use of properties seems unnecessary but as soon as you start using documentation and by that making your program easier to use for programming, you will recognize the benefits of those.
Success
Cor
- Edited by Cor Ligthert Friday, August 21, 2020 3:31 PM
- Proposed as answer by Xingyu ZhaoMicrosoft contingent staff Monday, August 24, 2020 1:22 AM
- Marked as answer by Brian Tkatch Monday, August 24, 2020 2:59 PM
Friday, August 21, 2020 3:29 PM -
Good points.
I always wonder when to use Properties. In most cases, i do not need to hide the variables, so i just friend the variables and move on. I tried them in the case above so i could have separate modifiers for the Get and Set, but, there is no access modifier to do what i want, in the way that i wanted. Maybe next time. :)
Friday, August 21, 2020 3:39 PM -
Hi Brian Tkatch,
Hope following references can help you understand when to use nested classes.
Code in the first reference is C#, and you can convert them to VB.NET.
Best Regards,
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Monday, August 24, 2020 7:53 AM