Results 1 to 13 of 13

Thread: [RESOLVED] Why use Properties?

  1. #1

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Resolved [RESOLVED] Why use Properties?

    Hi,

    I was just thinking for a reason (good, sound reason), why do we need to implement Properties, or any kind of 'Get'ers and 'Set'ers.

    Just because it conforms to OOP way of programming is something not reasonable (at least to me). Suppose if the same question is put to you while you are being interviewed, what do you guys suppose could answer the question in succinct manner?

    How does it help? How does it help in future development? When we say it is secure, how is it secure? If it is efficient, how so?

    Hope I am being clear.

    Thank you
    Show Appreciation. Rate Posts.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Why use Properties?

    What else would you use?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Why use Properties?

    What harm in using the variable itself?

    Edit: I mean why can't we make variables public and use them?
    Show Appreciation. Rate Posts.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Why use Properties?

    In short, there's no reason... I don't know what the bindability of public vars is... I'm pretty sure they don't show up as "Properties" for custom controls (in the Properties window in the designer) nor am I sure how they are serializable (if they are) aaaaand, you can't make them read only.... or write only. And you can't run code when a variable is set like you can with a property. Properties make it easier to add validation and stuff.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Why use Properties?

    Thanks for the answer tg. Makes sense.

    I am keeping this thread open, just in case someone wants to add more points.

    Edit: Sorry, can't rep you. Need to spread some reputation points before. But thank you.
    Show Appreciation. Rate Posts.

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Why use Properties?

    I think the basic idea is that you can protect the data it is supposed to hold. It's one thing when you are developing everything, but if you are to develop one piece of code and then someone is to develop the other, the other guy might not know the limitations imposed on a certain variable. Let's say you have a property called Age and it's an Integer. If it was a variable the other dude could set it to -5 and screw up a lot of things. With a property you can check the value passed to see if it meets the required parameters. That way you ensured that the (private) variable can not be put into an invalid state.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why use Properties?

    It really comes down to one thing: fields are just data while properties can contain logic associated with that data. The two most common forms that logic takes are validation and events. There's also the fact that you can make a property ReadOnly. A field can be ReadOnly too, but then that literally means that its value can't change. A ReadOnly property can't be set externally but the value of its backing field can change internally. Basically the same reasons provided in every one of the many other threads that already exist on this topic.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Why use Properties?

    Thank you guys.

    Edit: Sorry JMC, can't add to your rep too. But thank you.
    Show Appreciation. Rate Posts.

  9. #9
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Why use Properties?

    Quote Originally Posted by techgnome View Post
    In short, there's no reason... I don't know what the bindability of public vars is... I'm pretty sure they don't show up as "Properties" for custom controls (in the Properties window in the designer) nor am I sure how they are serializable (if they are) aaaaand, you can't make them read only.... or write only. And you can't run code when a variable is set like you can with a property. Properties make it easier to add validation and stuff.

    -tg
    Use accessor methods instead example:

    c# Code:
    1. frmSummary summary = new frmSummary();
    2. //Assign accessor values
    3. summary.var ="Something";
    4. Summary.ShowDialog();

    then in the Second form

    c# Code:
    1. private type data variable
    2. public type var{
    3. set{
    4. data variable = value;
    5. }
    6. }

    Then in form_load (or whereever):

    c# Code:
    1. Set the object ie a textbox.text = variable;

    That is what I have been taught to do in order to make the program more secure.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why use Properties?

    Quote Originally Posted by Nightwalker83 View Post
    Use accessor methods instead example
    You just provided an example of using a property. Those accessor methods are the property getter and setter. A property bundles a get and set method and makes them appear to the outside world to behave just like a field, which is a nicer option than Java where you just write two methods with "get_" and "set_" prefixes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Why use Properties?

    Quote Originally Posted by jmcilhinney View Post
    You just provided an example of using a property. Those accessor methods are the property getter and setter. A property bundles a get and set method and makes them appear to the outside world to behave just like a field, which is a nicer option than Java where you just write two methods with "get_" and "set_" prefixes.
    Ah ok.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  12. #12
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Why use Properties?

    Here's a few reasons:
    http://blogs.msdn.com/b/vbteam/archi...han-aneja.aspx
    There are even more, such as being able to call events in the setter and the fact that properties show up in the designer where fields do not.

    I linked to this webpage at least 3 times already

  13. #13
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: [RESOLVED] Why use Properties?

    Want to know something funny, if you look at the IL emitted from your application you will see that the property you make is actually just a set of get()/set() functions when it boils down to it.

    Having said that, they are wired into the framework so don't go using get/set methods instead of properties.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width