Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 13th, 2004, 11:43 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
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\">
Reply With Quote
  #2 (permalink)  
Old October 13th, 2004, 10:16 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Location: Melbourne, VIC, Australia.
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.
Reply With Quote
  #3 (permalink)  
Old October 14th, 2004, 02:20 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
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.:)
Reply With Quote
  #4 (permalink)  
Old October 14th, 2004, 06:10 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Location: Melbourne, VIC, Australia.
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.
Reply With Quote
  #5 (permalink)  
Old October 15th, 2004, 06:48 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
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.:)
Reply With Quote
  #6 (permalink)  
Old May 21st, 2007, 11:29 AM
Registered User
 
Join Date: May 2007
Location: , , .
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.

Reply With Quote
  #7 (permalink)  
Old May 23rd, 2007, 08:20 PM
Registered User
 
Join Date: May 2007
Location: , , .
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

Reply With Quote
  #8 (permalink)  
Old May 24th, 2007, 12:57 AM
joefawcett's Avatar
Wrox Author
Points: 9,716, Level: 42
Points: 9,716, Level: 42 Points: 9,716, Level: 42 Points: 9,716, Level: 42
Activity: 9%
Activity: 9% Activity: 9% Activity: 9%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,067
Thanks: 1
Thanked 37 Times in 36 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)
Reply With Quote
  #9 (permalink)  
Old May 26th, 2007, 12:08 PM
Registered User
 
Join Date: May 2007
Location: , , .
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?

Reply With Quote
  #10 (permalink)  
Old May 27th, 2007, 01:58 AM
joefawcett's Avatar
Wrox Author
Points: 9,716, Level: 42
Points: 9,716, Level: 42 Points: 9,716, Level: 42 Points: 9,716, Level: 42
Activity: 9%
Activity: 9% Activity: 9% Activity: 9%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,067
Thanks: 1
Thanked 37 Times in 36 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)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 12:01 PM
Friend Adder in VB.Net renjininair_2000 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 0 March 11th, 2006 01:34 AM
anyone friend can give me world address database a ChrisWu Classic ASP Databases 0 March 31st, 2004 10:13 PM



All times are GMT -4. The time now is 08:03 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.