getter and setter, get{} I need help understanding

Could someone help me out. I am trying to understand something. Look at the get methods in this class. (I think it is called a getter). This is confusing me, because I am working with data binding and in the example code, they never seem to explicitly call the get method to return the examNumber and examName. Instead these methods seem to be called implicitly somehow. Anyway, can someone help me to understand this more. Thank's in advance, Will

public class Exam

{private String examNumber;
  private String examName;
  public String ExamNumber
  {
    get 
      {
         return examNumber;
      }
   }

 public String ExamName

{
   get
      {
         return examName;
      }

}

public Exam(String strExamNumber,String strExamName)

{

examNumber = strExamNumber;

examName = strExamName;

//

// TODO: Add constructor logic here

//

}

}



Answer this question

getter and setter, get{} I need help understanding

  • BuffaloJ

    i want to give some additional information about properties in C#. C#  provides construct for accessing attributes called a property.Properties declared with public are available to code outside of the class (they can be accessed by client code). Properties declared as private are available only to code within the class. Moreover, The get accessor is used to place code that returns a value for the property when read by a client. If you remove the get accessor and its corresponding brackets, clients won't be able to read the value of the property.The set accessor  accepts a new property value from client code. If you remove the set accessor (and its corresponding brackets), clients won't be able to change the value of the property.
    e prope
    in your code you set the properties as well, but in the method you confused
    usage of properties
    for accessing attributes.As posted before,  u can access like that :
     
    string examName = exam.ExamName;
    string examNumber = exam.ExamNumber;


    ---------------------------------------

    Marie Myers, devBiz
    Helping Developers Build Better Software
    Meet VC: An exciting Vs.Net add-in




  • TimothyJ

    In the Exam class, ExamNumber and ExamName are known as "Properties".  The user of the class writes....

    Exam exam = ...;
    string examName = exam.ExamName;
    string examNumber = exam.ExamNumber;

    The compiler converts this to something like...

    Exam exam = ...;
    string examName = exam._getExamName();
    string examNumber = exam._getExamNumber();

    see http://msdn.microsoft.com/library/default.asp url=/library/en-us/csref/html/vclrfUsingProperties.asp




  • getter and setter, get{} I need help understanding