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 13 1549
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
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
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
"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
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
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
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
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
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
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
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: .pd. |
last post by:
If I have a control that adds itself to its container, does that break
encapsulation? Is it a bad thing?
Here's what I mean:
public class fred
{
public barny b;
public fred()
{
|
by: K.K. |
last post by:
Consider the following code:
>>>>>>>>>>>
// Define an empty class
public class ZorgleCollection : Dictionary<string, Zorgle>
{
}
// Somewhere outside ZorgleCollection:
|
by: enchantingdb |
last post by:
I have an exam tomorrow that covers the perceived advantages and
disadvantages of object oriented programming, in particular
polymorphism, inheritance and encapsulation. I know the advantages but...
|
by: jmsantoss |
last post by:
Hi,
This is a design question. I have a class named "DataBuffer" that
stores some data. After "DataBuffer" is created it can not be modified.
All the methods of "DataBuffer" are const as data...
|
by: Roger Lakner |
last post by:
I often see operator implemented something like this:
class Foo { ... };
class FooList
{
public:
const Foo& operator (unsigned index) const {return
array;};
Foo& operator (unsigned index) ...
|
by: bluejack |
last post by:
Ahoy:
For as long as I've been using C, I've vacillated on the optimal
degree of encapsulation in my designs. At a minimum, I aggregate data
and code that operate on that data into classlike...
|
by: copx |
last post by:
I have recently read "Everything you ever wanted to know about C types" by
Peter Seebach (1), and learned about incomplete types. Now, I realise the
value of encapsulation, but I wonder whether it...
|
by: subramanian100in |
last post by:
Is my following understanding correct ?
Data abstraction means providing the interface - that is, the set of
functions that can be called by the user of a class.
Information hiding means...
|
by: rupert.thurner |
last post by:
the edgewall trac release 0.11 is blocked now since more than one
month for a memory leak nobody is able to find, see
http://groups.google.com/group/trac-dev/browse_thread/thread/116e519da54f16b...
|
by: cyberted |
last post by:
Hello all..
I am writing an application in PHP to send commands to a router via telnet using the fsockopen function which works well.
My question is that I want to read in a text file from...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |