Results 1 to 9 of 9

Thread: *RESOLVED* Properties... why do we use them?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    *RESOLVED* Properties... why do we use them?

    What's wrong with having a public variable in a class? I'm not asking what are the advantages of having a property, but why shouldnt we have a public variable?
    I was analyzing a small project with Project Analyzer and it said:

    "Variables that aren't Private should be avioded in classes. Read/Write access to a Class's memeber variables should only be allowed via properties."
    why!?!
    Last edited by MrPolite; Sep 30th, 2002 at 10:41 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    The idea is aimed at application development teams really, where Joe can work on parts of an app with variables named X and Y, and Bob next door can work on different parts of the same app and not care if his variable names are unique from Joe's. Basically variable scoping is a development safety tool (at least imo).

  3. #3
    New Member The Developer's Avatar
    Join Date
    Sep 2002
    Location
    London, England, UK
    Posts
    11
    When developing large applications, it is unsafe to allow non-private variables in a class module.

    The value of the variable can be changed without your application knowing it has been changed. With properties, however, your application knows when a statement is setting or retrieving the value of a property and you can run any extra processing required before returning or setting the property value for the calling statement.

    Hope this clarifies this issue...

    Catch you later!
    The Developer

  4. #4
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    I always use public variables when I can be sure that I cannot create an error just by setting that variable to an unexpected value.

    I use properties when I want to do data validation, or run some specialized code when I want to change a value.

    Posted by Slow_Learner:

    The idea is aimed at application development teams really, where Joe can work on parts of an app with variables named X and Y, and Bob next door can work on different parts of the same app and not care if his variable names are unique from Joe's. Basically variable scoping is a development safety tool (at least imo).
    I dont see what variable scope has to do with it. Wouldn't Joe and Bob be working on different classes?
    Last edited by Hu Flung Dung; Sep 30th, 2002 at 08:11 PM.

  5. #5
    New Member The Developer's Avatar
    Join Date
    Sep 2002
    Location
    London, England, UK
    Posts
    11
    I agree 'Hu Flung Dung', when I read Slow_Learner's post, I just thought to myself..."what the f*** is this guy on about?!"

    Variable scoping has nothing to do with team development and besides, team programmers almost always work on seperate classes as 'Hu Flung Dung' already stated.

    Good luck...
    The Developer

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Like said before, to validate the data being set. Lets say you have a variable that you only want to be set to a number between 1 and 10. But it is an integer, and we know that there are a lot of numbers that the integer can hold. How do we control so that it only holds the numbers between 1 and 10, we use a property. When someone sets the propery to 100, you know it is out of range so you go ahead and set the value to 10 for the variable, thus illiminating errors later when processing code that thinks it is only going to get a number from 1 to 10. You can also choose to raise an error, raise an event in your class, or just disreguard the value. With a property, you get all those options. You don't get those with a public variable.

  7. #7
    New Member The Developer's Avatar
    Join Date
    Sep 2002
    Location
    London, England, UK
    Posts
    11

    Thumbs up

    Well explained hellswraith...with a nice example too!
    The Developer

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Thumbs up

    Originally posted by Hu Flung Dung
    I always use public variables when I can be sure that I cannot create an error just by setting that variable to an unexpected value.
    This explained it
    That's what I was doing. This stupid project analyzer was saying I'm evil and I should die because I'm a bad coder
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Originally posted by Hu Flung Dung
    II dont see what variable scope has to do with it. Wouldn't Joe and Bob be working on different classes?
    This just comes from me not knowing what I'm talking about.

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