Results 1 to 7 of 7

Thread: question about creating a class?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Location
    Atlanta Ga.
    Posts
    177

    question about creating a class?

    When creating a class, the attributes (member variables/instance variables) were declared as Private, which requires writing two special procedures to access the data. Why go to this extra trouble? Why not just declare these as Public?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Because the Property Let/Get/Set accessors allow you to control what you deem as valid data.
    VB Code:
    1. 'Public member
    2. Public SomeProp As Integer
    3.  
    4. 'In some other project
    5. mClass.SomeProp = (-1)

    If you only want a number between 1 and 10, then there is no way to let the user of the class know. But if you use accessors...
    VB Code:
    1. Private mSomeProp As Integer
    2.  
    3. Public Property Let SomeProp(ByVal i As Integer)
    4.  
    5.     If i < 1 or i > 10 Then Err.Raise 5 'Raise some error
    6.  
    7.     mSomeProp = i 'Otherwise set the local value
    8.  
    9. End Property
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    It also gives you the ability to perform other tasks internally based on the data.

    VB Code:
    1. Public Event NumberChanged()
    2.  
    3. Private mSomeProp As Integer
    4.  
    5.  
    6. Public Property Let SomeProp(ByVal i As Integer)
    7.  
    8.     If i < 1 or i > 10 Then Err.Raise 5 'Raise some error
    9.  
    10.     mSomeProp = i 'Otherwise set the local value
    11.     RaiseEvent NumberChanged()
    12.  
    13. End Property
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Using Public variables is generally considered bad practice. There's a couple of reasons for this, first of all if you use a Public variable you will not know when the value has been changed, however if you use a Property procedure the Let/Set procedure will be called when the property is changing value. This allows you to check if a proper value has been passed. Let's say that only a value between 1 and 10 is allowed, how can you then check if the new value is correct if you have declared a public Integer variable?

    In some cases certain requirements might also be filled before the user of your class may change one property value. For example if you build a class that allows the user to upload/download files from a FTP server you should not be able to pass a file name before a connection to the server has been made.

    Cheers,

    Joacim

  5. #5
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    Is it still considered bad practice if it doesn't matter what data is put into that variable? I know such situations aren't common, but I've seen it occur a few times before.

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by Hu Flung Dung
    Is it still considered bad practice if it doesn't matter what data is put into that variable? I know such situations aren't common, but I've seen it occur a few times before.
    If the data has no restrictions, then you might just want to use a UDT.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    Fanatic Member SkiNLaB's Avatar
    Join Date
    Jan 2002
    Location
    Sydney, Australia
    Posts
    747
    i need some examples of how to deal with invalid data....

    any ideas?

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