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 .
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.
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 ...)
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).
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
}
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.
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?
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?