Is VB.NET breaking leaking encapsulation ...

A

AshokG

Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

Note that Age is private in VB is getting accessing from C#/VB.NET through
interface... -Huh

C# doen't allow this as it won't compile!

Regards
Ashok
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

AshokG said:
Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

Note that Age is private in VB is getting accessing from C#/VB.NET through
interface... -Huh

C# doen't allow this as it won't compile!

Regards
Ashok

You are confusing the Age method with the Age method. :)

There are actually two methods Age in the class. The method Age in the
Test class is private. The method Age in the ITest interface is public.
They just happen to be the same method and happen to have the same name.

When the method has a different name than the method that it implements,
it gets less confusing:

Private Function Answer() As Integer Implements IQuestion.Reply
Return 42
End Function

The method Answer is private, but the method Reply is public.


In C# it's a bit stricter, and thus less confusing. The methods that
implement the interface has to have the same names as in the interface,
and they have to be public.
 
A

AshokG

Göran Andersson,

Thanks for the reply. Even if you rename the method in the implementation it
get's called.

My point here is allowing to call a private method through the Interface. If
you delcare with class name then you can't call.

So, Private members should not be allowed to call from outside the class and
similarly it should not allow Interface implemented member to declared as
Private

Regards,
Ashok

Göran Andersson said:
AshokG said:
Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above
ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

Note that Age is private in VB is getting accessing from C#/VB.NET
through interface... -Huh

C# doen't allow this as it won't compile!

Regards
Ashok

You are confusing the Age method with the Age method. :)

There are actually two methods Age in the class. The method Age in the
Test class is private. The method Age in the ITest interface is public.
They just happen to be the same method and happen to have the same name.

When the method has a different name than the method that it implements,
it gets less confusing:

Private Function Answer() As Integer Implements IQuestion.Reply
Return 42
End Function

The method Answer is private, but the method Reply is public.


In C# it's a bit stricter, and thus less confusing. The methods that
implement the interface has to have the same names as in the interface,
and they have to be public.
 
A

Armin Zingler

AshokG said:
Göran Andersson,

Thanks for the reply. Even if you rename the method in the
implementation it get's called.

My point here is allowing to call a private method through the
Interface. If you delcare with class name then you can't call.

So, Private members should not be allowed to call from outside the
class and similarly it should not allow Interface implemented member
to declared as Private

You must distinguish between the "Class interface" (or better: the entirety
of all publicly available members) and the implemented interface. As you see
here, the Private modifier does work:

Public Class Test2
Public Sub Test
dim o as new test
msgbox(o.Age) '<---- Does not work because it's private
end sub
End Class

Using the ITest interface, the member is accessible, of course, because
there are no private Interface members at all. That's the purpose of an
interface.

Without this behavior, it wouldn't be possible to define the set of publicly
accessible members independently from the implemented interfaces.

In other words, by implementing an Interface, I don't want to be forced to
make a member public through a class reference.


Armin
 
T

tommaso.gastaldi

Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}

}

Note that Age is private in VB is getting accessing from C#/VB.NET through
interface... -Huh

C# doen't allow this as it won't compile!

Regards
Ashok

Nope. Within VB, if you try this, you will see you get a complaint
from the compiler:

Public Interface ITest
Function Name() As String
Function Age() As Integer
End Interface

Class ClassLibrary
Implements ITest

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

End Class


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim t As New ClassLibrary
MsgBox(t.Test)
End Sub

End Class



It wants:


Public Function Age() As Integer Implements ITest.Age

End Function

Public Function Name() As String Implements ITest.Name

End Function


The compiler autonatically provides them.


T
 
B

Bill McCarthy

Hi Ashok,

An interface is a contract. IF you implement an interface you guarantee to
expose the methods that interface defines.. that is the contract. C# also
has private interface implementation via explicit interface implementation,
in which case you can't even call that method from inside the class, you
must first cast the "this" (Me in Vb) reference to the interface. That is,
in the strictest form of the contract, the members are always accessible but
you may need to cast to the interface first.

I wrote a little mroe aobut this here:
http://msmvps.com/blogs/bill/archive/2005/02/27/37105.aspx

Bill.



AshokG said:
Göran Andersson,

Thanks for the reply. Even if you rename the method in the implementation
it get's called.

My point here is allowing to call a private method through the Interface.
If you delcare with class name then you can't call.

So, Private members should not be allowed to call from outside the class
and similarly it should not allow Interface implemented member to declared
as Private

Regards,
Ashok

Göran Andersson said:
AshokG said:
Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above
ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

Note that Age is private in VB is getting accessing from C#/VB.NET
through interface... -Huh

C# doen't allow this as it won't compile!

Regards
Ashok

You are confusing the Age method with the Age method. :)

There are actually two methods Age in the class. The method Age in the
Test class is private. The method Age in the ITest interface is public.
They just happen to be the same method and happen to have the same name.

When the method has a different name than the method that it implements,
it gets less confusing:

Private Function Answer() As Integer Implements IQuestion.Reply
Return 42
End Function

The method Answer is private, but the method Reply is public.


In C# it's a bit stricter, and thus less confusing. The methods that
implement the interface has to have the same names as in the interface,
and they have to be public.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

AshokG said:
Göran Andersson,

Thanks for the reply. Even if you rename the method in the implementation it
get's called.

Yes, of course. You have exposed the method publically through the
interface, that's why you can call it through the interface, but not
through the class.
My point here is allowing to call a private method through the Interface. If
you delcare with class name then you can't call.

But the method is not private. When you expose it throught the interface
it's public.
So, Private members should not be allowed to call from outside the class

You can't call private members, but you can always expose a private
member through another member that is public. For example:

Class Test

Private Function SecretAge() As Integer
Return 12
End Function

Public Function Age() As Integer
Return SecretAge()
End Function

End Class

Making the SecretAge private does not prevent a public method from using it.
and
similarly it should not allow Interface implemented member to declared as
Private

Then there would have to be a different way of telling the compiler if
the method should be reachable through the class or not. C# does this by
differentiating between implicit and explicit implementation of the
interface. That can't be used in VB, as there is only explicit
implementation.
 
A

AshokG

Göran Andersson,

Thanks for the reply,
Then there would have to be a different way of telling the compiler if the
method should be reachable through the class or not. C# does this by
differentiating between implicit and explicit implementation of the
interface. That can't be used in VB, as there is only explicit
implementation.

in C# even if it is implemented explicitly, it will not get compile. Means
C# enforces the implementing class to declare it as Public - VB doesn't!. So
VB should also do this.

Regards,
Ashok
 
A

AshokG

Bill,

Thanks on responding,

C# even if a class implements it explicitly, it will not get compile if it
is declared it as private. C# enforces the implementing class to declare it
as Public - VB doesn't!.

I think VB should also do this to eliminate the confusion.

Regards,
Ashok

Bill McCarthy said:
Hi Ashok,

An interface is a contract. IF you implement an interface you guarantee
to expose the methods that interface defines.. that is the contract. C#
also has private interface implementation via explicit interface
implementation, in which case you can't even call that method from inside
the class, you must first cast the "this" (Me in Vb) reference to the
interface. That is, in the strictest form of the contract, the members
are always accessible but you may need to cast to the interface first.

I wrote a little mroe aobut this here:
http://msmvps.com/blogs/bill/archive/2005/02/27/37105.aspx

Bill.



AshokG said:
Göran Andersson,

Thanks for the reply. Even if you rename the method in the implementation
it get's called.

My point here is allowing to call a private method through the Interface.
If you delcare with class name then you can't call.

So, Private members should not be allowed to call from outside the class
and similarly it should not allow Interface implemented member to
declared as Private

Regards,
Ashok

Göran Andersson said:
AshokG wrote:
Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above
ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

Note that Age is private in VB is getting accessing from C#/VB.NET
through interface... -Huh

C# doen't allow this as it won't compile!

Regards
Ashok


You are confusing the Age method with the Age method. :)

There are actually two methods Age in the class. The method Age in the
Test class is private. The method Age in the ITest interface is public.
They just happen to be the same method and happen to have the same name.

When the method has a different name than the method that it implements,
it gets less confusing:

Private Function Answer() As Integer Implements IQuestion.Reply
Return 42
End Function

The method Answer is private, but the method Reply is public.


In C# it's a bit stricter, and thus less confusing. The methods that
implement the interface has to have the same names as in the interface,
and they have to be public.
 
A

AshokG

oops!

I quickly checked the behaviour once again. C# simply doesn't allow to put
access modifiers on the explicitly impletemented interface members.

Anyway still C#'s behaviour is correct IMHO.

Regards,
Ashok
 
A

AshokG

oops!

I quickly checked the behaviour once again. C# simply doesn't allow to put
access modifiers on the explicitly impletemented interface members.

Anyway still C#'s behaviour is correct IMHO.

Regards,
Ashok

Bill McCarthy said:
Hi Ashok,

An interface is a contract. IF you implement an interface you guarantee
to expose the methods that interface defines.. that is the contract. C#
also has private interface implementation via explicit interface
implementation, in which case you can't even call that method from inside
the class, you must first cast the "this" (Me in Vb) reference to the
interface. That is, in the strictest form of the contract, the members
are always accessible but you may need to cast to the interface first.

I wrote a little mroe aobut this here:
http://msmvps.com/blogs/bill/archive/2005/02/27/37105.aspx

Bill.



AshokG said:
Göran Andersson,

Thanks for the reply. Even if you rename the method in the implementation
it get's called.

My point here is allowing to call a private method through the Interface.
If you delcare with class name then you can't call.

So, Private members should not be allowed to call from outside the class
and similarly it should not allow Interface implemented member to
declared as Private

Regards,
Ashok

Göran Andersson said:
AshokG wrote:
Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above
ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

Note that Age is private in VB is getting accessing from C#/VB.NET
through interface... -Huh

C# doen't allow this as it won't compile!

Regards
Ashok


You are confusing the Age method with the Age method. :)

There are actually two methods Age in the class. The method Age in the
Test class is private. The method Age in the ITest interface is public.
They just happen to be the same method and happen to have the same name.

When the method has a different name than the method that it implements,
it gets less confusing:

Private Function Answer() As Integer Implements IQuestion.Reply
Return 42
End Function

The method Answer is private, but the method Reply is public.


In C# it's a bit stricter, and thus less confusing. The methods that
implement the interface has to have the same names as in the interface,
and they have to be public.
 
A

AshokG

oops!

I quickly checked the behaviour once again. C# simply doesn't allow to put
access modifiers on the explicitly impletemented interface members.

Anyway still C#'s behaviour is correct IMHO.

Regards,
Ashok

AshokG said:
Göran Andersson,

Thanks for the reply,
Then there would have to be a different way of telling the compiler if
the method should be reachable through the class or not. C# does this by
differentiating between implicit and explicit implementation of the
interface. That can't be used in VB, as there is only explicit
implementation.

in C# even if it is implemented explicitly, it will not get compile. Means
C# enforces the implementing class to declare it as Public - VB doesn't!.
So VB should also do this.

Regards,
Ashok
 
B

Bill McCarthy

Hi Ashok,

in C# if you use Explicit Interface implementation, then you cannot mark
*any* accessibility on the method. As I said you cannot call the method
from even inside your won class without casting to the interface. SO that
is in essence private, or even more so, yet it is exposed via the interface
because an interface is a public contract.

To quote from the ECMA specification for the CLR:

Partition I , Section 8.9.4
"However, since accessibility attributes are relative to the implementing
type rather than the interface itself, all members of an interface shall
have public accessibility, and no security permissions can be attached to
members or to the interface itself."


Here's an example in C#:

interface IFoo
{
void Bar();
}


class Baz : IFoo
{

void test()
{
//can't call IFoo.Bar, need to cast to IFoo
(this as IFoo).Bar();
}


void IFoo.Bar() //Can be called from anywhere the class is cast to IFoo
{
Console.WriteLine("IFoo.Bar");
}

}


The IFoo.Bar method can be called anywhere you have a reference to the
object. This is exactly the same as in VB.

In VB however you can have multiple use of the same method, and that's when
the accessibility comes into play.


In either VB or C#, if you implement an interface you are agreeing to the
contract that the methods will be accessible from anywhere a reference to
the containing object is.






AshokG said:
Bill,

Thanks on responding,

C# even if a class implements it explicitly, it will not get compile if it
is declared it as private. C# enforces the implementing class to declare
it as Public - VB doesn't!.

I think VB should also do this to eliminate the confusion.

Regards,
Ashok

Bill McCarthy said:
Hi Ashok,

An interface is a contract. IF you implement an interface you guarantee
to expose the methods that interface defines.. that is the contract. C#
also has private interface implementation via explicit interface
implementation, in which case you can't even call that method from inside
the class, you must first cast the "this" (Me in Vb) reference to the
interface. That is, in the strictest form of the contract, the members
are always accessible but you may need to cast to the interface first.

I wrote a little mroe aobut this here:
http://msmvps.com/blogs/bill/archive/2005/02/27/37105.aspx

Bill.



AshokG said:
Göran Andersson,

Thanks for the reply. Even if you rename the method in the
implementation it get's called.

My point here is allowing to call a private method through the
Interface. If you delcare with class name then you can't call.

So, Private members should not be allowed to call from outside the class
and similarly it should not allow Interface implemented member to
declared as Private

Regards,
Ashok

AshokG wrote:
Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above
ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

Note that Age is private in VB is getting accessing from C#/VB.NET
through interface... -Huh

C# doen't allow this as it won't compile!

Regards
Ashok


You are confusing the Age method with the Age method. :)

There are actually two methods Age in the class. The method Age in the
Test class is private. The method Age in the ITest interface is public.
They just happen to be the same method and happen to have the same
name.

When the method has a different name than the method that it
implements, it gets less confusing:

Private Function Answer() As Integer Implements IQuestion.Reply
Return 42
End Function

The method Answer is private, but the method Reply is public.


In C# it's a bit stricter, and thus less confusing. The methods that
implement the interface has to have the same names as in the interface,
and they have to be public.
 
A

Armin Zingler

AshokG said:
oops!

I quickly checked the behaviour once again. C# simply doesn't allow
to put access modifiers on the explicitly impletemented interface
members.

Why do you consider this limitation being...
Anyway still C#'s behaviour is correct IMHO.

....the correct behavior?



Armin
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

AshokG said:
oops!

I quickly checked the behaviour once again. C# simply doesn't allow to put
access modifiers on the explicitly impletemented interface members.

As I said earlier, then you would have to come up with a different way
of telling the compiler if the method should be reachable through the
class or not.
Anyway still C#'s behaviour is correct IMHO.

As the syntax for implementing interfaces is different, you can't take
the C# behaviour and apply it to VB.
 

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