Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Framework 3.5  »  Objects and the ListBox  »  Improving the Person Object
 »  Home  »  .NET Newbie  »  Objects and the ListBox  »  Improving the Person Object
 »  Home  »  Windows Development  »  Visual Basic 2005  »  Objects and the ListBox  »  Improving the Person Object
Objects and the ListBox
by Larry Blake | Published  06/05/2009 | Framework 3.5 .NET Newbie Visual Basic 2005 | Rating:
Improving the Person Object

 

(Don't you wish it was so easy to improve people?) Lets add a bit more code, and then talk about it.

Public Class Person
  Public FirstName, LastName, Title As String
  Private _salary As Integer

  Public Sub New(ByVal f As String, ByVal l As String)
    FirstName = f
    LastName = l
    Title = "Employee"
    _salary = 50000
  End Sub

  Public Sub New(ByVal f As String, ByVal l As String, ByVal t As String)
    FirstName = f
    LastName = l
    Title = t
    _salary = 50000
  End Sub

  Public Sub New(ByVal f , ByVal l As String, ByVal t As String, ByVal s As Integer)
    FirstName = f
    LastName = l
    Title = t
    _salary = s
  End Sub

  Public Sub GiveRaise()
    _salary *= 1.03
  End Sub

  Public ReadOnly Property Salary()
    Get
      Return _salary
    End Get
  End Property

  Public ReadOnly Property Info()
    Get
      Dim s As String = FirstName & " " & LastName & vbCrLf _
        & "Title: " & Title & vbCrLf _
         & "Salary: " & _salary.ToString()
      Return s
    End Get
  End Property
End Class

The first thing I want to point out in this code is the use of the words Public and Private. Think of Form1 as a user of the Person object. Public variables, methods, properties, etc. are usable in Form1 and visible to IntelliSense. Private ones are hidden. (Again, showing a small, concise interface is Abstraction.  The unnecessary details are hidden by Encapsulation.)

The private variable _salary is used in the public property Salary. A Property is similar to a regular variable, but it can be made read-only as I've done here. So in Form1, the first of the following statements would be valid.  The second would cause an error.

MessageBox.Show(p.Salary.ToString())
p.Salary = 60000 ' ERROR! 

Properties can also have more complex code, as for example in Info().


The improved Person code has several subroutines called New(), which introduces two concepts. First, New() is a special type of method called a Constructor. A Constructor is the code that executes when the object is instantiated.


The second concept is Overloading. Overloading is the reuse of a method name with a different set of parameters. You probably already use overloaded methods without thinking about it. Heres one example.

MessageBox.Show(p.FirstName)
MessageBox.Show(p.FirstName, "My name is")

A set of parameters is called a Signature. Signatures must vary in total count and/or variable type. (Changing just a variable name is not a new signature.) Again, IntelliSense is aware of the different signatures and will suggest possible syntax variations.

 

Sponsored Links