Object oriented programming features in vb.net

Ratings:
(4.3)
Views: 1486
Banner-Img
Share this blog:

Object-Oriented Programming Features:

1) Data Abstraction or Data hiding:

It is used to provide relevant information and used to hide the relevant information from the user.

To achieve: Class or structures should be used  

2) Polymorphism:

Whenever an entry can change its definition based on the values of the argument supplied for that entity then it is said to be supporting polymorphism.

To achieve: The overloading concept is used to achieve polymorphism.  

Note:

If any language supports the above 3 features then it is said to be an object-based programming language.  

Ex: VB 6.0  

Inheritance:

It is used to reuse or to redefine the existing class definition.  

Note:

If any language supports the above all features then it is said to an object-oriented programming language.  

Ex: VB.Net  

Properties:

It is used to set or get the values from the private or protected members of the class  

Syntax:

Public [Read only Write only] property propertyName () as data type

Set (by val value as data type) [property should always be public]

Variable = value

End set

Get

Return variable

End get

End property

Value in the above syntax is a keyword which maintains the value assigned by the user at the runtime.

  • In order to define Read-only property then the “get” block should be used with the “Read-only” keyword.
  • In order to define the Write-only property then the “set” block should be used with the “Write only” keyword.
Product [class]

= product Id

= product Name

= price

Sub new (Pid, Pname, Price) [we can’t change pid, pname, price can be fixed or re written]

Read Only

Product Id

Product Name

Read & write only

Price

Open new app

‘‘Demo on properties

Imports system

Module property ‘‘Demo

Class Product

Private - Product Id As integer

Private – Product Name As a string

Private – Price As Double

Sub new (By vale pid As integer, By val pname As string, By val price As Double)

Me._Product Id = Pid

Me._Product Name = Pname

Me._Price = Price

End sub

Public Read-only property Product Id as an integer

Get

Return Me._Product Id

End Get

End property

Public Read-only property Product Name as String

Get

Return Me._Product Name

End Get

End property

Public Read-only property Price as Double

Get

Return Me._Price

End Get

End property

End class

Class ShowRoom

Public shared sub main ()

Dim tv As the new product (101, “LG Golden Eye”,13500)

tv. As productName = “Sekhar Diamond Eye” error since it

is read only

tv.price = 12000

Console.WriteLine (“Product Id: {0}”, tv.Product Id)

Console.WriteLine (“Product Name: {0}”, tv.ProductName)

Console.WriteLine (“Price: {0}”, tv.Price)

End sub

End class

Imports system

Module Property ‘‘Demo2

Class Product

Private – First Name As String

Private – Last Name As String

Public property First Name As String

Set (By val value As string)

Me._First Name = value

End set

Get

Return Me._First Name

End Get

End property

Public property Last Name As String

Set (By val value As string)

Me._Last Name = value

End set

Get

Return Me._Last Name

End Get

End property

End class

Sub main ()

Dim uname As New Name

uname.FirstName = “Shekar”

uname.LastName = “Srinivas”

Console.WriteLine (“user name: {0} {1}”,

uname.FirstName, uname.LastName)

End sub

Execute  
Inclined to build a profession as VB.Net Developer? Then here is the blog post on, explore VB.Net Training

Auto Implicit:

It is used to define a read and write property implicitly.  

Syntax:

Public property PropertyName As Datatype  

Note:

It is a feature introduced from VB.Net 4.0  

New Module:

Auto Implicit property ‘‘Demo.vb

Imports system

Module Property ‘‘Demo2

Class Name

Public property First Name As String

Public property Last Name As String

End class

Sub main ()

Dim uname As New Name

uname.FirstName = “Shekar”

uname.LastName = “Srinivas”

Console.WriteLine (“user name: {0} {1}”,

uname.FirstName, uname.LastName)

End sub

End Module

The above two programs are the same and give the same output Sekhar Srinivas. In the first program, we write the code manually, and in the second program code is generated by technology (.Net). The second program works in only VB.Net 4.0. In the 1st program, the data which is private has been published for use by means of “Property”. In the second program declaring of data types in private & published for using these by auto implicit keyword in a single line.

C: Start notepad f.vb (first program)

C: vbc f.vb; o/p shekar srinivas

C: f.vb

C: Start notepad s.vb

C: vbc s.vb; C: s.vb

To know code after execution is C: Mdasm f.exe    

Methods:

It is used to modulate the flow of the application.  

Advantage:

  • Easy debugging [By writing logic in several methods rather than a single method if a problem persists then we can rectify easily]
  • Reusability [if you write a method and there is another area od using that method we can call that methos]
  • Performance

VB.Net supports two types of methods:

  • Procedures or subroutines
  • Functions

The thing we can make using procedures can also make with functions and vice versa. So in C, C++, Java we have only functions they feel functions & procedures are the same. But VB.Net people feel that procedures and functions are different.

VB.Net supports strict English grammar and literal keywords like any other language. Procedure outcome of procedures and functions the thing we use is the same but we select based on our requirements.    

Procedures or subroutines:

It is a set of instructions that are used to perform a specific action or task.

Syntax:

[Modifiers] sub methodName ([arginfo]) ‘Statement(s)’ - - - - - - - - - - - - End sub  

Functions:

It is a set of instructions that are used to perform calculations, expression value, and used to return a value.  

Syntax:

[Modifier] function MethodName ([arginfo]) as Data type

‘Statement(s)’

- - - - -

- - - - - -

return < Return value>

End function  

In VB.Net main () method is a procedure but not function [so in VB.Net main () can’t return a value] constructor is also a procedure but not function.

Where argnfo in the above syntax is [By val|By ref] argName as Data type  

By Val:

It is used to pass the value from the caller to the called.

 By val  

Note:

The default mode of passing the information in VB.Net is Byval  

By Ref:

It is used to pass the reference of the variable from the caller to the called.  

Note:

If any modifications are performed at the called then those changes will be reflected at the caller.

    By Ref  

Observation:

Call By Value

Call by value  

Optional Parameters:

It is used to define a parameter as an optional parameter.  

Note:

Optional parameters should always be defined as the last parameters within the method.

  Optional parameters    

Note:

Optional parameters should be assigned with default values.

Param Arrays:

It is used to define a parameter present within the method as an array where the array is a collection of related data type values.  

Note:

‘‘Demo on Methods

Import system

Module Methods ‘‘Demo

Class Calculator

Public Function Add (By Val a As Integer, By Val b As Integer) As                        Integer

Return a + b

End Function

Public Function Add (By Val a As Integer, By Val b As Integer) As                        Integer = 0)

Return Function

End Function

Public Function Add (By Val Param Array nos () As Integer) As Integer

Dim total As Integer = 0

For Each no As Integer In nos

Total = total + no

Next

Return total

End Function

Public sub Display (By Val result As Integer)

Console.WriteLine (Result: {0}, result)

End sub

End class

Sub main ()

Dim calc As New Calculator

Calc.Display (calc.Add (10, 20))

Calc.Display (calc.Add (1, 2, 3, 4, 5, 6, 7, 8, 9))

End sub

End Module

Execute   

Observation:

Whenever more than one method with the same name and different argument information is used then it is said to be method Overloading. [If we want to determine more variables then we can use param arrays   if we don’t know how many arguments are passing we can use For Each instead of for Loop]  

Inheritance:

It is used to reuse or redefine the existing class definition

Note:

.Net supports only single inheritance  

Check out our tutorial on the VB.NET Tutorial

Syntax to inherit the class:

A class Derived class inherits Base class member(s)    - - - - - - - - End class The member’s defined using public, protected; friend and protected friend modifiers at the base class can be accessed directly from the derived class. The ex’s are customized applications purely for understanding the concept.

  Inheritance  

‘‘Demo on Inheritance

Imports system

Module Inheritance ‘‘Demo

Class Parents

Protected Friend sub-MyProperty ( )

Console.WriteLine (“Use property for business”)

End sub

End class

(Class children inherits parents)

Class Children inherits Parents

End class

Sub main ( )

Dim c as New Children

C.MyProperty ( )

End sub

End Module   

 

Also, for an in-depth understanding of VB.Net, click on

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.