Scoping within an interface?

S

Sohum

Hi

I'm reasonably new to this group, and to programming in general.

I'd like to know, can you selectively scope within an interface?

For example:

Public Interface MyInterface
Friend Property X as MyX
Public Method Y
End Interface

.... except you're not allowed to put the scoping keywords there.

Currently, I have a workaround as

Public Interface MyInterface
Property X as MyX
Method Y
End Interface

Public Class MyX
Friend...
Friend...
Friend...
End Class

Public Class ImplementerOfMyInterface
Implements MyInterface
....
Friend Property X as MyX Implements MyInterface.MyX
....
End Class

So, even though the MyX is visible in the public scope, its members are
friend, so outside clients can see that a MyX exists (when accessing through
the interface), but they cannot do anything to it.

My question is, this works in this example where the member to be restricted
is a property whose class members I can control the visibility of. What
happens if I want this interface to be usable everywhere, but
outside-assembly users cannot see a method? or an event? or a class that I
have no control over? Would I have to wrap them in a class? What if I can't
(tightly coupled to class it is in right now)?

So, does a solution exist?

Sohum
 
M

Mattias Sjögren

What happens if I want this interface to be usable everywhere, but
outside-assembly users cannot see a method? or an event?

Put the Friend members in a separate interface. Keep the original
interface public and the new one internal.

Public Interface MyInterface
Public Sub Y()
End Interface

Friend Interface MyInternalInterface
Property X as MyX
End Interface


Mattias
 
B

Branco Medeiros

Sohum wrote:
I'd like to know, can you selectively scope within an interface?
For example:
Public Interface MyInterface
Friend Property X as MyX
Public Method Y
End Interface
... except you're not allowed to put the scoping keywords there.

Yep. Interface members must have Public access.
Currently, I have a workaround as

Public Interface MyInterface
Property X as MyX
Method Y
End Interface

Public Class MyX
Friend...
Friend...
Friend...
End Class

Public Class ImplementerOfMyInterface
Implements MyInterface
...
Friend Property X as MyX Implements MyInterface.MyX
...
End Class

Maybe I didn't understand you right, but if you want to hide members
from an Interface, then maybe the best way is to derive a friend
interface from the public one:

Public Interface A
Sub MethodA
End Interface

Friend Interface B
Inherits A
Property X As MyX
End Interface

Friend Class MyX
Public ....
Public ....
End Class

Public Class MyImplementorOfA
Implements B '<---- allows friends to see beyond A,
' (while non-friends will just see an A)

Friend Sub MyMethod Inplements A.MethodA '<-- because B inherits A
'...
End Sub

Friend Property X Implements B.X '<-- invisible to non-friends
'...
End Property
End Class

HTH.

Regards,

Branco.
 
S

Sohum

First of all, it is the same Sohum (I don't know why OE uses my spymac
address)

Mattias Sjögren wrote:
Put the Friend members in a separate interface. Keep the original
interface public and the new one internal.

Public Interface MyInterface
Public Sub Y()
End Interface

Friend Interface MyInternalInterface
Property X as MyX
End Interface

This doesn't work, because I need to accept an Interface which
encompasses both in a method. I can't exactly write this:
Public Sub AcceptsInterface(A as MyInterface+MyInternalInterface)
now can I?

Branco Medeiros wrote:
Maybe I didn't understand you right, but if you want to hide members
from an Interface, then maybe the best way is to derive a friend
interface from the public one:

Public Interface A
Sub MethodA
End Interface

Friend Interface B
Inherits A
Property X As MyX
End Interface

Friend Class MyX
Public ....
Public ....
End Class

Public Class MyImplementorOfA
Implements B '<---- allows friends to see beyond A,
' (while non-friends will just see an A)

Friend Sub MyMethod Inplements A.MethodA '<-- because B inherits A
'...
End Sub

Friend Property X Implements B.X '<-- invisible to non-friends
'...
End Property
End Class
<snip>
So what happens here when I define a method in another class in this
assembly that needs to accept the interface, and needs to use Property
X? If
Public Sub AcceptsInterface(A as A)
.... = DirectCast(A,B)
then they can inherit A and create other interfaces and pass them to
this function, and the Directcast crashes. I could add error handling,
but I would prefer to not let them inherit in the first place (less
ugly). But I can't mark it NotInheritable because I've already inherit
from it! And I can't write
Public Sub AcceptsInterface(B as B)
because B is friend.
Is there any way to de-uglify the solution?

Sohum
 
B

Branco Medeiros

Sohum wrote:
So what happens here when I define a method in another class in this
assembly that needs to accept the interface, and needs to use Property
X? If
Public Sub AcceptsInterface(A as A)
... = DirectCast(A,B)
then they can inherit A and create other interfaces and pass them to
this function, and the Directcast crashes. I could add error handling,
but I would prefer to not let them inherit in the first place (less
ugly). But I can't mark it NotInheritable because I've already inherit
from it! And I can't write
Public Sub AcceptsInterface(B as B)
because B is friend.
Is there any way to de-uglify the solution?

:)))

Yep: don't DirectCast an A into a B if you don't know where the A is
coming from... :)

Seriously, if AcceptsInterface(A as A) is visible from outside the
scope where you just expect B's instead of A's, then there's no
guarantee that the A will really be a B...

Maybe it's easier said than done, but if you lend someone a B (through
it's A implementation) and then someone gives it back to you, it's
valid to think that maybe they're giving you something other than your
original B. Therefore, you'd better test and act accordingly (throw
exception) .

Regards,

Branco.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top