By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Is VB.NET breaking leaking encapsulation ...

P: n/a
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
Aug 6 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 6 '07 #2

P: n/a
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" <gu***@guffa.comwrote in message
news:OC**************@TK2MSFTNGP04.phx.gbl...
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.

--
Göran Andersson
_____
http://www.guffa.com

Aug 6 '07 #3

P: n/a
On 6 Ago, 08:56, "AshokG" <gw2ks...@hotmail.comwrote:
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


Aug 6 '07 #4

P: n/a
"AshokG" <gw******@hotmail.comschrieb
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

Aug 6 '07 #5

P: n/a
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.../27/37105.aspx

Bill.

"AshokG" <gw******@hotmail.comwrote in message
news:ea*************@TK2MSFTNGP06.phx.gbl...
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" <gu***@guffa.comwrote in message
news:OC**************@TK2MSFTNGP04.phx.gbl...
>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.

--
Göran Andersson
_____
http://www.guffa.com

Aug 6 '07 #6

P: n/a
AshokG wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 6 '07 #7

P: n/a
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

"Göran Andersson" <gu***@guffa.comwrote in message
news:uJ*************@TK2MSFTNGP06.phx.gbl...
AshokG wrote:
>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.

--
Göran Andersson
_____
http://www.guffa.com

Aug 7 '07 #8

P: n/a
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" <Bi**@NOSPAM.comwrote in message
news:E7**********************************@microsof t.com...
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.../27/37105.aspx

Bill.

"AshokG" <gw******@hotmail.comwrote in message
news:ea*************@TK2MSFTNGP06.phx.gbl...
>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" <gu***@guffa.comwrote in message
news:OC**************@TK2MSFTNGP04.phx.gbl...
>>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.

--
Göran Andersson
_____
http://www.guffa.com


Aug 7 '07 #9

P: n/a
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
"Göran Andersson" <gu***@guffa.comwrote in message
news:uJ*************@TK2MSFTNGP06.phx.gbl...
AshokG wrote:
>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.

--
Göran Andersson
_____
http://www.guffa.com

Aug 7 '07 #10

P: n/a
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" <gw******@hotmail.comwrote in message
news:e$****************@TK2MSFTNGP02.phx.gbl...
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

"Göran Andersson" <gu***@guffa.comwrote in message
news:uJ*************@TK2MSFTNGP06.phx.gbl...
>AshokG wrote:
>>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.

--
Göran Andersson
_____
http://www.guffa.com



Aug 7 '07 #11

P: n/a
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" <Bi**@NOSPAM.comwrote in message
news:E7**********************************@microsof t.com...
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.../27/37105.aspx

Bill.

"AshokG" <gw******@hotmail.comwrote in message
news:ea*************@TK2MSFTNGP06.phx.gbl...
>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" <gu***@guffa.comwrote in message
news:OC**************@TK2MSFTNGP04.phx.gbl...
>>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.

--
Göran Andersson
_____
http://www.guffa.com


Aug 7 '07 #12

P: n/a
"AshokG" <gw******@hotmail.comschrieb
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
Aug 7 '07 #13

P: n/a
AshokG wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 7 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.