locked
How to limit access to a containing class? RRS feed

  • Pytanie

  • 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.


    środa, 19 sierpnia 2020 20:36

Odpowiedzi

  • 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 

    1. Constructors
    2. Methods (Functions and Sub in VB)
    3. 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



    piątek, 21 sierpnia 2020 15:29

Wszystkie odpowiedzi

  • 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.

    czwartek, 20 sierpnia 2020 08:20
  • 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.
    • Zmodyfikowany przez Brian Tkatch czwartek, 20 sierpnia 2020 17:48
    czwartek, 20 sierpnia 2020 17:48
  • They are really access levels.  Take a look at this.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/access-levels


    Search Documentation

    SerialPort Info

    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

    czwartek, 20 sierpnia 2020 18:57
  • Reading them and deeply understanding them, i find, are two completely different things. :)
    czwartek, 20 sierpnia 2020 19:22
  • Hi Brian Tkatch,

    The following references may help you understand access modifiers in VB.NET.

    1. Understanding Access Modifiers In VB.NET
    2. 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,

    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.

    piątek, 21 sierpnia 2020 01:48
  • 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.


    • Zmodyfikowany przez Brian Tkatch piątek, 21 sierpnia 2020 15:26
    piątek, 21 sierpnia 2020 15:25
  • 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 

    1. Constructors
    2. Methods (Functions and Sub in VB)
    3. 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



    piątek, 21 sierpnia 2020 15:29
  • 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. :)

    piątek, 21 sierpnia 2020 15:39
  • Hi Brian Tkatch,

    Hope following references can help you understand when to use nested classes. 

    1. When and why to use Nested Classes?
    2. Private nested classes

    Code in the first reference is C#, and you can convert them to VB.NET.

    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.

    poniedziałek, 24 sierpnia 2020 07:53