Do controls have to be private?

G

Guest

Hi

I am using VC++.net 2003. Whenever I place a control on a winform, it is declared as private. Then I have to write set and get methods to access these controls from a calling form. Why are they declared private instead of public? Does it hurt to change them to public? It seems like it would be much faster to set/get properties directly

Thank you
Joe
 
D

Doug Forster

Hi Joe,

Well it would break your class encapsulation. IMO this would be a great
first step towards creating a bunch of spaghetti objects if thats what you
want.

Cheers

Doug Forster

Joe Thompson said:
Hi,

I am using VC++.net 2003. Whenever I place a control on a winform, it is
declared as private. Then I have to write set and get methods to access
these controls from a calling form. Why are they declared private instead
of public? Does it hurt to change them to public? It seems like it would
be much faster to set/get properties directly.
 
J

Jay B. Harlow [MVP - Outlook]

Joe,
As Doug stated, it would break encapsulation.

I would recommend only adding a Get property, as generally you really do not
want classes outside the form to change the instance of the control. Also I
rarely expose the control itself as a property, normally I only expose one
or two properties of the control as properties of my form.

For example, my form would have a get/set String property for FirstName,
that modified the Text property of the FirstNameTextBox control. The other
properties of the FirstNameTextBox I want to be under 100% control of the
form itself.

Hope this helps
Jay

Joe Thompson said:
Hi,

I am using VC++.net 2003. Whenever I place a control on a winform, it is
declared as private. Then I have to write set and get methods to access
these controls from a calling form. Why are they declared private instead
of public? Does it hurt to change them to public? It seems like it would
be much faster to set/get properties directly.
 
G

Guest

Hi

Thanks for your replies. I understand what you're saying. The only thing I want to do is to set values before I show the dialog and get them back after OK is selected. Some things are textboxes, radio buttons, numericupdowns, etc. I guess I'm just getting lazy. In Borland C++ Builder I believe the controls are "published" which allowed access from outside

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top