473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Internal vs friend (C++)

I need to grant a class access to protected fields of
another class in the way its possible in C++ with the
friend keyword. However I would like to keep the class
protected towards other class within the same
program/assembly. The two classes are not 'related'
(inherited).

How can I do this. I cant see how the keyword Internal
can be used for this purpose.

best regards Jesper.

Nov 15 '05 #1
7 9782
Jesper,

The only way that the internal keyword is going to help is if both
classes are defined in the same assembly. If they are, then internal fields
are able to be seen by any of the classes in the same assembly. The only
other option you have is protected, which forces you to derive from a base
class to see protected members.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jesper" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
I need to grant a class access to protected fields of
another class in the way its possible in C++ with the
friend keyword. However I would like to keep the class
protected towards other class within the same
program/assembly. The two classes are not 'related'
(inherited).

How can I do this. I cant see how the keyword Internal
can be used for this purpose.

best regards Jesper.

Nov 15 '05 #2
Do you know why this functionality (friend)is discarded
in C#, is it to be implemented in another way?
-----Original Message-----
Jesper,

The only way that the internal keyword is going to help is if bothclasses are defined in the same assembly. If they are, then internal fieldsare able to be seen by any of the classes in the same assembly. The onlyother option you have is protected, which forces you to derive from a baseclass to see protected members.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jesper" <an*******@discussions.microsoft.com> wrote in messagenews:04****************************@phx.gbl...
I need to grant a class access to protected fields of
another class in the way its possible in C++ with the
friend keyword. However I would like to keep the class
protected towards other class within the same
program/assembly. The two classes are not 'related'
(inherited).

How can I do this. I cant see how the keyword Internal
can be used for this purpose.

best regards Jesper.

.

Nov 15 '05 #3
Jesper,

It was never discarded. C# is not meant to be an extension or a
variation on C++. It is a new language, and this functionality is simply
just not there.

As for why it is not there, I imagine that the original language
designers didn't see much use for it. Also, given that the language has
been in use for a few years now and there hasn't been any huge outcry over
it, I would think that it serves as validation of their decision. I'm not
saying it is right or wrong, just that it is not there, and no one is really
complaining about it.

What exactly are you looking to achieve?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jesper" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
Do you know why this functionality (friend)is discarded
in C#, is it to be implemented in another way?
-----Original Message-----
Jesper,

The only way that the internal keyword is going to

help is if both
classes are defined in the same assembly. If they are,

then internal fields
are able to be seen by any of the classes in the same

assembly. The only
other option you have is protected, which forces you to

derive from a base
class to see protected members.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jesper" <an*******@discussions.microsoft.com> wrote in

message
news:04****************************@phx.gbl...
I need to grant a class access to protected fields of
another class in the way its possible in C++ with the
friend keyword. However I would like to keep the class
protected towards other class within the same
program/assembly. The two classes are not 'related'
(inherited).

How can I do this. I cant see how the keyword Internal
can be used for this purpose.

best regards Jesper.

.

Nov 15 '05 #4
Something like this, where I assert that an object is of
a certain type in order to get access to a member of the
class. Class2 cannot be inherited from Class1.

class Class1
{
protected int i;

Set_I(int i,object obj)
{
if (obj is Class2)
{
this.i = i;
}
}
}

If Class2 is a friend of Class1, I would not need to
write a setter. Moreover I would get errors at compile
time if I try to access the class from a class that is
not a friend.

by the way, thanks for answering
brgds Jesper.

-----Original Message-----
Jesper,

It was never discarded. C# is not meant to be an extension or avariation on C++. It is a new language, and this functionality is simplyjust not there.

As for why it is not there, I imagine that the original languagedesigners didn't see much use for it. Also, given that the language hasbeen in use for a few years now and there hasn't been any huge outcry overit, I would think that it serves as validation of their decision. I'm notsaying it is right or wrong, just that it is not there, and no one is reallycomplaining about it.

What exactly are you looking to achieve?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jesper" <an*******@discussions.microsoft.com> wrote in messagenews:0c****************************@phx.gbl...
Do you know why this functionality (friend)is discarded
in C#, is it to be implemented in another way?
>-----Original Message-----
>Jesper,
>
> The only way that the internal keyword is going to

help is if both
>classes are defined in the same assembly. If they are,
then internal fields
>are able to be seen by any of the classes in the same

assembly. The only
>other option you have is protected, which forces you
to derive from a base
>class to see protected members.
>
> Hope this helps.
>
>
>--
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
>
>"Jesper" <an*******@discussions.microsoft.com> wrote
in message
>news:04****************************@phx.gbl...
>> I need to grant a class access to protected fields

of >> another class in the way its possible in C++ with the >> friend keyword. However I would like to keep the class >> protected towards other class within the same
>> program/assembly. The two classes are not 'related'
>> (inherited).
>>
>> How can I do this. I cant see how the keyword Internal >> can be used for this purpose.
>>
>> best regards Jesper.
>>
>
>
>.
>

.

Nov 15 '05 #5
100
Hi Nicholas,
Also, given that the language has
been in use for a few years now and there hasn't been any huge outcry over
it, I would think that it serves as validation of their decision.
I disagree with you here. Enough questions about *friend* or releated has
been asked it this news group. Thus, I believe that the absence of this
functionality is more like flaw.

Look at this for example.

Lots of objects are exported via properties of another object (owner) if we
want the owner to be notified for changes that happen in the exported
objects we have couple of choices:

1. The owner can hook up bunch of events which can be very annoying and can
polute the code with a lot of EventArgs classes and delegates. What if the
owner wants to return back some result. Because of the multicast nature of
the events returning value via events could be error prone unless special
precautions are taken which is another exces of code.

2. The owner can provide public or internal functions for those classes to
send notifications about thier state changes. Which polutes the owner with
methods documented or named in a manner to give the hint that they are for
internal use.

3. Declare the class internally in the owner class. Which doesn't work in
all casses and especially now when the classes cannot be spread across files
makes the owner-class file big and hard to read and maintain (at least it is
good that we have #region directive which is nice handled by the IDE).

It would be so easy if we had c++-friend-like functionality. Yes, I know
one may argue that *friend* breaks encapsulation of the objects but it can
be vary useful and can help to write more clear and maintainable code.

I agree that *internal* declared methods and properties kind of solves the
problem. Classes or methods has to be declared as *friend* inside the owner
class which means that the classes has to be well known to the owner at
compile type. Once compiled no new friends can be added. Which in turn means
that the owner and its friends more like comes from the same assembly.
However *internals* are visible to all classes in the assembly, so we cannot
be sure that a team-mate won't decide to take the short cut and call those
methos.

Unfortunately, it is not enough the c# team to add the *friend*
functionality it has to be supported by the CLR as well. Otherwise it won't
do.

B\rgds
100
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jesper" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
Do you know why this functionality (friend)is discarded
in C#, is it to be implemented in another way?
-----Original Message-----
Jesper,

The only way that the internal keyword is going to

help is if both
classes are defined in the same assembly. If they are,

then internal fields
are able to be seen by any of the classes in the same

assembly. The only
other option you have is protected, which forces you to

derive from a base
class to see protected members.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jesper" <an*******@discussions.microsoft.com> wrote in

message
news:04****************************@phx.gbl...
> I need to grant a class access to protected fields of
> another class in the way its possible in C++ with the
> friend keyword. However I would like to keep the class
> protected towards other class within the same
> program/assembly. The two classes are not 'related'
> (inherited).
>
> How can I do this. I cant see how the keyword Internal
> can be used for this purpose.
>
> best regards Jesper.
>
.


Nov 15 '05 #6
Meaning no disrespect, but I've seen very few cases of a "friend" design
that couldn't be done better in another manner, one that maintains strict
encapsulation.

In your example, Class2 needs to access internals of Class1. However,
Class3 doesn't and you want to hide the internals from Class3. Then it's
not really an "internal" value, is it?

Take a few moments and look at the design. Review the patterns in the GoF
book and look for a different approach, one that doesn't use "friend".

Honestly, there are dozens of very well described patterns in OO development
(GoF, Fowler, and many others). I don't recall ANY that require this
construct. (I may be wrong in that. My memory is not so perfect as to
recall the details of every single published pattern).

I intend no insult or offense. I simply suggest that your design may be
better off if you refactored it.
--- Nick

"Jesper" <an*******@discussions.microsoft.com> wrote in message
news:0b****************************@phx.gbl...
Something like this, where I assert that an object is of
a certain type in order to get access to a member of the
class. Class2 cannot be inherited from Class1.

class Class1
{
protected int i;

Set_I(int i,object obj)
{
if (obj is Class2)
{
this.i = i;
}
}
}

If Class2 is a friend of Class1, I would not need to
write a setter. Moreover I would get errors at compile
time if I try to access the class from a class that is
not a friend.

by the way, thanks for answering
brgds Jesper.

-----Original Message-----
Jesper,

It was never discarded. C# is not meant to be an

extension or a
variation on C++. It is a new language, and this

functionality is simply
just not there.

As for why it is not there, I imagine that the

original language
designers didn't see much use for it. Also, given that

the language has
been in use for a few years now and there hasn't been

any huge outcry over
it, I would think that it serves as validation of their

decision. I'm not
saying it is right or wrong, just that it is not there,

and no one is really
complaining about it.

What exactly are you looking to achieve?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jesper" <an*******@discussions.microsoft.com> wrote in

message
news:0c****************************@phx.gbl...
Do you know why this functionality (friend)is discarded
in C#, is it to be implemented in another way?

>-----Original Message-----
>Jesper,
>
> The only way that the internal keyword is going to
help is if both
>classes are defined in the same assembly. If they are, then internal fields
>are able to be seen by any of the classes in the same
assembly. The only
>other option you have is protected, which forces you to derive from a base
>class to see protected members.
>
> Hope this helps.
>
>
>--
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
>
>"Jesper" <an*******@discussions.microsoft.com> wrote in message
>news:04****************************@phx.gbl...
>> I need to grant a class access to protected fields of >> another class in the way its possible in C++ with the >> friend keyword. However I would like to keep the class >> protected towards other class within the same
>> program/assembly. The two classes are not 'related'
>> (inherited).
>>
>> How can I do this. I cant see how the keyword Internal >> can be used for this purpose.
>>
>> best regards Jesper.
>>
>
>
>.
>

.

Nov 15 '05 #7
Hi Nick, Thanks your kind reply and advise, I'll look into GoF etc
Best regards Jesper.
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Shea Martin | last post by:
Class A { public: class B; int funct(const B &b); }; Class A::B { private:
8
by: Carlos J. Quintero | last post by:
Hi, As you know the current keywords "protected internal" (C#) or "Protected Friend" (VB.Net) means "Protected Or internal" (C#) or "Protected Or Friend" (VB.Net), that is, the member is...
2
by: miben | last post by:
I need to set a variable returned by a readonly property in a class by another class. So the only way to set that value is from a specific class and function. Public Sub Main Dim setter As New...
2
by: Brian Henry | last post by:
Is there a .NET equivilent of the "internal" keyword in VB.NET? say if i had this in C# internal bool b_MyBoolean;
10
by: LZXIA | last post by:
Compiling... overlaod.cpp d:\vc work\cpp\think_in_cpp\chapter10\overlaod.cpp(17) : fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1786) Please choose the Technical...
2
by: Mountain | last post by:
If a large framework (or library, application, etc.) has optional parts and some users may not want all parts, it would make sense to break the framework into separate physical assemblies. However,...
4
by: bob_jenkins | last post by:
C# allows code to be generated, but the generated code is in its own assembly. Is there a way for that generated code to access internal classes in the assembly that produced it? To have the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.