Home | Work & Business | Computer Software | Visual Basic | How to Create Visual Basic Objects

How to Create Visual Basic Objects

by Lysis
  • Overview

    Creating object classes in Visual Basic gives programmers the ability to develop reusable code. Objects are filled with properties, methods, and constructors that are used in applications. Creating objects is accomplished in the Visual Studio workspace as a new class. Once the class is created, the methods and properties are programmed.
 
  • Step 1

    Right-click the project name and select "Add Class" from the Visual Studio workspace. This opens a new class file to code the object's properties and methods.
  • Step 2

    Create private variables. These variables are only accessible to the class and cannot be used in other parts of the code: Private CustomerId As String Private CustomerName As String
  • Step 3

    Create a constructor that sets the variables created in step 2. This constructor is used when the code instantiates the object class: Public Sub New(ByVal myCustId As String, ByVal myCustName As String) CustomerId = myCustId CustomerName = myCustName End Sub
  • Step 4

    Create methods for the object class. This gives a way for developers to call functions that process code specific for the class. Methods are the "verbs" of classes. They process an action. The following code is an example of a method for the customer class: Public Sub CreateNewId() myCustomerId = myCustomerId + 1 //new customer id End Sub
  • Step 5

    Create properties for the object. Properties are the "nouns" of object classes. Properties set a part of the object like customer name, ID, or address. The following code creates a property for the customer class that is returned: Public ReadOnly Property CustomerID As Integer Get Dim CustomerID As Integer = myCustomerID Return myCustomerID End Get End Property
  • 3

References & Resources