473,378 Members | 1,375 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,378 software developers and data experts.

I need something between protected and public

It seems I need one more type of member protection in my C# classes. I
run into this regularly. I've got a class X in assembly A. Class Y in
assembly B inherits from X and has a constructor that takes an
instance of X. The only way class Y can access data on the instance of
X coming into its constructor is if I make it public on X. Public is
too strong in this situation if data is only used in constructors of
derived classes. I want the ability to allow privileged access to
members of an instance from derived classes. (Protected allows this
privileged access only on the base instance.) Has anyone else run into
this shortcoming?
Nov 21 '07 #1
8 1125
not_a_commie <no********@gmail.comwrote:
It seems I need one more type of member protection in my C# classes. I
run into this regularly. I've got a class X in assembly A. Class Y in
assembly B inherits from X and has a constructor that takes an
instance of X. The only way class Y can access data on the instance of
X coming into its constructor is if I make it public on X. Public is
too strong in this situation if data is only used in constructors of
derived classes. I want the ability to allow privileged access to
members of an instance from derived classes. (Protected allows this
privileged access only on the base instance.) Has anyone else run into
this shortcoming?
Sorry, it's not clear to me what protected doesn't do for you. Could
you give a concrete example with code?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 21 '07 #2
It's not qutie what you described, but it'll probably do the trick:

1 - Mark your method as internal
2 - Grant explicit privlidges to other assemblies to see your Internal
methods.

[assembly:
InternalsVisibleTo("Coversant.SoapBox.Services.Som eService,PublicKey=....")]

.... although, I must admit, I'm not clear on why "protected" isn't working
for you.

--
Chris mullins

"not_a_commie" <no********@gmail.comwrote in message
news:ff**********************************@g21g2000 hsh.googlegroups.com...
It seems I need one more type of member protection in my C# classes. I
run into this regularly. I've got a class X in assembly A. Class Y in
assembly B inherits from X and has a constructor that takes an
instance of X. The only way class Y can access data on the instance of
X coming into its constructor is if I make it public on X. Public is
too strong in this situation if data is only used in constructors of
derived classes. I want the ability to allow privileged access to
members of an instance from derived classes. (Protected allows this
privileged access only on the base instance.) Has anyone else run into
this shortcoming?

Nov 21 '07 #3
As Jon alluded to, I'm not sure whether what you describe is a shortcoming or
simply an attempt to make the language's access modifiers do something that
would normally be done in another way. If,as you opine, you "run into this
regularly", it could be more of a developer design paradigm limitation rather
than a framework shortcoming. More details would certainly help.

--Peter
"Inside every large program, there is a small program trying to get out."
http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://www.blogmetafinder.com

"not_a_commie" wrote:
It seems I need one more type of member protection in my C# classes. I
run into this regularly. I've got a class X in assembly A. Class Y in
assembly B inherits from X and has a constructor that takes an
instance of X. The only way class Y can access data on the instance of
X coming into its constructor is if I make it public on X. Public is
too strong in this situation if data is only used in constructors of
derived classes. I want the ability to allow privileged access to
members of an instance from derived classes. (Protected allows this
privileged access only on the base instance.) Has anyone else run into
this shortcoming?
Nov 21 '07 #4
On 2007-11-21 13:58:59 -0800, Jon Skeet [C# MVP] <sk***@pobox.comsaid:
not_a_commie <no********@gmail.comwrote:
>It seems I need one more type of member protection in my C# classes. I
run into this regularly. I've got a class X in assembly A. Class Y in
assembly B inherits from X and has a constructor that takes an
instance of X. The only way class Y can access data on the instance of
X coming into its constructor is if I make it public on X. Public is
too strong in this situation if data is only used in constructors of
derived classes. I want the ability to allow privileged access to
members of an instance from derived classes. (Protected allows this
privileged access only on the base instance.) Has anyone else run into
this shortcoming?

Sorry, it's not clear to me what protected doesn't do for you. Could
you give a concrete example with code?
I suspect he's trying to do something like this:

class X
{
protected int _value;

public X(int value)
{
_value = value;
}
}

class Y : X
{
public Y(X x)
{
// This line generates CS1540 because
// x._value is not a legal expression here
_value = x._value;
}
}

or maybe this:

class Y : X
{
// same problem with x._value
public Y(X x) : base(x._value) { }
}

Why C# doesn't allow this, I'm not completely clear on. But it
doesn't. Code in class Y can only access the protected member on an
instance of Y, not X.

Pete

Nov 22 '07 #5
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote:

<snip>
Why C# doesn't allow this, I'm not completely clear on. But it
doesn't. Code in class Y can only access the protected member on an
instance of Y, not X.
Ah, right. I think the idea is to give you more information about types
of your own instance, without exposing the details of "foreigners". I
suspect it's not so much a C# decision as a CLR design decision.

Can't say I've ever had a problem with it though...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 22 '07 #6
"Peter Duniho" <Np*********@NnOwSlPiAnMk.comschrieb im Newsbeitrag
news:2007112117004664440-NpOeStPeAdM@NnOwSlPiAnMkcom...
On 2007-11-21 13:58:59 -0800, Jon Skeet [C# MVP] <sk***@pobox.comsaid:

I suspect he's trying to do something like this:

class X
{
protected int _value;

public X(int value)
{
_value = value;
}
}

class Y : X
{
public Y(X x)
{
// This line generates CS1540 because
// x._value is not a legal expression here
_value = x._value;
}
}
OK, here an idea of a solution:

class X
{
//This is accessible by a derived class only, if the accessed expression
is of that derived class.
protected int _value;

//This is accessible by derived class.
protected static GetValue(X x)
{
return x._value;
}

public X(int value)
{
_value = value;
}
}

class Y: X
{
public Y(X x)
{
_value = GetValue(x);
}
}

Christof

Nov 22 '07 #7
The two solutions proposed (protected static and privileged internal
access) are both excellent suggestions that will help me solve this
problem. Thank you.
Nov 22 '07 #8
On 2007-11-22 08:04:41 -0800, not_a_commie <no********@gmail.comsaid:
The two solutions proposed (protected static and privileged internal
access) are both excellent suggestions that will help me solve this
problem. Thank you.
For what it's worth, my preferred solution (assuming that all of the
interesting criteria were already included in your original post) would
be simply to define a constructor for X that takes an instance of X and
use the protected field there instead of in Y.

Especially if you are considering creating a static getter method in
class X anyway (i.e. you're going to tie the API for X to the needs of
Y), you might as well do it in a more general-purpose, usable way.

This of course assumes that the use of the protected field is for
something reasonably obvious. You weren't specific about how you'd use
it, but if you need to get the value for something you're going to do
to Y, but don't intend to ever do anything to the actual protected
member of X in the new instance then this suggestion wouldn't work.

Whether that's the case, I don't know. Your original post wasn't specific.

Also worth maybe nothing, but if for some reason defining a constructor
for X that takes an instance of X doesn't work, then I'd suggest that
there's a possibility your overall design isn't as clean as it could or
ought to be. I have found that in almost all situations, if I find
myself trying to write code to get around an apparent language
limitation, it's usually because I'm being hard-headed and not paying
attention to the strong hints the language is giving me to fix my
design. :)

Pete

Nov 22 '07 #9

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

Similar topics

2
by: spammy | last post by:
hi all, im trying to establish whether i have a race condition or critical section in the following. i have a dataaccess class that continually retireves a table from a sqlserver (which may be...
2
by: Sam Samnah | last post by:
I am building a custome control and I need to access the information in the hidden field (HtmlInputHidden HIH) so that when a button is pressed the information in the hidden fields value is...
3
by: Gustaf | last post by:
I have a class that I need to adapt to various scenarios. Some properties and methods will be needed in every case, while other's are unique for one case. So I made a base class, and a set of other...
8
by: Bruno Alexandre | last post by:
Hi guys, I'm using a session to save an ArrayList, so I do not read Database everytime user reload the page or enter the site (the Data is consistent for all entire session time when the user is...
1
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
7
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
3
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display an array of objects using a GUI. My instructions are that the CD class and it's sublcass don't need to change I just need to modify class CDInventory to include the GUI. I'm not...
1
by: twin2003 | last post by:
need help with inventory part 5 here is what I have to do Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next...
0
by: shahiz | last post by:
This the error i get when i try to run my program Error: Unable to realize com.sun.media.amovie.AMController@18b81e3 Basically i have a mediapanel class that initialize and play the media as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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...

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.