You are currently offline, waiting for your internet to reconnect

How to create classes and objects in Visual Basic .NET or in 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.
  3. Examine the code for the new class in the Code View window.

Define fields and constructors

  1. Add two Private fields to the BaseballTeam class as follows:
    Private Name As StringPrivate Stadium As String					
  2. Add two more Private fields, and provide initial field values as follows:
    Private Wins As Integer = 0Private Defeats As Integer = 0					
  3. 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					

Define methods

  1. 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					
  2. 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

  1. 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					
  2. 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 properties

  1. 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).
  2. 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

  1. Display the code for Module1.vb in the Code View window.
  2. 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")					
  3. Add the following code to test the public methods on the object:
    sf.PlayGame(7, 2)sf.PlayGame(BaseballTeam.Result.Lose)					
  4. Add the following statements that exercise the properties of the object:
    Console.Out.WriteLine("Record: " & sf.Record)sf.Ballpark = "3Com Park"					
  5. Display the current state of the object as follows:
    Console.Out.WriteLine(sf.ToString())					
  6. 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

  1. Build and run the application.
  2. On the Debug menu, click Start Without Debugging to run the application.
  3. Verify that the application displays the following information on the console:
    Record: 0.5San Francisco Giants, play at 3Com Park:  W1 L1					
Properties

Article ID: 307210 - Last Review: 10/16/2012 02:43:00 - Revision: 5.0

Microsoft Visual Basic .NET 2002 Standard Edition, Microsoft Visual Basic .NET 2003 标准版, Microsoft Visual Basic 2005

  • kbvs2005swept kbvs2005applies kbhowtomaster KB307210
Feedback