473,320 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Specify member scope in Interface Classes

Have you ever noticed that when you look at an Interface Class
definition (ie IDisposable), the methods or properties belonging to the
class are defined as abstract (C#) or overridable (vb)?

How can this be? When I create an Interface class and try to give
methods any signature at all, I receive compile errors. In terms of
scope, all that I can define for method implementation is the return
type, method name, and arguments. I cannot define a method as
public/private, abstract/virtual (C#), overridable (VB), etc.
How did Microsoft accomplish this? You'll notice that pretty much all
Interface classes in the .NET framework have this kind of scope.

Why do I care you might ask? I would like to create an Interface class
and have classes that implement it hide the methods, so that the
interface method(s) implementation is private. A good example of how
the framework is doing this is in the
System.Web.UI.WebControls.WebControl class.

Notice that the class implements the IAttributeAccessor interface,
which defines 2 methods (SetAttribute and GetAttribute). Also notice
that these methods are NOT accessible from WebControl or any inheriting
control, such as TextBox, Button, etc. How did they accomplish this
behavior? As of now, I'm thinking that the 2 methods are marked with
an attribute, but which one?

Any thoughts or ideas as to how they accomplished this would be great!
Aaron Hanusa

Jul 21 '05 #1
4 1748
But, if you cast the WebControl to IAttributeAccessor, then you can call
SetAttribute and GetAttribute.

So, the methods are not really inaccessible after all.

The point of making an interface, is so that someone can cast your object to
the interface class, and call the method, without knowing or caring what the
actual type is.

If the method is private, then no one would ever need to do this - so at
that point, who cares if the method is in the interface? No one but the
class itself could ever call it. Why would it matter in the slightest if it
was in the interface in this case?

<aa******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Have you ever noticed that when you look at an Interface Class
definition (ie IDisposable), the methods or properties belonging to the
class are defined as abstract (C#) or overridable (vb)?

How can this be? When I create an Interface class and try to give
methods any signature at all, I receive compile errors. In terms of
scope, all that I can define for method implementation is the return
type, method name, and arguments. I cannot define a method as
public/private, abstract/virtual (C#), overridable (VB), etc.
How did Microsoft accomplish this? You'll notice that pretty much all
Interface classes in the .NET framework have this kind of scope.

Why do I care you might ask? I would like to create an Interface class
and have classes that implement it hide the methods, so that the
interface method(s) implementation is private. A good example of how
the framework is doing this is in the
System.Web.UI.WebControls.WebControl class.

Notice that the class implements the IAttributeAccessor interface,
which defines 2 methods (SetAttribute and GetAttribute). Also notice
that these methods are NOT accessible from WebControl or any inheriting
control, such as TextBox, Button, etc. How did they accomplish this
behavior? As of now, I'm thinking that the 2 methods are marked with
an attribute, but which one?

Any thoughts or ideas as to how they accomplished this would be great!
Aaron Hanusa

Jul 21 '05 #2
Aaron,
Have you ever noticed that when you look at an Interface Class
definition (ie IDisposable), the methods or properties belonging to the
class are defined as abstract (C#) or overridable (vb)?
Well the methods are implicitly virtual, and some API browsers may
incorrectly include such modifiers on the methods even though you're
not allowed to explicitly write them yourself.

How did Microsoft accomplish this?
They didn't.

Notice that the class implements the IAttributeAccessor interface,
which defines 2 methods (SetAttribute and GetAttribute). Also notice
that these methods are NOT accessible from WebControl or any inheriting
control, such as TextBox, Button, etc. How did they accomplish this
behavior?


In VB you simply make the implementing method Private.

Private Sub Foo() Implements IBar.Foo

In C# you use explicit implementation

void IBar.Foo() {}

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #3
As to how they did it, I believe this works the way you describe (except
that of course you can still cast the object to ITest, and call the method):

Public Class MyTestClass
Implements ITest
Private Shadows Sub MyTestSub() Implements ITest.MyTestSub
End Sub
End Class

Public Interface ITest
Sub MyTestSub()
End Interface

<aa******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Have you ever noticed that when you look at an Interface Class
definition (ie IDisposable), the methods or properties belonging to the
class are defined as abstract (C#) or overridable (vb)?

How can this be? When I create an Interface class and try to give
methods any signature at all, I receive compile errors. In terms of
scope, all that I can define for method implementation is the return
type, method name, and arguments. I cannot define a method as
public/private, abstract/virtual (C#), overridable (VB), etc.
How did Microsoft accomplish this? You'll notice that pretty much all
Interface classes in the .NET framework have this kind of scope.

Why do I care you might ask? I would like to create an Interface class
and have classes that implement it hide the methods, so that the
interface method(s) implementation is private. A good example of how
the framework is doing this is in the
System.Web.UI.WebControls.WebControl class.

Notice that the class implements the IAttributeAccessor interface,
which defines 2 methods (SetAttribute and GetAttribute). Also notice
that these methods are NOT accessible from WebControl or any inheriting
control, such as TextBox, Button, etc. How did they accomplish this
behavior? As of now, I'm thinking that the 2 methods are marked with
an attribute, but which one?

Any thoughts or ideas as to how they accomplished this would be great!
Aaron Hanusa

Jul 21 '05 #4
Thanks for your reply Marina,

Yes, you are correct, I could cast the object and access the methods
then, but I think you are missing my point. Microsoft achieved the
behavior that I originally posted, and that's what I'm after. How did
they do it? They were somehow able to define the methods as abstract,
or at least have the WebControl define the methods and then hide them.
Furthermore, what if you want inheriting classes that implement the
interface to be able to override that functionality?

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gianni Mariani | last post by:
While digging for a work around for a VC++7.1 compiler bug I came to find an interesting thing about pointer to member conversions. struct A { int a; }; struct B : A
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
2
by: Mark Sisson | last post by:
Hi all. SITUATION ================ 1. I have a base class with a member variable that's an object 2. I have several classes that inherit from the base class. 3. There are several methods in...
4
by: aaronh64 | last post by:
Have you ever noticed that when you look at an Interface Class definition (ie IDisposable), the methods or properties belonging to the class are defined as abstract (C#) or overridable (vb)? How...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
5
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope...
3
by: =?Utf-8?B?Y2FsZGVyYXJh?= | last post by:
Dear all, I have one question base on a choice of having a public interfcace access compare to public class member. IN other word let say that I have a public interface named ImyInterface with...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.