Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That's the way to use it, except for the way you're setting it, possibly. Instead of accessing the member variable, you may want to use the property from within the class, so you can use uniform rules regarding each member variable. This is the primary advantage to using properties, is to bring the access and setter logic all into one place. It really depends on your specific needs whether or not you want to set it using the Property or not. Note though, that in the constructor you want to be very careful when calling the Property, as you may or may not be relying on other parts of the class being initialized which would not be done yet if accessed through the constructor. Again, this depends on your specific implementation.</p> <p>It is also a little cleaner to use:</p> <pre><code>myObject.Property1 = "Test String"; Console.WriteLine(myObject.Property1); </code></pre> <p>Than what you see in some other languages:</p> <pre><code>myObject.setProperty1("Test String"); System.out.writeln(myObject.getProperty1()); </code></pre> <p>Here's a case where you can encapsulate some logic:</p> <pre><code>public int Order { get { return m_order; } set { // Put some rules checking here. Maybe a call to make sure that the order isn't duplicated or some other error based on your business rules. m_order = value; } } </code></pre> <p>Another way they're useful would be like this:</p> <pre><code>public int Order { get; private set; } </code></pre> <p>And now you've got an auto-implemented property with backing member variable that can only be set inside the class but read everywhere else.</p> <p>Finally, if you need to control the logic, you can write this:</p> <pre><code>public int Order { get { return m_order; } protected set { // Again, you can do some checking here if you want... m_order = value; // You can also do other updates if necessary. Perhaps a database update... } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload