About properties

Can someone please explain to me in plain english what the following code is saying

public string Result

{

get {

if

(_userAnswer == _correctAnswer)

{

return "Correct";

}

else {

return "Incorrect";

}

}

} thanks



Answer this question

About properties

  • Chrismogz

    Hi, IS dude

    These codes is in a certain class, "Result" is a property of the class,   "_userAnswer" and  "_correctAnswer" are two attributes of the class. The property "Result" is to judge if the "_userAnswer" attribute is equal to "_correctAnswer" attribute, if equals, then property "Result" gonna to be "Correct", and  if different, property "Result" gonna to be "Incorrect".

    For example (the whole original class may be like this):

            public class Judgement< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

            {

                ......//some other methods, attributes, properties and so on...

                private int _userAnswer;

                private int _correctAnswer;

                public string Result

                {

                    get

                    {

                        if (_userAnswer == _correctAnswer)

                        {

     

                            return "Correct";

     

                        }

                        else

                        {

                            return "Incorrect";

                        }

                    }

                }

            }

    These are all about the Class structure issues.

    If you are also not clear about that reference to: http://msdn.microsoft.com/library/default.asp url=/library/en-us/csref/html/vcwlksimplepropertiestutorial.asp

    BR

     



  • god_of_coolness

    Unlike member variables, properties can be used to compute things that syntactically appear like data members (not methods) when you use them. For example, you could make a property that adds those two members of yours together and returns the result. From outside the class, it would appear as if you are accessing a single member because you'd write it like:

    something = classObject.propertyName;

    yet propertyName (in this example) would be a member function not a data member. You couldn't do the same thing using member variables without adding a new member to your class to contain the sum, and also writing code everywhere either of the members it relys on are changed in order to keep its value correct.

    They also give you total control over validation of class data. If you use a public data member, then anywhere outside the class you can change that data to any value in the domain of the associated type.

    class SomeClass
    {
      public int data;
    }

    // elswhere in the program
    SomeClass object = new SomeClass();
    object.data = 1023834; // You can assign any integer value you want here.

    But if you need to ensure that the data member only ever contains values in a particular range, you have no control over it by using a public member variable. In my example above, you can set object.data to any value at all -- even one that would be considered invalid for the SomeClass class.

    Use a public property function and make the data member private and you gain total control over what values can be assigned to the data member from outside the class.

    class SomeClass

      private int data;
     
      public int Data
      {
        get { return data; }
        set
        {
          // make sure data can only be set to a value in the range 0...256
          if( (value < 0) || (value >= 256)) throw SomeException;   else data = value;
        }
    }

    // elswhere in the program
    SomeClass object = new SomeClass();
    object.data = 1023834; // compile error - cuz data is a private member of SomeClass.
    object.Data = 1023834; // runtime error - exception thrown cuz 1023834 is not a valid value.
    object.Data = 22; // this works because the value is in the legal range

    The result is you get a class with a data member which appears to be a public one, but cannot be set to an invalid value from outside the class.

    The exact same thing can be accomplished by using two member functions (say GetData and SetData) that users of the class can call to get or set the value of the private data member. You could then control the value of the thing, but you'd hafta put in a parameter list when you call it, and it takes two member functions with differing names to do it. The idea with properties is to make them look syntactically like data members when you use them.

     


  • Jaime Stuardo

    hi, this explanation isn't exactly clear. Can you please tell me what this code is trying to achieve

    Thanks


  • akjal

    oh, IS dude

    It seems to be quite nature for you to regard properties as "strange" attributes, while actully they are special method called accessors, which provide a flexible mechanism to read, write, or compute the values of private fields.

    BR



  • Shawn Wildermuth - MVP &amp;#40;C&amp;#35;&amp;#41;

    This is a property function. It gives the class a read-only property called Result that is a string datatype. When you access the Result property of the class for read access, it runs the "get" function part of the Result property. The "get" function then compares the values of the two variables. If they are equal, the function returns "Correct" as the value of the Result property. If they are different, it returns "Incorrect" instead.

    So for example if you have a class like this:

    public class MyClass

      public int _userAnswer;
      public int _correctAnswer;
     
      public string Result
      {
        get
        {
          if( _userAnswer == _correctAnswer)
          { return "Correct"; }
          else
          { return "Incorrect"; }
        }
      }
    }


    then you can write the following bit to determine a string result based on the values of the member variables _userAnswer and _correctAnswer (which have been previous set to some value elsewhere in the program):

    MyClass myobject = new MyClass();
    // code goes here that assigns values to the _userAnswer and _correctAnswer members
    // of myobject
    string myresult = myobject.Result;

    When this bit finishes, the variable myresult contains the string "Correct" if the MyClass members were the same, or "Incorrect" if they were different.

     


  • Vyatsek

    Thank you all. Actually I don't have any problem with understanding these examples because the syntax bodies are quite simple. I have seen some really complex property bodies and I guess what I am not sure is whether you could just view properties as another way of 'defining' the variables


  • DineshSharma

    if variable _userAnser is equal to the variable _correctAnswer then the result given is "Correct" else it gives "Incorrent", and assigns the result to the string Result

  • About properties