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
- Object-oriented concepts
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 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
- 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
Defeats = Defeats + 1
- 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" & Defeats
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
- 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
Defeats = Defeats + 1
- Define a ReadOnly property named Record as follows:
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).
Public ReadOnly Property Record() As Double
Dim played As Integer = Wins + Defeats
Return CType(Wins / played, Double)
- 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
Set(ByVal Value As String)
Stadium = Value
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", _
- Add the following code to test the public methods on the object:
- Add the following statements that exercise the properties of the object:
Console.Out.WriteLine("Record: " & sf.Record)
sf.Ballpark = "3Com Park"
- 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.)
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:
San Francisco Giants, play at 3Com Park: W1 L1
Article ID: 307210 - Last Review: October 16, 2012 - Revision: 5.0
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft Visual Basic .NET 2003 标准版
- Microsoft Visual Basic 2005
|kbvs2005swept kbvs2005applies kbhowtomaster KB307210|