Microsoft KB Archive/307210

= How to create classes and objects in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 307210

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q307210





For a Microsoft Visual C# .NET version of this article, see 307368.



SUMMARY
Visual Basic .NET or Visual Basic 2005 are object-oriented programming languages. You define classes to represent the types in your application, and you create objects as instances of these classes.

In Visual Basic .NET or Visual Basic 2005, a class can contain fields, methods, and properties. This article demonstrates how to create a new class to represent a baseball team. In this article, you will define fields, methods, and properties for the class. You will then create an object of this class type and make use of its methods and properties.

Requirements
This article assumes that you are familiar with the following topics:
 * Visual Basic .NET syntax or Visual Basic 2005 syntax
 * Object-oriented concepts

Create a new Console Application

 * 1) Start Visual Studio .NET or Visual Studio 2005, and create a new Visual Basic Console Application project named ClassesAndObjects on your local computer.
 * 2) Save the project.

Create a new class

 * 1) On the Project menu, click Add Class.
 * 2) In the Add New Item dialog box, type BaseballTeam.vb in the Name text box, and then click Open.

Note In Visual Studio 2005, click Add.
 * 1) Examine the code for the new class in the Code View window.

Define fields and constructors
  Add two Private fields to the BaseballTeam class as follows: Private Name As String Private Stadium As String   Add two more Private fields, and provide initial field values as follows: Private Wins As Integer = 0 Private Defeats As Integer = 0   Add a Public constructor for the class to initialize the name of the team and the stadium: Public Sub New(ByVal Nm As String, ByVal St As String) Name = Nm  Stadium = St End Sub 

Define methods
  Add a Public method to the class as follows: Public Sub PlayGame(ByVal RunsFor As Integer, _                   ByVal RunsAgainst As Integer) If RunsFor > RunsAgainst Then Wins = Wins + 1 Else Defeats = Defeats + 1 End If End Sub   All classes in Visual Basic .NET or Visual Basic 2005 ultimately inherit from a base class named Object. This class defines common capabilities for all classes. An example is the ToString method, which returns a string representation of the class state. Override this method in your BaseballTeam class as follows: Public Overrides Function ToString As String Return Name & &quot;, play at &quot; & Stadium & &quot;: &quot; & _ &quot; W&quot; & Wins & &quot; L&quot; & Defeats End Function </li></ol>

Define overloaded methods
  Visual Basic .NET and Visual Basic 2005 support overloaded methods. Overloaded methods are methods that have the same name but a different signature. Define an Enum in the BaseballTeam class as follows: Public Enum Result Win Lose End Enum </li>  To define an overloaded version of the PlayGame method that takes the result of a game as its parameter, create another PlayGame method as follows: Public Sub PlayGame(ByVal Res As Result) If Res = Result.Win Then Wins = Wins + 1 Else Defeats = Defeats + 1 End If End Sub </li></ol>

Define properties
  Define a ReadOnly property named Record as follows: Public ReadOnly Property Record As Double Get Dim played As Integer = Wins + Defeats Return CType(Wins / played, Double) End Get End Property The Get method returns the baseball team's playing record (for example, if the team wins 10 games and loses 10 games, its record is 0.5). </li>  To allow the baseball team's Stadium field to be read or changed, add a read/write property named Ballpark as follows: Public Property Ballpark As String Get Return Stadium End Get Set(ByVal Value As String) Stadium = Value End Set End Property </li></ol>

Create and use an object
 Display the code for Module1.vb in the Code View window.</li>  In the Main method, use the New operator to create a BaseballTeam object. Assign the object reference to a local BaseballTeam variable as follows: Dim sf As BaseballTeam = New BaseballTeam(&quot;San Francisco Giants&quot;, _                                         &quot;Candlestick Park&quot;) </li>  Add the following code to test the public methods on the object: sf.PlayGame(7, 2) sf.PlayGame(BaseballTeam.Result.Lose) </li>  Add the following statements that exercise the properties of the object: Console.Out.WriteLine(&quot;Record: &quot; & sf.Record) sf.Ballpark = &quot;3Com Park&quot; </li>  Display the current state of the object as follows: Console.Out.WriteLine(sf.ToString) </li>  Set the object reference to Nothing to indicate that you no longer need the BaseballTeam object. This makes the object available for garbage collection. (Note that Visual Basic .NET and Visual Basic 2005 do not have a Delete operator.) sf = Nothing </li></ol>

Verify that it works
 Build and run the application.</li> On the Debug menu, click Start Without Debugging to run the application.</li> <li> Verify that the application displays the following information on the console: Record: 0.5 San Francisco Giants, play at 3Com Park: W1 L1                    </li></ol>

<div class="references_section">