What is the real point of Properties?

Hi

I would like to know what the real point of Properties. I read somewhere that they are used in classes to protect the fields. What does that mean??

I really dont get the point of Properties. Could you please provide me with info of why Properties are important.

Thanks,

Loco

Comments

  • : Hi
    :
    : I would like to know what the real point of Properties. I read
    : somewhere that they are used in classes to protect the fields. What
    : does that mean??
    :
    : I really dont get the point of Properties. Could you please provide
    : me with info of why Properties are important.
    :
    : Thanks,
    :
    : Loco

    I don't have a good grasp of the object-oriented programming approach yet. However, I think this will help....

    Class properties can be associated with characteristics. It describes the overall appearance, color, texture, etc. of a class. As an example, a class "dog" can have properties like color, size, furType, etc..
  • In object oriented programming (OOP), properties are like adjectives. They describe a noun, in this case an object. Example properties would be name, color, etc ...

    In OOP, classes should be black boxes meaning we should NOT care how it is implemented internally. Internal variables in the class are called fields. Problem is, you can make fields public ...

    [code]
    public class Person
    {
    public string Name;
    public int Age;
    }
    [/code]

    Name and Age are fields ... at this point a person who uses the class can assign anything within the data constraints they want ... like so ...

    [code]
    Person p = new Person();
    p.Name = "John Doe";
    p.Age = -30;
    [/code]

    This is not right, you shouldn't have a negative age. However, we can do nothing to stop someone from using your class this way. With properties, you can have the semantics of fields but with the flexibility of functions:

    (NOTE: The get portion is called when you try to read a property and the set portion is called hwen you try to assign a value to a property. In C# and in the set portion the "value" keyword is a pseudo-variable that contains the value that is being currently assigned.)

    [code]
    public class Person
    {
    private string _Name;
    private int _Age;

    // Name Property
    public string Name
    {
    get
    {
    return _Name;
    }
    set
    {
    if(value == null)
    value = string.Empty;

    _Name = value;
    }
    }

    // Age Property
    public string Age
    {
    get
    {
    return _Age;
    }
    set
    {
    if(value < 1)
    value = 1;

    _Age= value;
    }
    }
    }
    [/code]

    Even though there is more typing, the class has been made a little more robust and validates values as it is being set:

    [code]
    Person p = new Person();
    p.Name = "John Doe"; // Calls the Name property
    p.Age = -30; // Calls the Age property

    MessageBox.Show(p.Age); // Should display 1
    [/code]

    You can also have your properties throw exceptions alerting a developer that they used the property wrong.

    [code]
    public class Person
    {
    ...

    // Age Property
    public string Age
    {
    get
    {
    return _Age;
    }
    set
    {
    if(value < 1)
    throw new Exception("Age must be larger-than zero");

    _Age= value;
    }
    }
    }
    [/code]

    As a matter of design, properties should do very light tasks. If the property ends up spiraling into mounds of code, consider making it a function of the class instead.

    : Hi
    :
    : I would like to know what the real point of Properties. I read
    : somewhere that they are used in classes to protect the fields. What
    : does that mean??
    :
    : I really dont get the point of Properties. Could you please provide
    : me with info of why Properties are important.
    :
    : Thanks,
    :
    : Loco
  • : In object oriented programming (OOP), properties are like
    : adjectives. They describe a noun, in this case an object. Example
    : properties would be name, color, etc ...
    :
    : In OOP, classes should be black boxes meaning we should NOT care how
    : it is implemented internally. Internal variables in the class are
    : called fields. Problem is, you can make fields public ...
    :
    : [code]:
    : public class Person
    : {
    : public string Name;
    : public int Age;
    : }
    : [/code]:
    :
    : Name and Age are fields ... at this point a person who uses the
    : class can assign anything within the data constraints they want ...
    : like so ...
    :
    : [code]:
    : Person p = new Person();
    : p.Name = "John Doe";
    : p.Age = -30;
    : [/code]:
    :
    : This is not right, you shouldn't have a negative age. However, we
    : can do nothing to stop someone from using your class this way. With
    : properties, you can have the semantics of fields but with the
    : flexibility of functions:
    :
    : (NOTE: The get portion is called when you try to read a property and
    : the set portion is called hwen you try to assign a value to a
    : property. In C# and in the set portion the "value" keyword is a
    : pseudo-variable that contains the value that is being currently
    : assigned.)
    :
    : [code]:
    : public class Person
    : {
    : private string _Name;
    : private int _Age;
    :
    : // Name Property
    : public string Name
    : {
    : get
    : {
    : return _Name;
    : }
    : set
    : {
    : if(value == null)
    : value = string.Empty;
    :
    : _Name = value;
    : }
    : }
    :
    : // Age Property
    : public string Age
    : {
    : get
    : {
    : return _Age;
    : }
    : set
    : {
    : if(value < 1)
    : value = 1;
    :
    : _Age= value;
    : }
    : }
    : }
    : [/code]:
    :
    : Even though there is more typing, the class has been made a little
    : more robust and validates values as it is being set:
    :
    : [code]:
    : Person p = new Person();
    : p.Name = "John Doe"; // Calls the Name property
    : p.Age = -30; // Calls the Age property
    :
    : MessageBox.Show(p.Age); // Should display 1
    : [/code]:
    :
    : You can also have your properties throw exceptions alerting a
    : developer that they used the property wrong.
    :
    : [code]:
    : public class Person
    : {
    : ...
    :
    : // Age Property
    : public string Age
    : {
    : get
    : {
    : return _Age;
    : }
    : set
    : {
    : if(value < 1)
    : throw new Exception("Age must be larger-than zero");
    :
    : _Age= value;
    : }
    : }
    : }
    : [/code]:
    :
    : As a matter of design, properties should do very light tasks. If the
    : property ends up spiraling into mounds of code, consider making it a
    : function of the class instead.
    :
    : : Hi
    : :
    : : I would like to know what the real point of Properties. I read
    : : somewhere that they are used in classes to protect the fields. What
    : : does that mean??
    : :
    : : I really dont get the point of Properties. Could you please provide
    : : me with info of why Properties are important.
    : :
    : : Thanks,
    : :
    : : Loco
    :

    Thanks a lot, now it makes perfect sense why I need to use properties..

    loco
  • : : In object oriented programming (OOP), properties are like
    : : adjectives. They describe a noun, in this case an object. Example
    : : properties would be name, color, etc ...
    : :
    : : In OOP, classes should be black boxes meaning we should NOT care how
    : : it is implemented internally. Internal variables in the class are
    : : called fields. Problem is, you can make fields public ...
    : :
    : : [code]: :
    : : public class Person
    : : {
    : : public string Name;
    : : public int Age;
    : : }
    : : [/code]: :
    : :
    : : Name and Age are fields ... at this point a person who uses the
    : : class can assign anything within the data constraints they want ...
    : : like so ...
    : :
    : : [code]: :
    : : Person p = new Person();
    : : p.Name = "John Doe";
    : : p.Age = -30;
    : : [/code]: :
    : :
    : : This is not right, you shouldn't have a negative age. However, we
    : : can do nothing to stop someone from using your class this way. With
    : : properties, you can have the semantics of fields but with the
    : : flexibility of functions:
    : :
    : : (NOTE: The get portion is called when you try to read a property and
    : : the set portion is called hwen you try to assign a value to a
    : : property. In C# and in the set portion the "value" keyword is a
    : : pseudo-variable that contains the value that is being currently
    : : assigned.)
    : :
    : : [code]: :
    : : public class Person
    : : {
    : : private string _Name;
    : : private int _Age;
    : :
    : : // Name Property
    : : public string Name
    : : {
    : : get
    : : {
    : : return _Name;
    : : }
    : : set
    : : {
    : : if(value == null)
    : : value = string.Empty;
    : :
    : : _Name = value;
    : : }
    : : }
    : :
    : : // Age Property
    : : public string Age
    : : {
    : : get
    : : {
    : : return _Age;
    : : }
    : : set
    : : {
    : : if(value < 1)
    : : value = 1;
    : :
    : : _Age= value;
    : : }
    : : }
    : : }
    : : [/code]: :
    : :
    : : Even though there is more typing, the class has been made a little
    : : more robust and validates values as it is being set:
    : :
    : : [code]: :
    : : Person p = new Person();
    : : p.Name = "John Doe"; // Calls the Name property
    : : p.Age = -30; // Calls the Age property
    : :
    : : MessageBox.Show(p.Age); // Should display 1
    : : [/code]: :
    : :
    : : You can also have your properties throw exceptions alerting a
    : : developer that they used the property wrong.
    : :
    : : [code]: :
    : : public class Person
    : : {
    : : ...
    : :
    : : // Age Property
    : : public string Age
    : : {
    : : get
    : : {
    : : return _Age;
    : : }
    : : set
    : : {
    : : if(value < 1)
    : : throw new Exception("Age must be larger-than zero");
    : :
    : : _Age= value;
    : : }
    : : }
    : : }
    : : [/code]: :
    : :
    : : As a matter of design, properties should do very light tasks. If the
    : : property ends up spiraling into mounds of code, consider making it a
    : : function of the class instead.
    : :
    : : : Hi
    : : :
    : : : I would like to know what the real point of Properties. I read
    : : : somewhere that they are used in classes to protect the fields. What
    : : : does that mean??
    : : :
    : : : I really dont get the point of Properties. Could you please provide
    : : : me with info of why Properties are important.
    : : :
    : : : Thanks,
    : : :
    : : : Loco
    : :
    :
    : Thanks a lot, now it makes perfect sense why I need to use
    : properties..
    :
    : loco

    One other question. What is the purpose of the 'lock' statement on properties?

    Thanks again
    loco.
  • : One other question. What is the purpose of the 'lock' statement on
    : properties?
    :
    : Thanks again
    : loco.
    :
    iwilld0it,
    wow! that's a very nice explanation! thanks a lot!

    'lock' is used in multi-threaded applications. Its primary purpose is to prevent other threads from accessing or changing the value of the property while being used by the current thread.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories