Results 1 to 5 of 5

Thread: A class, it's method and self-referencing

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    A class, it's method and self-referencing

    I created this class and a method. the method is used to submit data to a DB. that data is actually an object created from the same class. W

    What I'd like to know is, is it considered good practice to use the Private variables(fields) or should you still use the object.properties? ex:

    VB Code:
    1. public class Loan
    2.  
    3. private strName as string 'the field
    4.  
    5. public property Name() as string
    6. get
    7. return strName
    8. end get
    9.  
    10. set
    11. ..
    12. ..
    13. end set
    14.  
    15. public function SubmitData() as something
    16.  
    17. send strName to DB 'OR
    18. send LoanObject.Name to DB
    19.  
    20. end function

    That's pretty close anyway. I am currently sending the variables' data and NOT sending the data as properties. Which is more efficient? Which is better?

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    The answer is.... it depends.

    If you are just returning the variable, what would it matter? It is probably more efficient to just use the variable, but that efficiency isn't going to be noticed at all I would think.

    I tend to use the properties though for readability in my code. The other reasons would be if you are doing something dynamic in the property to return a modified value. You may want that instead of what is in the property itself.

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    To help visualize what I mean more:

    VB Code:
    1. public class Loan
    2.  
    3. private objLoanParent as LoanParentObject
    4.  
    5. public property ParentID() as integer
    6. get
    7. return objLoanParent.ID
    8. end get
    9. End Property
    10.  
    11. public function SubmitData() as something
    12.  
    13. send objLoanParent.ID to DB
    14. 'OR
    15. send Me.ParentID to DB
    16.  
    17. end function
    18. End Class
    Or better yet:
    VB Code:
    1. public class Loan
    2.  
    3. private objLoanParent as LoanParentObject
    4.  
    5. public property NextParentID() as integer
    6. get
    7. return objLoanParent.ID + 1
    8. end get
    9. End Property
    10.  
    11. public function SubmitData() as something
    12.  
    13. send objLoanParent.ID + 1 to DB
    14. 'OR
    15. send Me.NextParentID to DB
    16.  
    17. end function
    18. End Class

  4. #4
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Like hellswraith, I also tend to use properties. IMHO it makes the code more extensible (for cases where you need to do something to the data before you return it, etc.).

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    cool. thanks for the suggestions guys! I tried a little bit of both and like the readability of using the properties too. PLUS, I found out that if you're inheriting from another class, you kinda HAVE to use properties

    Thanks again!

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