Visual Basic 2012 Voorbeelden
   

visual basic 2012 broncode voorbeelden

Blijf op de hoogte van de recente aanpassingen op vbvoorbeelden!

Microsoft Visual Studio 2012Microsoft Developers Network - Visual BasicMicrosoft .NET Framework

18.3. Default Access Modifiers

Print Email Deel op Twitter Deel op Facebook

Dit artikel is gepubliceerd op maandag 15 oktober 2012 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.

18.3.1. Type definities in een Namespace

Access modifiers van type definities (bijvoorbeeld klassen) gedefinieerd in een namespace (dus ook in de root van een project) zijn by default Friend, wat maakt dat deze klassen slechts binnen het project (de "assembly") beschikbaar zijn.

De enige ander mogelijkheid (buiten Friend) is Public, door deze te gebruiken zijn de klassen ook buiten de assembly/project beschikbaar.

Klassen kunnen dus niet ingekapseld (Protected of Private) worden in een namespace.
Visual Basic 2012 Broncode - Codevoorbeeld 436
Namespace Namespace1
    Class Class1
        ' Friend (default), available within the defining assembly.
    End Class
    Public Class Class2
        ' Public, available for anyone with access to the defining assembly.
    End Class
    'Private Class Class3
    '    ' Private, impossible to encapsulate a type in a namespace.
    'End Class
    'Proteced Class Class4
    '    ' Private, impossible to encapsulate a type in a namespace.
    'End Class
    'Protected Friend Class Class5
    '    ' Private, impossible to encapsulate a type in a namespace.
    'End Class
    'Shared Class Class6
    '    ' Shared, only possible for members of a class.
    'End Class
End Namespace

18.3.2. Members van een Klasse

De accessmodifiers van members gedefinieerd in klassen kunnen zijn Public, Private, Protected, Friend of Protected Friend (allen eventueel gecombineerd met accessmodifier Shared).

Access modifier Protected Friend is de doorsnede van Protected en Friend.  Deze members zullen dus enkel beschikbaar zijn voor afgeleiden binnen de assembly/project.

Access modifier Public is de default voor implementatie members (Subs, Functions en Propertys).

Velden hebben geen default accessmodifier, Dim betekend hetzelfde als Private, Private is natuurlijk beter leesbaar.
Visual Basic 2012 Broncode - Codevoorbeeld 437
Class Class1
    ' Private (Shared), only available within the defining class.
    Dim member1 As String = "member1"
    Shared member2 As String = "member2"
    Private member3 As String = "member3"
    Private Shared member4 As String = "member4"
    '
    ' Protected (Shared), only available within the defining class and
    ' derived classes.
    Protected member5 As String = "member5"
    Protected Shared member6 As String = "member6"
    '
    ' Protected Friend (Shared), only available within the defining class
    ' and derived classes in the defining assembly.
    Protected Friend member7 As String = "member7"
    Protected Friend Shared member8 As String = "member8"
    '
    ' Public (Shared), available for anyone with access to the defining
    ' class.
    Public Member9 As String = "Member9"
    Public Shared Member10 As String = "Member10"
    '
    ' Private (Shared), only available within the defining class.
    Private Function member11()
        member11 = "member11"
    End Function
    Private Shared Function member12()
        member12 = "member12"
    End Function
    '
    ' Protected (Shared), only available within the defining class and
    ' derived classes.
    Protected Function member13()
        member13 = "member13"
    End Function
    Protected Shared Function member14()
        member14 = "member14"
    End Function
    '
    ' Protected Friend (Shared), only available within the defining class
    ' and derived classes in the defining assembly.
    Protected Friend Function member15()
        member15 = "member15"
    End Function
    Protected Friend Shared Function member16()
        member16 = "member16"
    End Function
    '
    ' Public (default for implementation members) (Shared), available for
    ' anyone with access to this class.
    Function Member17()
        Member17 = "Member17"
    End Function
    Shared Function Member18()
        Member18 = "Member18"
    End Function
    Public Function Member19()
        Member19 = "Member19"
    End Function
    Public Shared Function Member20()
        Member20 = "Member20"
    End Function
    '
    Public Sub Test1()
        ' can access all members (even encapsulated members)
        '
        Console.WriteLine(Me.member1)
        Console.WriteLine(Class1.member2)
        Console.WriteLine(Me.member3)
        Console.WriteLine(Class1.member4)
        Console.WriteLine(Me.member5)
        Console.WriteLine(Class1.member6)
        Console.WriteLine(Me.member7)
        Console.WriteLine(Class1.member8)
        Console.WriteLine(Me.Member9)
        Console.WriteLine(Class1.Member10)
        Console.WriteLine(Me.member11)
        Console.WriteLine(Class1.member12)
        Console.WriteLine(Me.member13)
        Console.WriteLine(Class1.member14)
        Console.WriteLine(Me.member15)
        Console.WriteLine(Class1.member16)
        Console.WriteLine(Me.Member17)
        Console.WriteLine(Class1.Member18)
        Console.WriteLine(Me.Member19)
        Console.WriteLine(Class1.Member20)
    End Sub
End Class
Class Class2 : Inherits Class1
    Public Sub Test2()
        ' can access all members of Class1 (except Private members)
        '
        'Console.WriteLine(Me.member1)      ' impossible, encapsulated in Class1
        'Console.WriteLine(Class1.member2)  ' impossible, encapsulated in Class1
        'Console.WriteLine(Me.member3)      ' impossible, encapsulated in Class1
        'Console.WriteLine(Class1.member4)  ' impossible, encapsulated in Class1
        'Console.WriteLine(Me.member5)      ' impossible, encapsulated in Class1
        Console.WriteLine(Class1.member6)
        Console.WriteLine(Me.member7)
        Console.WriteLine(Class1.member8)
        Console.WriteLine(Me.Member9)
        Console.WriteLine(Class1.Member10)
        'Console.WriteLine(Me.member11)     ' impossible, encapsulated in Class1
        'Console.WriteLine(Class1.member12) ' impossible, encapsulated in Class1
        'Console.WriteLine(Me.member13)     ' impossible, encapsulated in Class1
        Console.WriteLine(Class1.member14)
        Console.WriteLine(Me.member15)
        Console.WriteLine(Class1.member16)
        Console.WriteLine(Me.Member17)
        Console.WriteLine(Class1.Member18)
        Console.WriteLine(Me.Member19)
        Console.WriteLine(Class1.Member20)
    End Sub
End Class
Class Client
    Public Shared Sub Main()
        Console.WriteLine("Members accessed thru an object of type Class1 :")
        Dim object1 As Class1 = New Class1
        'Console.WriteLine(object1.member1)  ' impossible, encapsulated in Class1
        'Console.WriteLine(Class1.member2)   ' impossible, encapsulated in Class1
        'Console.WriteLine(object1.member3)  ' impossible, encapsulated in Class1
        'Console.WriteLine(Class1.member4)   ' impossible, encapsulated in Class1
        'Console.WriteLine(object1.member5)  ' impossible, encapsulated in Class1
        'Console.WriteLine(Class1.member6)   ' impossible, encapsulated in Class1
        Console.WriteLine(object1.member7)
        Console.WriteLine(Class1.member8)
        Console.WriteLine(object1.Member9)
        Console.WriteLine(Class1.Member10)
        'Console.WriteLine(object1.member11) ' impossible, encapsulated in Class1
        'Console.WriteLine(Class1.member12)  ' impossible, encapsulated in Class1
        'Console.WriteLine(object1.member13) ' impossible, encapsulated in Class1
        'Console.WriteLine(Class1.member14)  ' impossible, encapsulated in Class1
        Console.WriteLine(object1.member15)
        Console.WriteLine(Class1.member16)
        Console.WriteLine(object1.Member17)
        Console.WriteLine(Class1.Member18)
        Console.WriteLine(object1.Member19)
        Console.WriteLine(Class1.Member20)
        Console.WriteLine()
        '
        Console.WriteLine("Members accessed internally " & _
                          "in an object of type Class1 :")
        object1.Test1()
        Console.WriteLine()
        '
        Console.WriteLine("Members accessed internally " & _
                          "in an object of type Class2 :")
        Dim object2 As Class2 = New Class2
        object2.Test2()
        Console.WriteLine()
        '
        Console.ReadLine()
    End Sub
End Class
Console Application Output
Members accessed thru an object of type Class1 :
member7
member8
Member9
Member10
member15
member16
Member17
Member18
Member19
Member20

Members accessed internally in an object of type Class1 :
member1
member2
member3
member4
member5
member6
member7
member8
Member9
Member10
member11
member12
member13
member14
member15
member16
Member17
Member18
Member19
Member20

Members accessed internally in an object of type Class2 :
member6
member7
member8
Member9
Member10
member14
member15
member16
Member17
Member18
Member19
Member20

Dit artikel is gepubliceerd op maandag 15 oktober 2012 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.