المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : A Question About Class Properties



C# Programming
12-06-2009, 02:30 AM
I'm having trouble grasping the value of properties and the get/set accessors in C#. In my reading, typical examples are structured as:

public class SomeClass()
{
private int myProperty;
public int MyProperty
{
get {return myProperty;}
set {myProperty = value;)
}
}

I'm using this feature for the first time in a form that is created by a main form, and every data member collected by the child form is to be returned to the main form. The VS 2008 Designer did all the hard work for me, for which I'm duly grateful, but it created all the form data members as private, and the parent form can't access them. In keeping with what seems to be the preferred design philosophy of C#, I'm busy filling in public get/set accessor pairs for each member so the parent form can retrieve the data, but it seems kind of silly to me. Why not just make all the data members public and be done with it?

I realize this is quite a simple example, as it's my first serious attempt to make something useful with C#, but I really don't see the value of this construct. Can someone explain or provide examples of situations that would make this useful?http://www.barakasoft.com/script/Forums/Images/smiley_confused.gif

"A Journey of a Thousand Rest Stops Begins with a Single Movement"