Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 October 13th, 2004, 11:43 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default Friend Class in C#

Hello everybody,
I know we have Friend Class in C++ but I wanted to know whether I can use friend class in C#,
I figured out that its possible to use friend class in VB.NET(from http://msdn.microsoft.com/library/de...akeyFriend.asp)
but it seems I cant use it in C# (because of this link)
am I right?
Thanks in advance.

--------------------------------------------
Mehdi.:)
__________________
_____________
<font color=\"teal\"><font size=\"1\"><b>Mehdi.
software student.</b></font id=\"size1\"></font id=\"teal\">
 
Old October 13th, 2004, 10:16 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are right in that you can't use the keyword Friend in C#. But the equivalnet in C# is internal.

In VB.NET the a Friend class is
Quote:
quote:An entity with friend access is accessible only within the program that contains the entity declaration
.

In C# an internal class is only accessible within the assembly which it is declared (effectively the same thing).

So, to provide the same functionality just declare your class in C# as "internal".

Kep.
 
Old October 14th, 2004, 02:20 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks for checking my problem,
I meant from Friend Class something there is in C++(not in VB.NET or C#.NET)
Internal(C#.NET)==Friend(VB.NET)!=Friend(C++)
Friend classes in C++ let just one class access another class
Take a look at my example
Code:
C++
class wheel{
friend class car;//car can access wheel without any restriction
friend class bus;//bus can access wheel without any restriction
 //other declarations for wheel
};
class car{
 //declarations for car
};
class bus{
 //declarations for bus
};
as you see I didnt use Nested Classes,(I used Composite Classes instead)
I dont know how I can convert my C++ example to C#(without using Nested Classes)
(if I use Nested classes I have to declare Wheel class
in every bus or car class ...)


--------------------------------------------
Mehdi.:)
 
Old October 14th, 2004, 06:10 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Couldn't you use a generalised base class.

For example...
Code:
public abstract class Vehicle
{
    protected class Wheel
    {
        // details of wheel
    }
    // vehicle details
}

public class Car : Vehicle
{
    // car specifics
}

public class Bus : Vehicle
{
    // bus specifics
}
This should give you access to the Wheel class in both Car and Bus (and derivatives).

I hope this helps.


Kep.
 
Old October 15th, 2004, 06:48 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

yes exactly,
Code:
public abstract class Vehicle
{
    protected class Wheel
    {
        // details of wheel
    }
    protected Wheel wheel;   
    // vehicle details
}

public class Car : Vehicle
{
    // car specifics
}

public class Bus : Vehicle
{
    // bus specifics
}
Thanks Keith.

--------------------------------------------
Mehdi.:)
 
Old May 21st, 2007, 11:29 AM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get "inaccessable due to protection level" compile errors when I try this. Are you making the "details of wheel" public? That's the only way I've gotten this to work. I've always been taught to strive for the goal of making all data members private.

 
Old May 23rd, 2007, 08:20 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Or at the very least, protected. I know this thread is about 2.5 years old, but can anyone give me some information?

Thanks,
Tylo

 
Old May 24th, 2007, 12:57 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I think protected internal is correct, when a thread is as old as this one it's probably necessary to state your actual problem more clearly, what exactly are you trying to achieve?

--

Joe (Microsoft MVP - XML)
 
Old May 26th, 2007, 12:08 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have a two classes (Class1 & Class2).

I want to allow one Class1 to edit Class2's private or protected data. However, I want Class2's data to remain uneditable by any other Classes.

In a way, I suppose the necessity of inheritance in this method ensures that only classes I want will be able to edit the data. However, I just feel funny making all the data public inside the base class(es). I'd like to make it protected or private, but the method doesn't seem to work that way. Should I just suck it up?

 
Old May 27th, 2007, 01:58 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Making the properties "protected internal" should work. That gives access to inhertited classes or ones in the same assembly.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Friend access modifier acts as private sandeep ASP.NET 2.0 Professional 2 July 19th, 2007 07:35 AM
Friend WithEvents kscase Visual Basic 2005 Basics 1 May 3rd, 2007 03:16 PM
friend adder in vb.net renjininair_2000 C# 1 March 13th, 2006 01:01 PM
Friend Adder in VB.Net renjininair_2000 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 0 March 11th, 2006 02:34 AM
anyone friend can give me world address database a ChrisWu Classic ASP Databases 0 March 31st, 2004 11:13 PM





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