This article was previously published under Q307210
For a Microsoft Visual C# .NET version of this article, see 307368.
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.
This article assumes that you are familiar with the following topics:
Visual Basic .NET syntax or Visual Basic 2005 syntax
Create a new Console Application
Start Visual Studio .NET or Visual Studio 2005, and create a new Visual Basic Console Application project named ClassesAndObjects on your local computer.
Save the project.
Create a new class
On the Project menu, click Add Class.
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.
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 StringPrivate Stadium As String
Add two more Private fields, and provide initial field values as follows:
Private Wins As Integer = 0Private 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 = StEnd Sub
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 IfEnd 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 & ", play at " & Stadium & ": " & _ " W" & Wins & " L" & DefeatsEnd Function
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 LoseEnd Enum
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 IfEnd Sub
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 GetEnd 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).
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 SetEnd Property
Create and use an object
Display the code for Module1.vb in the Code View window.
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("San Francisco Giants", _ "Candlestick Park")
Add the following code to test the public methods on the object:
Display the current state of the object as follows:
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
Verify that it works
Build and run the application.
On the Debug menu, click Start Without Debugging to run the application.
Verify that the application displays the following information on the console:
Record: 0.5San Francisco Giants, play at 3Com Park: W1 L1