Public, Private or Friend Methods

Hello

I am starting in VB.Net from VBA.
I am building a 3 Tier Application. (Application, Business Logic DLL and Data Access DLL)

I have got things working with the Class Properties in the Business DLL as set to Public.

I have been reading more and it seems that I should be setting the Properties to Private?

Is this the Correct way to go? - If so How do I Access the Data from the Application?
Do I have a Get Method for every Property in the Class?
And a Corresponding Set Method?

If I do have Get and Set Methods for each Property (50+) does that slow the Application down?
p-platerAsked:
Who is Participating?
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.

Najam UddinCommented:
Public in Business dll is okay, you shouldnt set them to private else you would not be able call them from application.

Public Property Name As String

Open in new window

is all right if you dont have any custom logic in get or set. This style is called Auto-Implemented Properties. Get and set are also required if you want to restrict write or read on your property, ex

Public ReadOnly Property Count As Integer
	Get
	    Return 500
	End Get
    End Property

Open in new window



This is a read only property

Let me know if you need more detail. This will help you more
0

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
käµfm³d 👽Commented:
Get and set are also required if you want to restrict write or read on your property
That depends on the version of VB in use.  For example, in VS 2015 you can do:

Public ReadOnly Property Count As Integer = 500

Open in new window

1
p-platerAuthor Commented:
Why is there do much online and in tutorials saying to never set properties to public Or if you do use public then make them read only?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

käµfm³d 👽Commented:
I  believe you're thinking of fields, which are basically class-level variables. You don't want fields to be exposed outside of your class because you lose control over how they are used. Instead, you create properties which consumers of your class use to interact with those fields. Properties can have validation logic which prevent incorrect values from being stored within the field. Properties can also raise events to let listeners know that something important has happened with the field (well, really something has happened with the property, but since properties are really just front-ends for your fields...). Fields should (most often) be public because you do want consumers of your class to be able to see them. Fields are not public because you don't want consumers of your class to be able to see the fields (for the reasons I mention above).

As for read-only, if no one can update the value of the field, then you don't have to worry about side effects from consumers updating the value. You can only ever read the value, so it never changes. int.MinValue and int.MaxValue are two examples of read-only fields which are public.
0
p-platerAuthor Commented:
OK

I've done some more reading, am I correct in saying.
When you declare a property without a get and set method one is created behind the scenes with it's own field?

So if I need to add some validation latter then all I have to do is create the get and set methods (This should work without any other code changes?)
0
käµfm³d 👽Commented:
When you declare a property without a get and set method one is created behind the scenes with it's own field?
Yes.

So if I need to add some validation latter then all I have to do is create the get and set methods (This should work without any other code changes?)
Yes.
0
p-platerAuthor Commented:
Thanks very much
0
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
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.