Reputation: 6490
I have noticed for high demand in my projects for objects such as textboxes or buttons public.
Is there any problems by setting them public?
What does public, private, static really mean?
Upvotes: 1
Views: 753
Reputation: 63105
Access Modifiers (C# Programming Guide)
public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private
The type or member can be accessed only by code in the same class or struct.
protected
The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.
protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
There is no security risk as far as I know. But there may be better alternative approach to design your program
Upvotes: 3
Reputation: 72666
The best access modifier to give to a gui component when you want to access it directly is :internal (that is the default in VB.NET for example).
However you shouldn't give a public or internal modifier on a GUI control and you shouldn't access it directly, because the presentation layer and business logic layer should be kept separated in a well designed architecture ...
Upvotes: 1
Reputation: 9070
You should not, for a clean design.
You should in reality put the logic of your application outside forms! However, if you want to keep logic inside forms, you should at least expose them with public properties and methods, without giving direct access to form controls.
For example you can provide things like a method "EnableSave" or "QuitApplication" or "UpdateState".
Upvotes: 3
Reputation: 47058
public
, private
, etc are called access modifiers and determine the rules for which other code are allowed to access each member.
There is no technical problem of setting controls as public. But I would not recommend it. Having everything public is a good recipe for creating spaghetti code.
Keep all access to controls within your form and expose only a small set of public methods with a simple interface for external actors to access data and operations on the form.
Upvotes: 3
Reputation: 6553
Public, private and static deal with scope and what can talk to the objects / methods
Public
-> Other classes can create an instance of your class (assuming the class is public
) and call this object / method directly
Private
-> Other classes can create an instance of your class (assuming the class is public
) but can NOT access this object / method
Static
-> Other classes can directly access this object / method (assuming class is public
and method is public static
) such as: YourClassName.ObjectOrMethod
without having to create an instance of YourClassName
Upvotes: 2