Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 15th, 2009, 03:11 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Default What is the equivalant of a 'friend' keyword in C Sharp?

What is the equivalant of a 'friend' keyword in C Sharp?

How do I use the 'internal' keyword?

I have read that 'internal' keyword is a replacement for 'friend' in C#.

I am using a dll in my C# project that I have the source code for and yet I do not want to modify the existing code. I have inherited the class and I can use my inherited class any way I want. The problem is that most of the code in the parent class has private methods. Will using a friend somehow make it possible to access or call these private methods?

I have been told that if I have inherited from a base class, I should be able to access all the protected methods from the inheriting class. Therefore I should not need any "friend" equivalent. But does that also hold true for private methods?
 
Old January 15th, 2009, 07:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Quote:
I have read that 'internal' keyword is a replacement for 'friend' in C#.
That's correct. More like the analog than replacement.

Quote:
Will using a friend somehow make it possible to access or call these private methods?
Only if you replace the private modifier with internal. But then the methods will be exposed to any other code in the same assembly, as well as to the derived class.

Quote:
I have been told that if I have inherited from a base class, I should be able to access all the protected methods from the inheriting class.
That's correct.

Quote:
But does that also hold true for private methods?
No. Access to private methods in the base class will be linited to the base class. They won't be exposed to the derived class.
 
Old January 16th, 2009, 05:11 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

In addition to Bob's response private methods on a class are never going to be directly callable other than within the class. Internal methods are callable from within the same assembly so often methods are marked protected internal, callable from subclasses or within the assembly. You can get round this using reflection but this would indicate poor design in the base class, if a method's private it should be because there's no need to call it directly. However when designing classes not every future usage can be foreseen.
It's also possible that extension methods may suit your need if you're using C# 3 or greater.
__________________
Joe
http://joe.fawcett.name/
 
Old January 17th, 2009, 01:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Quote:
...private methods on a class are never going to be directly callable other than within the class
This is true. But there is a single important instance in which a certain type of 'private' method is 'indirectly' callable on a class: explicit interface implementation.

Explicit interface member implementations are private in an important sense: they are excluded from the public interface of classes and aren’t accessible through class instances or by their fully qualified name in a method invocation. Visual Studio’s Class View displays them as private members.

Of course, on the other hand, they are also public, in that they can be accessed through an interface instance.

Problem with interfaces is that since all interface members have implicitly public access, the interface member implementations in a class instance are potentially available to any code using the object. But by implementing an interface member explicitly (“privately”) , and pushing the actual method implementation into a private method, your object gains some level of protection. At least the method can now only be called by directly casting the object to the interface.

Important technique to be aware of anyway:

Code:
classProgram
{
 public interface IMyInterface
 {
    // All interface members implicitly have public access. 
    void MyMethod();
 }
 
 public abstract class MyBaseClass : IMyInterface
 {
    // Explicitly implemented interface method is technically private
    // from the perspective of class and strcut instances, and VS Class
    // View lists it as such.
    void IMyInterface.MyMethod()
    {
       // Call the private base class implementation of MyMethod
       this.MyMethod();
    }
 
    private void MyMethod()
    {
       // Actual implementation of MyMethod.
    }
 }
 
 public class MyDerivedClass : MyBaseClass
 {
 }
 
 static void Main(string[] args)
 {
    MyDerivedClass dc = new MyDerivedClass();
 
    // dc.IMyInterface.MyMethod()       // Inaccessible
    // dc.MyMethod()                         // Inaccessible
    // (MyBaseClass)dc).MyMethod(); // Inaccessible
 
    ((IMyInterface)dc).MyMethod();     // Accessible!!
 }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
using members of a C Sharp class Bill_Thompson C# 2005 4 January 13th, 2009 06:04 PM
schedule C-Sharp process Derf ASP.NET 2.0 Professional 9 April 18th, 2008 10:21 PM
Friend Class in C# mehdi62b C# 11 March 13th, 2008 06:54 PM
Friend WithEvents kscase Visual Basic 2005 Basics 1 May 3rd, 2007 03:16 PM
HOW TO EDIT THE MAC WITH C SHARP? ÄÏÔÚÄÏ·½ C# 0 June 23rd, 2006 02:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.