C# Class Accessibility

Dear Experts

In User Control, I have the following Class and it's instantiation:
public class tblPropsType
{
        public List<string> _arrH;
        public bool _blnV = false;
        public string _posV;
        public string _mthV;

        public List<string> arrHeaders
        {
            get { return _arrH; }
            set { _arrH = value; }
        }

        public void tblColView(bool Enabled, string Position, string Method)
        {
            _blnV = Enabled;
            _posV = Position;
            _mthV = Method;
        }
}

public tblPropsType tblProps = new tblPropsType();

Open in new window

And I'm using tblProps to set the variables from Parent Page, however, since the underscored are public, they show in the intellisense in Parent Page. If I remove the keyword public, variables will be inaccessible due to their protection level.

My question is, how can I hide the underscored variables from Parent Page and keep them accessible to User Control ?

Many thanks indeed.
Faraj1969System AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
I think that is NOT possible as you describe it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi Faraj1969;

Create your class as shown below. Then you can access your private fields through the public properties.

    public class tblPropsType
    {
        private List<string> _arrH;
        private bool _blnV = false;
        private string _posV;
        private string _mthV;

        public List<string> arrHeaders
        {
            get { return _arrH; }
            set { _arrH = value; }
        }

        public bool BlnV 
        { 
            get { return _blnV; }
            set { _blnV = value; }
        }
        public string PosV
        {
            get { return _posV; }
            set { _posV = value; }
        }

        public string MthV
        {
            get { return _mthV; }
            set { _mthV = value; }
        }


        public void tblColView(bool Enabled, string Position, string Method)
        {
            _blnV = Enabled;
            _posV = Position;
            _mthV = Method;
        }
    }

Open in new window

Faraj1969System AdministratorAuthor Commented:
@AndyAinscow: I believe you're right, what I actually want my not work as described.

@Fernando, your solution would work, but this again will expose BlnV, PosV and MthV to Parent Page.

How do we usually do this ? I mean what's the ideal solution for it ?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

AndyAinscowFreelance programmer / ConsultantCommented:
In C++ there is a 'friend' specifier which does control availability on a class basis - ideal for you.  This functionality does not exist in C#.  Anything that is public is available everywhere, so you can not restrict availability on the level you would like.  All that the suggestion from Fernando does is hide the detail (but does allow a difference between reading and writing the variable) but still maintains availability by a level of indirection.
Fernando SotoRetiredCommented:
Yes, but access is controlled by the public property.
AndyAinscowFreelance programmer / ConsultantCommented:
>>How do we usually do this ? I mean what's the ideal solution for it ?

Basically one takes note, writes code and lives with this.  As I said initially what you want the way you want it isn't possible.
Faraj1969System AdministratorAuthor Commented:
Many thanks Experts, at least now I know its a dead end to what I was trying to do, and to do in different way.

Appreciated.
Fernando SotoRetiredCommented:
Hi Faraj1969;

There is a way of doing this. Most likely you created the UserControl inside the same project that of the Windows Form. What you need to do is to create a new Windows Control Library project in the same solution as your Windows application or in a separate solution. Then you can rename the UserControl1.cs in the new project to something more meaningful. Then copy and paste your current user control into the new one, being careful that the class header still has inheritance from : UserControl on the line. Then change all the access modifiers for the fields you do not want to share with the other application, Windows Form, to internal. In the application you want to use the UserControl add a reference to the UserControl project. Do not forget to remove/delete the original UserControl from the Windows Form application. Do a Build Clean then recompile. Then you should be good to go.
Faraj1969System AdministratorAuthor Commented:
Fernando it's a web page, so I'm going through another direction. By the way, by mistake I selected two answers from Andy; I wanted to split the points. So I filed a Request for Attention, and hopefully a Moderator will resolve the issue, I apologize for the inconvenience.
Fernando SotoRetiredCommented:
Not a problem Faraj1969.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.