This example defines a class that represents a physical object in the real world -- a part.

Note the constructor, as well as a variety of methods.


Public Class Part 
     Private modelNumber As String
     Private partNumber As String 
     Private cost As Decimal

     ' no argument constructor to initialize an object of class Part
     Public Sub New ( )
          modelNumber = ""
          partNumber = ""
          cost =  0.0
     End Sub ' constructor

     ' This method sets the attributes of Part to the values passed
     Public Sub setPart ( ByVal mn As String, ByVal pn As String, ByVal c  As Decimal)
          modelNumber = mn
          partNumber = pn
          cost = c
     End Sub ' setPart

     ' This method returns the value of the modelNumber attribute
     Public Function getModel ( ) As String
          Return modelNumber
     End Function ' getModel

     ' This method returns the value of the partNumber attribute
     Public Function getPart ( )  As String
          Return partNumber
     End Function ' getPart

     ' This method returns the value of the cost attribute
     Public Function getCost ( )  As Decimal
          Return cost
     End Function ' getCost


     ' This method prints the values of the attributes
     Public Sub showPart ( )
          Console.Out.WriteLine("Model:" + vbTab + modelNumber )
          Console.Out.WriteLine ( "Part:" vbTab + partNumber )
          Console.Out.WriteLine ( "Cost:" +vbTab + CStr ( cost ) )
    End Sub 'showPart 

End Class


If this class is part of an application, an object can be instantiated and the results displayed using the get methods of the class.

Dim part1 As Part = New Part ( )                                  ' instantiate the part
part1.setPart ( "m1234", "p5678", CDec( 22.95) )   ' set the part values

part1.showPart ( )                                                           ' display attribute values on screen

Console.Out.WriteLine ( part1.getModel( ) )             ' alternate approach
Console.Out.WriteLine ( part1.getPart( ) )
Console.Out.WriteLine ( CStr ( part1.getCost( ) ) )

Notice that a specific method has to be supplied in order to get the value of individual data items.