Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
Visual Basic Classes and Objects
Visual Basic Classes and Objects
In visual basic, Classes and Objects are interrelated. The class in visual basic is nothing but a collection of various data members (fields, properties, etc.) and member functions. The object in visual basic is an instance of a class to access the defined properties and methods.
Now, we will learn what are the classes and objects in visual basic and how to use it in visual basic applications with examples.
Visual Basic Class
In visual basic, Class is a data structure and it will combine the various types of data members such as fields, properties, member functions and events into a single unit.
Create Class in Visual Basic
In visual basic, classes can be created by using Class
keyword. Following is the declaration of class in a visual basic programming language.
Public Class users
' Properties, Methods, Events, etc.
End Class
If you observe the above syntax, we defined the class “users” using Class
keyword with public
access modifier. Here, the public
access specifier will allow the users to create an object for "users" class and in the class body, we can create the required fields, properties, methods and events to use it in our applications.
Now, we will see how to create a class in visual basic programming language with example.
Visual Basic Class Example
Following is the example of creating a class in visual basic programming language with various data members and member functions.
Public Class Users
Public id As Integer = 0
Public name As String = String.Empty
Public Sub New()
' Constructor Statements
End Sub
Public Sub GetUserDetails(ByVal uid As Integer, ByVal uname As String)
id = uid
uname = name
Console.WriteLine("Id: {0}, Name: {1}", id, name)
End Sub
Public Property Designation As Integer
Public Property Location As String
End Class
If you observe the above visual basic class example, we defined the “Users” class with a various data members and member functions based on our requirements.
Following is the detailed description of various data members which we used in the above visual basic class example.
If you observe the above image, we used various data members like access modifiers, fields, properties, methods, constructors, etc. in our visual basic class based on our requirements.
We will learn more about visual basic access modifiers, fields, properties, methods, constructors, etc. topics in the next chapters with examples.
Visual Basic Class Members
As discussed, a Class can contain multiple data members in a visual basic programming language. The following table lists a different type of data members that can be used in visual basic classes.
Member | Description |
---|---|
Fields | Variables of the class |
Methods | Computations and actions that can be performed by the class |
Properties | Actions associated with reading and writing named properties of the class |
Events | Notifications that can be generated by the class |
Constructors | Actions required to initialize instances of the class or the class itself |
Operators | Conversions and expression operators supported by the class |
Constants | Constant values associated with the class |
Indexers | Actions associated with indexing instances of the class like an array |
Finalizers | Actions to perform before instances of the class are permanently discarded |
Types | Nested types declared by the class |
We can use required data members while creating the class in visual basic programming language based on our requirements.
Visual Basic Object
In visual basic, Object is an instance of a Class and that can be used to access the data members and member functions of a class.
Creating Objects in Visual Basic
Generally, we can say that objects are the concrete entities of classes. In visual basic, we can create objects by using a New keyword followed by the name of the class like as shown below.
Dim user As Users = New Users()
If you observe the above example, we created an instance (user) for the class (Users) which we created in the previous section. Now the instance “user” is a reference to an object that is based on the Users. By using the object name “user” we can access all the data members and member functions of Users class.
Visual Basic Objects Example
Following is the example of creating objects in visual basic programming language.
Module Module1
Sub Main()
Dim user As Users = New Users("Suresh Dasari", 30)
user.GetUserDetails()
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
Public Class Users
Public Property UName As String
Public Property UAge As Integer
Public Sub New(ByVal name As String, ByVal age As Integer)
UName = name
UAge = age
End Sub
Public Sub GetUserDetails()
Console.WriteLine("Name: {0}, Age: {1}", UName, UAge)
End Sub
End Class
End Module
If you observe above example, we created a new class called “Users” with a required data members and member functions. To access Users class methods and properties, we created an object (user) for Users class and performing required operations.
When we execute the above visual basic program, we will get the result like as shown below.
This is how we can create and use the classes and objects in visual basic applications based on our requirements.