Accessibility
Whenever you look at introducing new syntax for types, you must also look at the rules that govern
accessibility for these new types. Each time you declare a constructed type, the accessibility of that type's
parameters must be taken into consideration. Here's a basic example that highlights how accessibility
can influence a constructed type:
[VB code]
Public Class MyType(Of T)
End Class
Public Class AccessTest
Private Class PrivateClass
End Class
Public Function GetMyType() As MyType(Of PrivateClass)
End Function
End Class
[C# code]
public class MyType<T> {}
public class AccessTest {
private class PrivateClass {}
public MyType<PrivateClass> getMyType() { }
}
This example creates a closed class,
AccessTest
, which contains a nested class (
PrivateClass
) as well
as a public method. You'll notice that this method actually returns a constructed type,
MyType
, which
was constructed using
PrivateClass
as a type argument.
The problem with this implementation is that
PrivateClass
, which has private accessibility, is being
used in the construction of the publicly accessible return type of the method
getMyType()
. As you
might suspect, the compiler catches and throws an error when you attempt to compile this example.
This same brand of error would also apply in situations where you attempt to use a protected type as a
parameter to a publicly accessible constructed type.
The rule of thumb here is that the accessibility of any class's constructed types is constrained by the
accessibility of the type arguments passed to that constructed type. Let's look at another example:
[VB code]
Public Class MyType(Of T)
End Class
Public Class AccessTest
Public class PublicClass
End Class
Protected class ProtectedClass
End Class
Private class PrivateClass
End Class
Public Function Foo1() As MyType(Of ProtectedClass)
70
Chapter 4
07_559885 ch04.qxd 9/8/05 11:01 PM Page 70