Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

Specify member scope in Interface Classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aaronh64@hotmail.com - 30 Dec 2004 21:59 GMT
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
Matt Berther - 31 Dec 2004 05:14 GMT
Hello aaronh64@hotmail.com,

They are not privately implemented, rather explicitly implemented. Simply
put, the implementation is still public, however, it is available only through
the interface, rather than on the class directly.

The implementation in all likelihood looks something like this:

class WebControl : IAttributeAccessor
{
   string IAttributeAccessor.GetAttribute(string key)
   {
       // some code
   }

   void IAttributeAccessor.SetAttribute(string key, string value)
   {
       // some code
   }    
}

So, the way that this can get called is as such (assuming the class above):

WebControl ctl = new WebControl();
IAttributeAccessor accessor = ctl as IAttributeAccessor;
if (accessor != null)
{
   accessor.SetAttribute(key, value);
}

You can read more about explicit interface implementation at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwl
kexplicitinterfaceimplementationtutorial.asp


Hope this helps... If not, please follow up...

--
Matt Berther
http://www.mattberther.com

> Have you ever noticed that when you look at an Interface Class
> definition (ie IDisposable), the methods or properties belonging to
[quoted text clipped - 22 lines]
> Any thoughts or ideas as to how they accomplished this would be great!
> Aaron Hanusa
aaronh64@hotmail.com - 31 Dec 2004 16:52 GMT
Matt, perfect, the link is exactly what I was looking for, thanks!
Aaron

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.