Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have a private abstract class called TDSeq in which there are some abstract members and non-abstract members. There are 2 derived classes which it gets data from:- private class TDSeqBuy: TDSeq and private class TDSeqSell: TDSeq.

The members from the private abstract class that I am trying to access are private/public bools/doubles/integers.

The data flows from the derived classes through to the private abstract class by protected abstract name {get;}. After which the data is "moved" to the above mentioned private/public bool/doubles/integers.

I would like to access data for read-only purposes from the abstract class to a public class but do not know how to do that. Could someone please help?
Posted

A few thoughts:

1. You refer to a 'private abstract class' - are we talking about nested classes here? If not, I'm not sure what you mean by a private class. Can you explain?

2. Private members of a class can only ever be accessed from within that class. To access them within sub-classes (derived classes) declare them as protected. If you declare members as public, but declare the class as internal, you can only access them from other classes in the same assembly - and sub-classes can only be declared within the same assembly. Or make the class public and members protected internal - sub-classes can then be declared in other assemblies, but protected internal members can only be accessed by sub-classes within the same assembly. See http://msdn.microsoft.com/en-us/library/ms173121(v=vs.80).aspx[^] for further details.

3. Note also that sub-classes can't have greater accessibility than base classes (so can't have public sub-class of an internal class). Similarly for virtual / abstract members - e.g. override can't be public if base is protected.

4. All 'get' properties (without a corresponding 'set') are read only by definition - though your get code could of course have internal side effects that alter the object's state. If you need both read-only and read-write behaviour with different accessibility, consider having two separate properties.

Hopefully that makes some sense and helps a bit (and that I haven't got things too tangled).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Apr-11 15:50pm    
If you through out practical consideration, the question is formally very clear.
Technically, you can do it only via Reflection.
Please see my answer.
--SA
NuttingCDEF 26-Apr-11 16:04pm    
Agreed that you can achieve things through Reflection that may be impossible in other ways, but, as you imply, that seems like overkill for what should be a relatively straightforward coding problem.

But note that a private (or protected) class still won't wash (nested classes apart) - e.g. VS2010 gives compilation error "Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal"
Sergey Alexandrovich Kryukov 26-Apr-11 19:48pm    
We both certainly understand all that, thank you for this discussion.
--SA
I would like to access data for read-only purposes from the abstract class to a public class...

Use a public property (getter). And put the setter as private or protected:
C#
class TDSeq
{
    //MyPrivateInt can be read from other classes
    //but can be written only by this class
    public int MyPrivateInt { get; private set; }
    //MyProtectedInt can be read from other classes
    //but can be written by this class and derived ones
    public int MyProtectedInt { get; protected set; }
    ...
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Apr-11 15:48pm    
Practically, this is a way to go, my 5. It is not an access from outside code though. Technically, it can be done only via Reflection.
Please see my Answer.
--SA
If your member of the class is private, the only way to do it is Reflection. If you use Reflection, you don't need even a class to be public or internal — it could be protected.

This is relatively easy to do, but I'm not sure if you really need it, that's why I'm not offering any code samples. For a usual programming chores you don't need such stuff. If you're interested and ask a sensible question about Reflection technique, I'll answer.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900